This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
23 changed files
with
128 additions
and
112 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,12 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
|
||
import { createTsonDeserialize, createTsonParser } from "./deserialize.js"; | ||
import { createTsonSerialize, createTsonStringify } from "./serialize.js"; | ||
import { TsonOptions } from "./types.js"; | ||
|
||
export const createTson = (opts: TsonOptions) => ({ | ||
deserialize: createTsonDeserialize(opts), | ||
parse: createTsonParser(opts), | ||
serialize: createTsonSerialize(opts), | ||
stringify: createTsonStringify(opts), | ||
}); |
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,58 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any, eslint-comments/disable-enable-pair */ | ||
|
||
import { isTsonTuple } from "./internals/isTsonTuple.js"; | ||
import { mapOrReturn } from "./internals/mapOrReturn.js"; | ||
import { | ||
TsonDeserializeFn, | ||
TsonNonce, | ||
TsonOptions, | ||
TsonParseFn, | ||
TsonSerialized, | ||
TsonTransformerSerializeDeserialize, | ||
} from "./types.js"; | ||
|
||
type WalkFn = (value: unknown) => unknown; | ||
type WalkerFactory = (nonce: TsonNonce) => WalkFn; | ||
|
||
type AnyTsonTransformerSerializeDeserialize = | ||
TsonTransformerSerializeDeserialize<any, any>; | ||
|
||
export function createTsonDeserialize(opts: TsonOptions): TsonDeserializeFn { | ||
const typeByKey: Record<string, AnyTsonTransformerSerializeDeserialize> = {}; | ||
|
||
for (const handler of opts.types) { | ||
if (handler.key) { | ||
if (typeByKey[handler.key]) { | ||
throw new Error(`Multiple handlers for key ${handler.key} found`); | ||
} | ||
|
||
typeByKey[handler.key] = | ||
handler as AnyTsonTransformerSerializeDeserialize; | ||
} | ||
} | ||
|
||
const walker: WalkerFactory = (nonce) => { | ||
const walk: WalkFn = (value) => { | ||
if (isTsonTuple(value, nonce)) { | ||
const [type, serializedValue] = value; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const transformer = typeByKey[type]!; | ||
return transformer.deserialize(walk(serializedValue)); | ||
} | ||
|
||
return mapOrReturn(value, walk); | ||
}; | ||
|
||
return walk; | ||
}; | ||
|
||
return ((obj: TsonSerialized) => | ||
walker(obj.nonce)(obj.json)) as TsonDeserializeFn; | ||
} | ||
|
||
export function createTsonParser(opts: TsonOptions): TsonParseFn { | ||
const deserializer = createTsonDeserialize(opts); | ||
|
||
return ((str: string) => | ||
deserializer(JSON.parse(str) as TsonSerialized)) as TsonParseFn; | ||
} |
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
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
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
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,11 +1,8 @@ | ||
export { | ||
createTson, | ||
createTsonDeserialize, | ||
createTsonParser, | ||
createTsonSerialize, | ||
createTsonStringify, | ||
} from "./tson.js"; | ||
export { createTson } from "./createTson.js"; | ||
export { createTsonDeserialize, createTsonParser } from "./deserialize.js"; | ||
export { createTsonSerialize, createTsonStringify } from "./serialize.js"; | ||
|
||
export * from "./handlers/index.js"; | ||
|
||
export type { TsonType } from "./types.js"; | ||
export type { TsonOptions } from "./types.js"; |
File renamed without changes.
File renamed without changes.
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,5 @@ | ||
import { TsonTuple } from "../types.js"; | ||
|
||
export function isTsonTuple(v: unknown, nonce: string): v is TsonTuple { | ||
return Array.isArray(v) && v.length === 3 && v[2] === nonce; | ||
} |
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,26 @@ | ||
import { isPlainObject } from "./isPlainObject.js"; | ||
|
||
/** | ||
* Maps over an object or array, returning a new object or array with the same keys. | ||
* If the input is not an object or array, the input is returned. | ||
*/ | ||
|
||
export function mapOrReturn( | ||
input: unknown, | ||
fn: (val: unknown, key: number | string) => unknown, | ||
): unknown { | ||
if (Array.isArray(input)) { | ||
return input.map(fn); | ||
} | ||
|
||
if (isPlainObject(input)) { | ||
const output: typeof input = {}; | ||
for (const [key, value] of Object.entries(input)) { | ||
output[key] = fn(value, key); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
return input; | ||
} |
File renamed without changes.
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