Skip to content

Commit f0a55f8

Browse files
cburgosalexcrichton
authored andcommitted
Feat/date opt params (#1759)
* add date methods/ constructors with opt params * Run fmt
1 parent 8861811 commit f0a55f8

File tree

2 files changed

+208
-2
lines changed

2 files changed

+208
-2
lines changed

crates/js-sys/src/lib.rs

+103-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,14 @@ extern "C" {
426426
}
427427

428428
// TODO pre-initialize the Array with the correct length using TrustedLen
429-
impl<A> std::iter::FromIterator<A> for Array where A: AsRef<JsValue> {
430-
fn from_iter<T>(iter: T) -> Array where T: IntoIterator<Item = A> {
429+
impl<A> std::iter::FromIterator<A> for Array
430+
where
431+
A: AsRef<JsValue>,
432+
{
433+
fn from_iter<T>(iter: T) -> Array
434+
where
435+
T: IntoIterator<Item = A>,
436+
{
431437
let out = Array::new();
432438

433439
for value in iter {
@@ -1992,6 +1998,75 @@ extern "C" {
19921998
#[wasm_bindgen(constructor)]
19931999
pub fn new_0() -> Date;
19942000

2001+
/// Creates a JavaScript Date instance that represents
2002+
/// a single moment in time. Date objects are based on a time value that is
2003+
/// the number of milliseconds since 1 January 1970 UTC.
2004+
///
2005+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
2006+
#[wasm_bindgen(constructor)]
2007+
pub fn new_with_year_month(year: u32, month: i32) -> Date;
2008+
2009+
/// Creates a JavaScript Date instance that represents
2010+
/// a single moment in time. Date objects are based on a time value that is
2011+
/// the number of milliseconds since 1 January 1970 UTC.
2012+
///
2013+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
2014+
#[wasm_bindgen(constructor)]
2015+
pub fn new_with_year_month_day(year: u32, month: i32, day: i32) -> Date;
2016+
2017+
/// Creates a JavaScript Date instance that represents
2018+
/// a single moment in time. Date objects are based on a time value that is
2019+
/// the number of milliseconds since 1 January 1970 UTC.
2020+
///
2021+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
2022+
#[wasm_bindgen(constructor)]
2023+
pub fn new_with_year_month_day_hr(year: u32, month: i32, day: i32, hr: i32) -> Date;
2024+
2025+
/// Creates a JavaScript Date instance that represents
2026+
/// a single moment in time. Date objects are based on a time value that is
2027+
/// the number of milliseconds since 1 January 1970 UTC.
2028+
///
2029+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
2030+
#[wasm_bindgen(constructor)]
2031+
pub fn new_with_year_month_day_hr_min(
2032+
year: u32,
2033+
month: i32,
2034+
day: i32,
2035+
hr: i32,
2036+
min: i32,
2037+
) -> Date;
2038+
2039+
/// Creates a JavaScript Date instance that represents
2040+
/// a single moment in time. Date objects are based on a time value that is
2041+
/// the number of milliseconds since 1 January 1970 UTC.
2042+
///
2043+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
2044+
#[wasm_bindgen(constructor)]
2045+
pub fn new_with_year_month_day_hr_min_sec(
2046+
year: u32,
2047+
month: i32,
2048+
day: i32,
2049+
hr: i32,
2050+
min: i32,
2051+
sec: i32,
2052+
) -> Date;
2053+
2054+
/// Creates a JavaScript Date instance that represents
2055+
/// a single moment in time. Date objects are based on a time value that is
2056+
/// the number of milliseconds since 1 January 1970 UTC.
2057+
///
2058+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
2059+
#[wasm_bindgen(constructor)]
2060+
pub fn new_with_year_month_day_hr_min_sec_milli(
2061+
year: u32,
2062+
month: i32,
2063+
day: i32,
2064+
hr: i32,
2065+
min: i32,
2066+
sec: i32,
2067+
milli: i32,
2068+
) -> Date;
2069+
19952070
/// The `Date.now()` method returns the number of milliseconds
19962071
/// elapsed since January 1, 1970 00:00:00 UTC.
19972072
///
@@ -2020,6 +2095,20 @@ extern "C" {
20202095
#[wasm_bindgen(method, js_name = setFullYear)]
20212096
pub fn set_full_year(this: &Date, year: u32) -> f64;
20222097

2098+
/// The setFullYear() method sets the full year for a specified date according to local time.
2099+
/// Returns new timestamp.
2100+
///
2101+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear)
2102+
#[wasm_bindgen(method, js_name = setFullYear)]
2103+
pub fn set_full_year_with_month(this: &Date, year: u32, month: i32) -> f64;
2104+
2105+
/// The setFullYear() method sets the full year for a specified date according to local time.
2106+
/// Returns new timestamp.
2107+
///
2108+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear)
2109+
#[wasm_bindgen(method, js_name = setFullYear)]
2110+
pub fn set_full_year_with_month_date(this: &Date, year: u32, month: i32, date: i32) -> f64;
2111+
20232112
/// The setHours() method sets the hours for a specified date according to local time,
20242113
/// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented
20252114
/// by the updated Date instance.
@@ -2072,6 +2161,18 @@ extern "C" {
20722161
#[wasm_bindgen(method, js_name = setUTCFullYear)]
20732162
pub fn set_utc_full_year(this: &Date, year: u32) -> f64;
20742163

2164+
/// The setUTCFullYear() method sets the full year for a specified date according to universal time.
2165+
///
2166+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear)
2167+
#[wasm_bindgen(method, js_name = setUTCFullYear)]
2168+
pub fn set_utc_full_year_with_month(this: &Date, year: u32, month: i32) -> f64;
2169+
2170+
/// The setUTCFullYear() method sets the full year for a specified date according to universal time.
2171+
///
2172+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear)
2173+
#[wasm_bindgen(method, js_name = setUTCFullYear)]
2174+
pub fn set_utc_full_year_with_month_date(this: &Date, year: u32, month: i32, date: i32) -> f64;
2175+
20752176
/// The setUTCHours() method sets the hour for a specified date according to universal time,
20762177
/// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time
20772178
/// represented by the updated Date instance.

crates/js-sys/tests/wasm/Date.rs

+105
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,69 @@ fn new() {
143143
assert!(JsValue::from(Date::new(&JsValue::undefined())).is_object());
144144
}
145145

146+
#[wasm_bindgen_test]
147+
fn new_with_year_month() {
148+
let date1 = Date::new_with_year_month(1975, 7);
149+
150+
assert_eq!(date1.get_full_year(), 1975);
151+
assert_eq!(date1.get_month(), 7);
152+
}
153+
154+
#[wasm_bindgen_test]
155+
fn new_with_year_month_day() {
156+
let date1 = Date::new_with_year_month_day(1975, 7, 8);
157+
158+
assert_eq!(date1.get_full_year(), 1975);
159+
assert_eq!(date1.get_month(), 7);
160+
assert_eq!(date1.get_date(), 8);
161+
}
162+
163+
#[wasm_bindgen_test]
164+
fn new_with_year_month_day_hr() {
165+
let date1 = Date::new_with_year_month_day_hr(1975, 7, 8, 4);
166+
167+
assert_eq!(date1.get_full_year(), 1975);
168+
assert_eq!(date1.get_month(), 7);
169+
assert_eq!(date1.get_date(), 8);
170+
assert_eq!(date1.get_hours(), 4);
171+
}
172+
173+
#[wasm_bindgen_test]
174+
fn new_with_year_month_day_hr_min() {
175+
let date1 = Date::new_with_year_month_day_hr_min(1975, 7, 8, 4, 35);
176+
177+
assert_eq!(date1.get_full_year(), 1975);
178+
assert_eq!(date1.get_month(), 7);
179+
assert_eq!(date1.get_date(), 8);
180+
assert_eq!(date1.get_hours(), 4);
181+
assert_eq!(date1.get_minutes(), 35);
182+
}
183+
184+
#[wasm_bindgen_test]
185+
fn new_with_year_month_day_hr_min_sec() {
186+
let date1 = Date::new_with_year_month_day_hr_min_sec(1975, 7, 8, 4, 35, 25);
187+
188+
assert_eq!(date1.get_full_year(), 1975);
189+
assert_eq!(date1.get_month(), 7);
190+
assert_eq!(date1.get_date(), 8);
191+
assert_eq!(date1.get_hours(), 4);
192+
assert_eq!(date1.get_minutes(), 35);
193+
assert_eq!(date1.get_seconds(), 25);
194+
}
195+
196+
#[wasm_bindgen_test]
197+
fn new_with_year_month_day_hr_min_sec_milli() {
198+
let date1 = Date::new_with_year_month_day_hr_min_sec_milli(1975, 7, 8, 4, 35, 25, 300);
199+
200+
assert_eq!(date1.get_full_year(), 1975);
201+
assert_eq!(date1.get_month(), 7);
202+
assert_eq!(date1.get_date(), 8);
203+
assert_eq!(date1.get_hours(), 4);
204+
assert_eq!(date1.get_minutes(), 35);
205+
assert_eq!(date1.get_seconds(), 25);
206+
assert_eq!(date1.get_milliseconds(), 300);
207+
}
208+
146209
#[wasm_bindgen_test]
147210
fn now() {
148211
assert!(Date::now() > 0.);
@@ -181,6 +244,27 @@ fn set_full_year() {
181244
assert_eq!(event1.get_full_year(), 1976);
182245
}
183246

247+
#[wasm_bindgen_test]
248+
fn set_full_year_with_month() {
249+
let event1 = Date::new(&"August 19, 1976 23:15:30".into());
250+
251+
event1.set_full_year_with_month(1979, 4);
252+
253+
assert_eq!(event1.get_full_year(), 1979);
254+
assert_eq!(event1.get_month(), 4);
255+
}
256+
257+
#[wasm_bindgen_test]
258+
fn set_full_year_with_month_date() {
259+
let event1 = Date::new(&"August 19, 1976 23:15:30".into());
260+
261+
event1.set_full_year_with_month_date(1979, -1, 25);
262+
263+
assert_eq!(event1.get_full_year(), 1978);
264+
assert_eq!(event1.get_month(), 11);
265+
assert_eq!(event1.get_date(), 25);
266+
}
267+
184268
#[wasm_bindgen_test]
185269
fn set_hours() {
186270
let event1 = Date::new(&"August 19, 1975 23:15:30".into());
@@ -274,6 +358,27 @@ fn set_utc_full_year() {
274358
assert_eq!(event1.get_utc_full_year(), 1975);
275359
}
276360

361+
#[wasm_bindgen_test]
362+
fn set_utc_full_year_with_month() {
363+
let event1 = Date::new(&"December 31, 1975 23:15:30 GMT-3:00".into());
364+
365+
event1.set_utc_full_year_with_month(1975, 6);
366+
367+
assert_eq!(event1.get_utc_full_year(), 1975);
368+
assert_eq!(event1.get_utc_month(), 6);
369+
}
370+
371+
#[wasm_bindgen_test]
372+
fn set_utc_full_year_with_month_date() {
373+
let event1 = Date::new(&"December 31, 1975 23:15:30 GMT-3:00".into());
374+
375+
event1.set_utc_full_year_with_month_date(1975, -2, 21);
376+
377+
assert_eq!(event1.get_utc_full_year(), 1974);
378+
assert_eq!(event1.get_utc_month(), 10);
379+
assert_eq!(event1.get_utc_date(), 21);
380+
}
381+
277382
#[wasm_bindgen_test]
278383
fn set_utc_hours() {
279384
let event1 = Date::new(&"August 19, 1975 23:15:30 GMT-3:00".into());

0 commit comments

Comments
 (0)