Skip to content

Commit

Permalink
Feat/date opt params (#1759)
Browse files Browse the repository at this point in the history
* add date methods/ constructors with opt params

* Run fmt
  • Loading branch information
cburgos authored and alexcrichton committed Sep 9, 2019
1 parent 8861811 commit f0a55f8
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 2 deletions.
105 changes: 103 additions & 2 deletions crates/js-sys/src/lib.rs
Expand Up @@ -426,8 +426,14 @@ extern "C" {
}

// TODO pre-initialize the Array with the correct length using TrustedLen
impl<A> std::iter::FromIterator<A> for Array where A: AsRef<JsValue> {
fn from_iter<T>(iter: T) -> Array where T: IntoIterator<Item = A> {
impl<A> std::iter::FromIterator<A> for Array
where
A: AsRef<JsValue>,
{
fn from_iter<T>(iter: T) -> Array
where
T: IntoIterator<Item = A>,
{
let out = Array::new();

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

/// Creates a JavaScript Date instance that represents
/// a single moment in time. Date objects are based on a time value that is
/// the number of milliseconds since 1 January 1970 UTC.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
#[wasm_bindgen(constructor)]
pub fn new_with_year_month(year: u32, month: i32) -> Date;

/// Creates a JavaScript Date instance that represents
/// a single moment in time. Date objects are based on a time value that is
/// the number of milliseconds since 1 January 1970 UTC.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
#[wasm_bindgen(constructor)]
pub fn new_with_year_month_day(year: u32, month: i32, day: i32) -> Date;

/// Creates a JavaScript Date instance that represents
/// a single moment in time. Date objects are based on a time value that is
/// the number of milliseconds since 1 January 1970 UTC.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
#[wasm_bindgen(constructor)]
pub fn new_with_year_month_day_hr(year: u32, month: i32, day: i32, hr: i32) -> Date;

/// Creates a JavaScript Date instance that represents
/// a single moment in time. Date objects are based on a time value that is
/// the number of milliseconds since 1 January 1970 UTC.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
#[wasm_bindgen(constructor)]
pub fn new_with_year_month_day_hr_min(
year: u32,
month: i32,
day: i32,
hr: i32,
min: i32,
) -> Date;

/// Creates a JavaScript Date instance that represents
/// a single moment in time. Date objects are based on a time value that is
/// the number of milliseconds since 1 January 1970 UTC.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
#[wasm_bindgen(constructor)]
pub fn new_with_year_month_day_hr_min_sec(
year: u32,
month: i32,
day: i32,
hr: i32,
min: i32,
sec: i32,
) -> Date;

/// Creates a JavaScript Date instance that represents
/// a single moment in time. Date objects are based on a time value that is
/// the number of milliseconds since 1 January 1970 UTC.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
#[wasm_bindgen(constructor)]
pub fn new_with_year_month_day_hr_min_sec_milli(
year: u32,
month: i32,
day: i32,
hr: i32,
min: i32,
sec: i32,
milli: i32,
) -> Date;

/// The `Date.now()` method returns the number of milliseconds
/// elapsed since January 1, 1970 00:00:00 UTC.
///
Expand Down Expand Up @@ -2020,6 +2095,20 @@ extern "C" {
#[wasm_bindgen(method, js_name = setFullYear)]
pub fn set_full_year(this: &Date, year: u32) -> f64;

/// The setFullYear() method sets the full year for a specified date according to local time.
/// Returns new timestamp.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear)
#[wasm_bindgen(method, js_name = setFullYear)]
pub fn set_full_year_with_month(this: &Date, year: u32, month: i32) -> f64;

/// The setFullYear() method sets the full year for a specified date according to local time.
/// Returns new timestamp.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear)
#[wasm_bindgen(method, js_name = setFullYear)]
pub fn set_full_year_with_month_date(this: &Date, year: u32, month: i32, date: i32) -> f64;

/// The setHours() method sets the hours for a specified date according to local time,
/// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented
/// by the updated Date instance.
Expand Down Expand Up @@ -2072,6 +2161,18 @@ extern "C" {
#[wasm_bindgen(method, js_name = setUTCFullYear)]
pub fn set_utc_full_year(this: &Date, year: u32) -> f64;

/// The setUTCFullYear() method sets the full year for a specified date according to universal time.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear)
#[wasm_bindgen(method, js_name = setUTCFullYear)]
pub fn set_utc_full_year_with_month(this: &Date, year: u32, month: i32) -> f64;

/// The setUTCFullYear() method sets the full year for a specified date according to universal time.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear)
#[wasm_bindgen(method, js_name = setUTCFullYear)]
pub fn set_utc_full_year_with_month_date(this: &Date, year: u32, month: i32, date: i32) -> f64;

/// The setUTCHours() method sets the hour for a specified date according to universal time,
/// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time
/// represented by the updated Date instance.
Expand Down
105 changes: 105 additions & 0 deletions crates/js-sys/tests/wasm/Date.rs
Expand Up @@ -143,6 +143,69 @@ fn new() {
assert!(JsValue::from(Date::new(&JsValue::undefined())).is_object());
}

#[wasm_bindgen_test]
fn new_with_year_month() {
let date1 = Date::new_with_year_month(1975, 7);

assert_eq!(date1.get_full_year(), 1975);
assert_eq!(date1.get_month(), 7);
}

#[wasm_bindgen_test]
fn new_with_year_month_day() {
let date1 = Date::new_with_year_month_day(1975, 7, 8);

assert_eq!(date1.get_full_year(), 1975);
assert_eq!(date1.get_month(), 7);
assert_eq!(date1.get_date(), 8);
}

#[wasm_bindgen_test]
fn new_with_year_month_day_hr() {
let date1 = Date::new_with_year_month_day_hr(1975, 7, 8, 4);

assert_eq!(date1.get_full_year(), 1975);
assert_eq!(date1.get_month(), 7);
assert_eq!(date1.get_date(), 8);
assert_eq!(date1.get_hours(), 4);
}

#[wasm_bindgen_test]
fn new_with_year_month_day_hr_min() {
let date1 = Date::new_with_year_month_day_hr_min(1975, 7, 8, 4, 35);

assert_eq!(date1.get_full_year(), 1975);
assert_eq!(date1.get_month(), 7);
assert_eq!(date1.get_date(), 8);
assert_eq!(date1.get_hours(), 4);
assert_eq!(date1.get_minutes(), 35);
}

#[wasm_bindgen_test]
fn new_with_year_month_day_hr_min_sec() {
let date1 = Date::new_with_year_month_day_hr_min_sec(1975, 7, 8, 4, 35, 25);

assert_eq!(date1.get_full_year(), 1975);
assert_eq!(date1.get_month(), 7);
assert_eq!(date1.get_date(), 8);
assert_eq!(date1.get_hours(), 4);
assert_eq!(date1.get_minutes(), 35);
assert_eq!(date1.get_seconds(), 25);
}

#[wasm_bindgen_test]
fn new_with_year_month_day_hr_min_sec_milli() {
let date1 = Date::new_with_year_month_day_hr_min_sec_milli(1975, 7, 8, 4, 35, 25, 300);

assert_eq!(date1.get_full_year(), 1975);
assert_eq!(date1.get_month(), 7);
assert_eq!(date1.get_date(), 8);
assert_eq!(date1.get_hours(), 4);
assert_eq!(date1.get_minutes(), 35);
assert_eq!(date1.get_seconds(), 25);
assert_eq!(date1.get_milliseconds(), 300);
}

#[wasm_bindgen_test]
fn now() {
assert!(Date::now() > 0.);
Expand Down Expand Up @@ -181,6 +244,27 @@ fn set_full_year() {
assert_eq!(event1.get_full_year(), 1976);
}

#[wasm_bindgen_test]
fn set_full_year_with_month() {
let event1 = Date::new(&"August 19, 1976 23:15:30".into());

event1.set_full_year_with_month(1979, 4);

assert_eq!(event1.get_full_year(), 1979);
assert_eq!(event1.get_month(), 4);
}

#[wasm_bindgen_test]
fn set_full_year_with_month_date() {
let event1 = Date::new(&"August 19, 1976 23:15:30".into());

event1.set_full_year_with_month_date(1979, -1, 25);

assert_eq!(event1.get_full_year(), 1978);
assert_eq!(event1.get_month(), 11);
assert_eq!(event1.get_date(), 25);
}

#[wasm_bindgen_test]
fn set_hours() {
let event1 = Date::new(&"August 19, 1975 23:15:30".into());
Expand Down Expand Up @@ -274,6 +358,27 @@ fn set_utc_full_year() {
assert_eq!(event1.get_utc_full_year(), 1975);
}

#[wasm_bindgen_test]
fn set_utc_full_year_with_month() {
let event1 = Date::new(&"December 31, 1975 23:15:30 GMT-3:00".into());

event1.set_utc_full_year_with_month(1975, 6);

assert_eq!(event1.get_utc_full_year(), 1975);
assert_eq!(event1.get_utc_month(), 6);
}

#[wasm_bindgen_test]
fn set_utc_full_year_with_month_date() {
let event1 = Date::new(&"December 31, 1975 23:15:30 GMT-3:00".into());

event1.set_utc_full_year_with_month_date(1975, -2, 21);

assert_eq!(event1.get_utc_full_year(), 1974);
assert_eq!(event1.get_utc_month(), 10);
assert_eq!(event1.get_utc_date(), 21);
}

#[wasm_bindgen_test]
fn set_utc_hours() {
let event1 = Date::new(&"August 19, 1975 23:15:30 GMT-3:00".into());
Expand Down

0 comments on commit f0a55f8

Please sign in to comment.