|
| 1 | +export as namespace SlowJsonStringify; |
| 2 | + |
| 3 | +export type JSONPrimitive = |
| 4 | + string |
| 5 | + | number |
| 6 | + | boolean |
| 7 | + | null; |
| 8 | + |
| 9 | +export type AttrType = |
| 10 | + 'string' |
| 11 | + | 'number' |
| 12 | + | 'boolean' |
| 13 | + | 'array' |
| 14 | + | 'null'; |
| 15 | + |
| 16 | +/** |
| 17 | + * SjsSchema |
| 18 | + * |
| 19 | + * Schema that describe the structure of your data. |
| 20 | + * |
| 21 | + * Usage: https://github.com/lucagez/slow-json-stringify#usage |
| 22 | + */ |
| 23 | +export type SjsSchema = { [prop: string]: AttrExecutable | SjsSchema }; |
| 24 | + |
| 25 | +/** |
| 26 | + * SjsSerializer |
| 27 | + * |
| 28 | + * Serialize any object enforcing the provided schema. |
| 29 | + */ |
| 30 | +export type SjsSerializer = (obj: unknown) => string; |
| 31 | + |
| 32 | +/** |
| 33 | + * SjsEscaper |
| 34 | + * |
| 35 | + * Escape any string against the previously provided regex. |
| 36 | + */ |
| 37 | +export type SjsEscaper = (str: string) => string; |
| 38 | + |
| 39 | +/** |
| 40 | + * AttrExecutable |
| 41 | + * |
| 42 | + * Object that should be used only inside sjs schemas. |
| 43 | + */ |
| 44 | +export type AttrExecutable = object; |
| 45 | + |
| 46 | +/** |
| 47 | + * Mapping between AttrType and JSONPrimitive?? |
| 48 | + * |
| 49 | + * The resulting JSONPrimitive should be of the same |
| 50 | + * type of AttrType. As the created template has a spot |
| 51 | + * to host the requested type. |
| 52 | + */ |
| 53 | +export type Serializer = (raw: any) => JSONPrimitive; |
| 54 | + |
| 55 | +/** |
| 56 | + * attr |
| 57 | + * |
| 58 | + * Utility used to define how sjs should handle the |
| 59 | + * provided raw data. |
| 60 | + */ |
| 61 | +export function attr(type: 'array', serializer?: SjsSerializer): AttrExecutable; |
| 62 | +export function attr(type: AttrType, serializer?: Serializer): AttrExecutable; |
| 63 | + |
| 64 | +/** |
| 65 | + * escape |
| 66 | + * |
| 67 | + * Provides basic escaping facilities. |
| 68 | + * @default regex - \\n|\\r|\\t|\\"|\\\\ |
| 69 | + */ |
| 70 | +export function escape(regex?: RegExp): (str: string) => string; |
| 71 | + |
| 72 | +/** |
| 73 | + * sjs |
| 74 | + * |
| 75 | + * compile the provided schema and exports a function |
| 76 | + * that serialize objects enforcing the schema. |
| 77 | + */ |
| 78 | +export function sjs(schema: SjsSchema): SjsSerializer; |
0 commit comments