-
Notifications
You must be signed in to change notification settings - Fork 1
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
3 changed files
with
77 additions
and
7 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,31 @@ | ||
export function is_string(arg: unknown): arg is string { | ||
return typeof arg === 'string'; | ||
} | ||
|
||
export function is_function(arg: unknown): arg is () => unknown { | ||
return typeof arg === 'function'; | ||
} | ||
|
||
export function is_array(arg: unknown): arg is Array<unknown> { | ||
return Array.isArray(arg); | ||
} | ||
|
||
export function is_integer(arg: unknown): arg is number { | ||
return Number.isInteger(arg); | ||
} | ||
|
||
export type Hash_Command = [number, number, string]; | ||
|
||
export function is_hash_command(arg: unknown): arg is Hash_Command { | ||
if (!is_array(arg)) { | ||
return false; | ||
} | ||
if (arg.length !== 3) { | ||
return false; | ||
} | ||
if (!(is_integer(arg[0]) && is_integer(arg[1]))) { | ||
return false; | ||
} | ||
return is_string(arg[2]); | ||
|
||
} |
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,18 @@ | ||
import { is_array, is_hash_command, type Hash_Command} from '../guards'; | ||
|
||
export function parse_hash(hash: string): Array<Hash_Command> | never { | ||
let input; | ||
try { | ||
input = JSON.parse(hash); | ||
} catch (e) { | ||
throw new Error(`invalid hash ${hash}`); | ||
} | ||
if (!is_array(input)) { | ||
throw new Error(`invalid hash ${hash}`); | ||
} | ||
const valid = input.every(is_hash_command); | ||
if (!valid) { | ||
throw new Error(`invalid hash ${hash}`); | ||
} | ||
return input as Array<Hash_Command>; | ||
} |