-
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.
feat: add atValue util to assist on new confirm, prompt, select api
- Loading branch information
Showing
3 changed files
with
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { type Serial, TypeGuard } from 'type-core'; | ||
|
||
export function safeSerialize(value: any): string { | ||
if (TypeGuard.isUndefined(value) || TypeGuard.isString(value)) { | ||
return String(value); | ||
} else { | ||
return JSON.stringify(walk(value)); | ||
} | ||
} | ||
|
||
function walk(value: any): Serial.Type { | ||
if ( | ||
TypeGuard.isNullish(value) || | ||
TypeGuard.isBoolean(value) || | ||
TypeGuard.isString(value) || | ||
TypeGuard.isNumber(value) | ||
) { | ||
return value; | ||
} | ||
|
||
if ( | ||
TypeGuard.isArray(value) && | ||
Object.getPrototypeOf(value) === Array.prototype | ||
) { | ||
return value.map((item) => walk(item)); | ||
} | ||
|
||
if ( | ||
TypeGuard.isRecord(value) && | ||
Object.getPrototypeOf(value) === Object.prototype | ||
) { | ||
return Object.fromEntries( | ||
Object.entries(value).map(([key, item]) => [key, walk(item)]) | ||
); | ||
} | ||
|
||
throw new Error(`Non serializable value: ${value}`); | ||
} |
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,20 @@ | ||
import type { Dictionary, Serial } from 'type-core'; | ||
|
||
import type { Task } from '../definitions'; | ||
import { safeSerialize } from '../helpers/safe-serialize'; | ||
|
||
/** | ||
* Takes a `tasks` record of `Task`s and returns a function | ||
* that will return the `Task` of the record that matches the | ||
* serialization of `value`; otherwise `null`. | ||
* Particularly useful for stdio tasks such as | ||
* confirm, prompt, and select. | ||
*/ | ||
export function atValue( | ||
tasks: Dictionary<Task> | ||
): (value: Serial.Type) => Task | null { | ||
return (value) => { | ||
const field = safeSerialize(value); | ||
return Object.hasOwnProperty.call(tasks, field) ? tasks[field] : null; | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './at-value'; | ||
export * from './cancellation'; | ||
export * from './fetch'; | ||
export * from './is-ci'; | ||
|