Skip to content

Commit

Permalink
feat: primitive type convert
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Jan 21, 2024
1 parent c0334a5 commit 6293f21
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "./test/log.deno.test.ts";
import "./test/minipack.test.ts";
import "./test/path.deno.test.ts";
import "./test/platform.deno.test.ts";
import "./test/primitive.test.ts";
import "./test/process.deno.test.ts";
import "./test/stream.test.ts";
import "./test/text.test.ts";
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./src/deflate.ts";
export * from "./src/fetch.ts";
export * from "./src/import.ts";
export * from "./src/minipack.ts";
export * from "./src/primitive.ts";
export * from "./src/stream.ts";
export * from "./src/text.ts";
export * from "./src/time.ts";
Expand Down
1 change: 1 addition & 0 deletions mod.universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./src/deflate.ts";
export * from "./src/fetch.ts";
export * from "./src/import.ts";
export * from "./src/minipack.ts";
export * from "./src/primitive.ts";
export * from "./src/stream.ts";
export * from "./src/text.ts";
export * from "./src/time.ts";
Expand Down
95 changes: 95 additions & 0 deletions src/primitive.ts
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;
}
}
}
46 changes: 46 additions & 0 deletions test/primitive.test.ts
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);
}
});

0 comments on commit 6293f21

Please sign in to comment.