feat(napi): implement Either type

This commit is contained in:
LongYinan 2021-11-02 00:34:19 +08:00
parent ff593eac6a
commit f26cd4aa7b
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
19 changed files with 538 additions and 1 deletions

View file

@ -14,6 +14,13 @@ Generated by [AVA](https://avajs.dev).
export function readFileAsync(path: string): Promise<Buffer>
export function getCwd(callback: (arg0: string) => void): void␊
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊
export function eitherStringOrNumber(input: string | number): number␊
export function returnEither(input: number): string | number␊
export function either3(input: string | number | boolean): number␊
interface Obj {␊
v: string | number␊
}␊
export function either4(input: string | number | boolean | Obj): number␊
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }␊
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }␊
export function enumToI32(e: CustomNumEnum): number␊

View file

@ -26,6 +26,10 @@ import {
getPackageJsonName,
getBuffer,
readFileAsync,
eitherStringOrNumber,
returnEither,
either3,
either4,
} from '../'
test('number', (t) => {
@ -130,3 +134,29 @@ test('async', async (t) => {
await t.throwsAsync(() => readFileAsync('some_nonexist_path.file'))
})
test('either', (t) => {
t.is(eitherStringOrNumber(2), 2)
t.is(eitherStringOrNumber('hello'), 'hello'.length)
})
test('return either', (t) => {
t.is(returnEither(2), 2)
t.is(returnEither(42), '42')
})
test('either3', (t) => {
t.is(either3(2), 2)
t.is(either3('hello'), 'hello'.length)
t.is(either3(true), 1)
t.is(either3(false), 0)
})
test('either4', (t) => {
t.is(either4(2), 2)
t.is(either4('hello'), 'hello'.length)
t.is(either4(true), 1)
t.is(either4(false), 0)
t.is(either4({ v: 1 }), 1)
t.is(either4({ v: 'world' }), 'world'.length)
})

View file

@ -4,6 +4,13 @@ export function sumNums(nums: Array<number>): number
export function readFileAsync(path: string): Promise<Buffer>
export function getCwd(callback: (arg0: string) => void): void
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void
export function eitherStringOrNumber(input: string | number): number
export function returnEither(input: number): string | number
export function either3(input: string | number | boolean): number
interface Obj {
v: string | number
}
export function either4(input: string | number | boolean | Obj): number
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }
export function enumToI32(e: CustomNumEnum): number

View file

@ -0,0 +1,57 @@
use napi::bindgen_prelude::*;
#[napi]
fn either_string_or_number(input: Either<String, u32>) -> u32 {
match input {
Either::A(s) => s.len() as u32,
Either::B(n) => n,
}
}
#[napi]
fn return_either(input: u32) -> Either<String, u32> {
if input > 10 {
Either::A(format!("{}", input))
} else {
Either::B(input)
}
}
#[napi]
fn either3(input: Either3<String, u32, bool>) -> u32 {
match input {
Either3::A(s) => s.len() as u32,
Either3::B(n) => n,
Either3::C(b) => {
if b {
1
} else {
0
}
}
}
}
#[napi(object)]
struct Obj {
pub v: Either<String, u32>,
}
#[napi]
fn either4(input: Either4<String, u32, bool, Obj>) -> u32 {
match input {
Either4::A(s) => s.len() as u32,
Either4::B(n) => n,
Either4::C(b) => {
if b {
1
} else {
0
}
}
Either4::D(f) => match f.v {
Either::A(s) => s.len() as u32,
Either::B(n) => n,
},
}
}

View file

@ -7,6 +7,7 @@ mod array;
mod r#async;
mod callback;
mod class;
mod either;
mod r#enum;
mod error;
mod nullable;