Skip to content

Commit cfbb076

Browse files
committed
Added jest
Added examples Added readme Changed structure to reduce serialised size
1 parent 51e9025 commit cfbb076

17 files changed

+9411
-343
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.eslintrc.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: ['@typescript-eslint'],
5+
extends: [
6+
'eslint:recommended',
7+
'plugin:@typescript-eslint/eslint-recommended',
8+
'plugin:@typescript-eslint/recommended',
9+
'prettier',
10+
],
11+
};

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
typed-stringify-*
2+
/typed-stringify-*
3+
/lib/

.npmignore

-4
This file was deleted.

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## Features
2+
3+
- Stringifies and parses objects while restoring the proper type on parse
4+
- Build in types: string, number, boolean, null, undefined, bigint and Date
5+
- Supports restoring custom types
6+
7+
## Usage
8+
9+
Basic example
10+
11+
```javascript
12+
import { parse, stringify } from 'typed-stringify';
13+
14+
const obj = { a: 'hello', b: [1, 2, 3, 4, 5] };
15+
console.log(obj);
16+
// { a: 'hello', b: [ 1, 2, 3, 4, 5 ] }
17+
const s = stringify(obj);
18+
console.log(s);
19+
// {"a":{"t":"string","v":"hello"},"b":[{"t":"number","v":"1"},{"t":"number","v":"2"},{"t":"number","v":"3"},{"t":"number","v":"4"},{"t":"number","v":"5"}]}
20+
const d = parse(s);
21+
console.log(d);
22+
// { a: 'hello', b: [ 1, 2, 3, 4, 5 ] }
23+
```
24+
25+
Example with bignumber.js library
26+
27+
```javascript
28+
import BigNumber from 'bignumber.js';
29+
import { ICustomParse, ICustomStringify, isITypedValue, IType, ITypedValue, parse, stringify } from 'typed-stringify';
30+
31+
type IMyType = IType | 'BigNumber';
32+
33+
const customStringify: ICustomStringify<IMyType> = (obj) => {
34+
if (obj instanceof BigNumber) {
35+
return { t: 'BigNumber', v: obj.toString() };
36+
}
37+
return undefined;
38+
};
39+
40+
const customParse: ICustomParse = (obj) => {
41+
if (isITypedValue(obj)) {
42+
const { t, v } = obj as ITypedValue<IMyType>;
43+
if (t === 'BigNumber') {
44+
if (v === undefined) {
45+
throw new Error('No value');
46+
}
47+
return { useResult: true, result: new BigNumber(v) };
48+
}
49+
}
50+
return { useResult: false };
51+
};
52+
53+
const obj = {
54+
a: 'hello',
55+
b: [new BigNumber(1), new BigNumber(2), new BigNumber(3), new BigNumber(4), new BigNumber(5)],
56+
};
57+
console.log(obj);
58+
// {
59+
// a: 'hello',
60+
// b: [
61+
// BigNumber { s: 1, e: 0, c: [Array] },
62+
// BigNumber { s: 1, e: 0, c: [Array] },
63+
// BigNumber { s: 1, e: 0, c: [Array] },
64+
// BigNumber { s: 1, e: 0, c: [Array] },
65+
// BigNumber { s: 1, e: 0, c: [Array] }
66+
// ]
67+
// }
68+
const s = stringify(obj, customStringify);
69+
console.log(s);
70+
// {"a":{"t":"string","v":"hello"},"b":[{"t":"BigNumber","v":"1"},{"t":"BigNumber","v":"2"},{"t":"BigNumber","v":"3"},{"t":"BigNumber","v":"4"},{"t":"BigNumber","v":"5"}]}
71+
const d = parse(s, customParse);
72+
console.log(d);
73+
// {
74+
// a: 'hello',
75+
// b: [
76+
// BigNumber { s: 1, e: 0, c: [Array] },
77+
// BigNumber { s: 1, e: 0, c: [Array] },
78+
// BigNumber { s: 1, e: 0, c: [Array] },
79+
// BigNumber { s: 1, e: 0, c: [Array] },
80+
// BigNumber { s: 1, e: 0, c: [Array] }
81+
// ]
82+
// }
83+
```

jestconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"transform": {
3+
"^.+\\.(t|j)sx?$": "ts-jest"
4+
},
5+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
7+
}

0 commit comments

Comments
 (0)