-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
171 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use nodejs_sys as napi; | ||
use raw::{Env, Local}; | ||
|
||
pub unsafe extern "C" fn new_date(env: Env, out: *mut Local, num: f64) { | ||
let status = napi::napi_create_date(env, num, out); | ||
assert_eq!(status, napi::napi_status::napi_ok); | ||
} | ||
|
||
pub unsafe extern "C" fn value(env: Env, p: Local) -> f64 { | ||
let mut value = 0.0; | ||
let status = napi::napi_get_date_value(env, p, &mut value as *mut f64); | ||
assert_eq!(status, napi::napi_status::napi_ok); | ||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ pub mod string; | |
pub mod tag; | ||
pub mod task; | ||
pub mod handler; | ||
pub mod date; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var addon = require('../native'); | ||
var assert = require('chai').assert; | ||
|
||
describe('JsDate', function() { | ||
it('should create a date', function () { | ||
const date = addon.create_date(); | ||
assert.instanceOf(date, Date); | ||
}); | ||
|
||
it('should create date from time', function () { | ||
const date = addon.create_date_from_value(31415); | ||
assert.instanceOf(date, Date); | ||
assert.equal(date.toUTCString(), new Date(31415).toUTCString()); | ||
}); | ||
|
||
it('should check if date is valid', function () { | ||
const dateIsValid = addon.check_date_is_valid(31415); | ||
assert.isTrue(dateIsValid); | ||
}); | ||
|
||
it('should check if date is invalid', function () { | ||
const dateIsValid = addon.check_date_is_invalid(); | ||
assert.isFalse(dateIsValid); | ||
}); | ||
|
||
it('should get date value', function () { | ||
const dateValue = addon.get_date_value(); | ||
assert.equal(dateValue, 31415); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use neon::prelude::*; | ||
use neon::types::JsDate; | ||
|
||
pub fn create_date(mut cx: FunctionContext) -> JsResult<JsDate> { | ||
let date = cx.date(31415); | ||
Ok(date) | ||
} | ||
|
||
pub fn create_date_from_value(mut cx: FunctionContext) -> JsResult<JsDate> { | ||
let time = cx.argument::<JsNumber>(0)?.value(&mut cx); | ||
let date = cx.date(time); | ||
Ok(date) | ||
} | ||
|
||
pub fn check_date_is_valid(mut cx: FunctionContext) -> JsResult<JsBoolean> { | ||
let time = cx.argument::<JsNumber>(0)?.value(&mut cx); | ||
let date = cx.date(time); | ||
let is_valid = date.is_valid(&mut cx); | ||
Ok(cx.boolean(is_valid)) | ||
} | ||
|
||
pub fn check_date_is_invalid(mut cx: FunctionContext) -> JsResult<JsBoolean> { | ||
let time = 2f64.powf(64.0); | ||
let date = cx.date(time); | ||
let is_valid = date.is_valid(&mut cx); | ||
Ok(cx.boolean(is_valid)) | ||
} | ||
|
||
pub fn get_date_value(mut cx: FunctionContext) -> JsResult<JsNumber> { | ||
let date = cx.date(31415); | ||
let value = date.value(&mut cx); | ||
println!("{:?}", value); | ||
Ok(cx.number(value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters