Skip to content

Commit 3b8f1ee

Browse files
authored
Merge pull request #15 from lucagez/FEATURE/add-typings-on-sjs
#14 add typings on sjs module
2 parents e986d02 + c5ee44e commit 3b8f1ee

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

index.d.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"main": "dist/sjs.js",
77
"module": "dist/sjs.mjs",
88
"unpkg": "dist/sjs.umd.js",
9+
"types": "index.d.ts",
910
"scripts": {
1011
"build": "microbundle",
1112
"build:dev": "microbundle watch",

0 commit comments

Comments
 (0)