Merge pull request #1065 from napi-rs/issue/date-type

fix(napi): re-export JsDate as Date in prelude
This commit is contained in:
LongYinan 2022-02-06 21:00:14 +08:00 committed by GitHub
commit 25f2147e6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 20 additions and 0 deletions

View file

@ -169,6 +169,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
("BigUint64Array", "BigUint64Array"), ("BigUint64Array", "BigUint64Array"),
("DataView", "DataView"), ("DataView", "DataView"),
("Date", "Date"), ("Date", "Date"),
("JsDate", "Date"),
("JsBuffer", "Buffer"), ("JsBuffer", "Buffer"),
("Buffer", "Buffer"), ("Buffer", "Buffer"),
("Vec", "Array<{}>"), ("Vec", "Array<{}>"),

View file

@ -23,6 +23,8 @@ mod string;
mod symbol; mod symbol;
mod task; mod task;
#[cfg(feature = "napi5")]
pub use crate::JsDate as Date;
pub use array::*; pub use array::*;
pub use arraybuffer::*; pub use arraybuffer::*;
#[cfg(feature = "napi6")] #[cfg(feature = "napi6")]

View file

@ -40,6 +40,7 @@ Generated by [AVA](https://avajs.dev).
/** napi = { version = 2, features = ["serde-json"] } */␊ /** napi = { version = 2, features = ["serde-json"] } */␊
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊
export function returnJsFunction(): (...args: any[]) => any␊ export function returnJsFunction(): (...args: any[]) => any␊
export function dateToNumber(input: Date): number␊
export function eitherStringOrNumber(input: string | number): number␊ export function eitherStringOrNumber(input: string | number): number␊
export function returnEither(input: number): string | number␊ export function returnEither(input: number): string | number␊
export function either3(input: string | number | boolean): number␊ export function either3(input: string | number | boolean): number␊

View file

@ -82,6 +82,7 @@ import {
returnJsFunction, returnJsFunction,
testSerdeRoundtrip, testSerdeRoundtrip,
createObjWithProperty, createObjWithProperty,
dateToNumber,
} from '../' } from '../'
test('export const', (t) => { test('export const', (t) => {
@ -548,3 +549,10 @@ Napi4Test('Promise should reject raw error in rust', async (t) => {
const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError))) const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError)))
t.is(err, fxError) t.is(err, fxError)
}) })
const Napi5Test = Number(process.versions.napi) >= 5 ? test : test.skip
Napi5Test('Date test', (t) => {
const fixture = new Date('2016-12-24')
t.is(dateToNumber(fixture), fixture.valueOf())
})

View file

@ -30,6 +30,7 @@ export function optionOnly(callback: (arg0?: string | undefined | null) => void)
/** napi = { version = 2, features = ["serde-json"] } */ /** napi = { version = 2, features = ["serde-json"] } */
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void
export function returnJsFunction(): (...args: any[]) => any export function returnJsFunction(): (...args: any[]) => any
export function dateToNumber(input: Date): number
export function eitherStringOrNumber(input: string | number): number export function eitherStringOrNumber(input: string | number): number
export function returnEither(input: number): string | number export function returnEither(input: number): string | number
export function either3(input: string | number | boolean): number export function either3(input: string | number | boolean): number

View file

@ -0,0 +1,6 @@
use napi::bindgen_prelude::*;
#[napi]
fn date_to_number(input: Date) -> Result<f64> {
input.value_of()
}

View file

@ -18,6 +18,7 @@ mod bigint;
mod callback; mod callback;
mod class; mod class;
mod class_factory; mod class_factory;
mod date;
mod either; mod either;
mod r#enum; mod r#enum;
mod error; mod error;