|
| 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 | +``` |
0 commit comments