-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
144 additions
and
0 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
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,95 @@ | ||
/** | ||
* WIP. | ||
*/ | ||
export type MaybeString = string | null | undefined; | ||
|
||
/** | ||
* WIP. | ||
*/ | ||
export type TypeStrict<T extends unknown, U extends boolean> = U extends true ? T : T | undefined; | ||
|
||
/** | ||
* WIP. | ||
*/ | ||
export interface PrimitiveMap{ | ||
"s": string; | ||
"n": number; | ||
"b": boolean; | ||
} | ||
|
||
/** | ||
* WIP. | ||
* @example | ||
* ```ts | ||
* const value = typeDecode("123", "n", true); | ||
* ``` | ||
*/ | ||
export function typeDecode<T extends keyof PrimitiveMap, U extends boolean>(text:MaybeString, type:T, strict?:U):TypeStrict<PrimitiveMap[T], U>{ | ||
switch(type){ | ||
case "s": return <PrimitiveMap[T]>typeS(text, strict); | ||
case "n": return <PrimitiveMap[T]>typeN(text, strict); | ||
case "b": return <PrimitiveMap[T]>typeB(text, strict); | ||
default: throw new Error(); | ||
} | ||
} | ||
|
||
/** | ||
* WIP. | ||
* @example | ||
* ```ts | ||
* const value = typeS("foo", true); | ||
* ``` | ||
*/ | ||
export function typeS<T extends boolean>(text:MaybeString, strict?:T):TypeStrict<string, T>{ | ||
if(text === undefined || text === null){ | ||
if(strict){ | ||
throw new Error(); | ||
} | ||
|
||
return <TypeStrict<string, T>>undefined; | ||
} | ||
|
||
return String(text); | ||
} | ||
|
||
/** | ||
* WIP. | ||
* @example | ||
* ```ts | ||
* const value = typeN("123", true); | ||
* ``` | ||
*/ | ||
export function typeN<T extends boolean>(text:MaybeString, strict?:T):TypeStrict<number, T>{ | ||
const n = Number(text); | ||
|
||
if(text === undefined || text === null || isNaN(n)){ | ||
if(strict){ | ||
throw new Error(); | ||
} | ||
|
||
return <TypeStrict<number, T>>undefined; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
/** | ||
* WIP. | ||
* @example | ||
* ```ts | ||
* const value = typeB("true", true); | ||
* ``` | ||
*/ | ||
export function typeB<T extends boolean>(text:MaybeString, strict?:T):TypeStrict<boolean, T>{ | ||
switch(text){ | ||
case "true": return true; | ||
case "false": return false; | ||
default: { | ||
if(strict){ | ||
throw new Error(); | ||
} | ||
|
||
return <TypeStrict<boolean, T>>undefined; | ||
} | ||
} | ||
} |
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,46 @@ | ||
import {assertEquals} from "../deps.test.ts"; | ||
import {typeDecode, typeS, typeN, typeB} from "../src/primitive.ts"; | ||
|
||
Deno.test({ | ||
name: "Type: Decode", | ||
fn(){ | ||
const result1 = typeDecode("foo", "s", true); | ||
const result2 = typeDecode(null, "s"); | ||
|
||
assertEquals(result1, "foo"); | ||
assertEquals(result2, undefined); | ||
} | ||
}); | ||
|
||
Deno.test({ | ||
name: "Type: Parse String", | ||
fn(){ | ||
const result1 = typeS("foo", true); | ||
const result2 = typeN(null); | ||
|
||
assertEquals(result1, "foo"); | ||
assertEquals(result2, undefined); | ||
} | ||
}); | ||
|
||
Deno.test({ | ||
name: "Type: Parse Number", | ||
fn(){ | ||
const result1 = typeN("12345", true); | ||
const result2 = typeN("foo"); | ||
|
||
assertEquals(result1, 12345); | ||
assertEquals(result2, undefined); | ||
} | ||
}); | ||
|
||
Deno.test({ | ||
name: "Type: Parse Boolean", | ||
fn(){ | ||
const result1 = typeB("true", true); | ||
const result2 = typeN("foo"); | ||
|
||
assertEquals(result1, true); | ||
assertEquals(result2, undefined); | ||
} | ||
}); |