Skip to content

Commit 13ffd87

Browse files
authored
development (#9)
* Stopped `undefined` reviver return value from deleting undefined values. Trimmed whitespace from context.source. * First pass at updating typing, internal as well as external. * Partial progress on repackaging for ESM. * Packaging changes for supporting ESM (properly!), CJS, and UMD.
1 parent aaa3c76 commit 13ffd87

12 files changed

Lines changed: 1151 additions & 751 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ dist/
4343

4444
test/output.json
4545
test/test.json
46+
/*.tgz

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### 5.0.0
2+
3+
* JSON-Z can now be used as an ESM module. There are no functional changes, but a possible breaking change with how the package is imported:
4+
5+
`import JSONZ from 'json-z';` instead of `import * as JSONZ from 'json-z';`
6+
17
### 4.2.1-4.2.6
28

39
* Fixed assorted replacer/reviver bugs.

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ JSON-Z optionally provides special handling for other data types, so that values
118118

119119
[IEEE 754]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
120120

121-
## Short Example
121+
## “Kitchen sink” example
122122

123123
```
124124
{
@@ -129,7 +129,7 @@ JSON-Z optionally provides special handling for other data types, so that values
129129
lineBreaks: "Look, Mom! \
130130
No \\n's!",
131131
million: 1_000_000, // Underscore separators in numbers allowed
132-
hexadecimal: 0xdecaf,
132+
hexadecimal: /* block comment */ 0xdecaf,
133133
// Leading 0 indicates octal if no non-octal digits (8, 9) follow
134134
octal: [0o7, 074],
135135
binary: 0b100101,
@@ -170,7 +170,7 @@ const JSONZ = require('json-z')
170170
Or, as TypeScript:
171171

172172
```typescript
173-
import * as JSONZ from 'json-z';
173+
import JSONZ from 'json-z';
174174
```
175175

176176
### Browsers
@@ -488,11 +488,6 @@ If `<file>` is not provided, then STDIN is used.
488488
- `-V`, `--version`: Output the version number
489489
- `-h`, `--help`: Output usage information
490490

491-
### Formal Grammar
492-
493-
Note: For simplicity's sake, the parsing of line and block comments is omitted from the grammar below.
494-
495-
496491
### Development
497492

498493
```sh

lib/bignumber-util.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { JsonZOptions, Quote } from './options-manager';
2+
3+
export function getBigDecimalType(): string;
4+
export function getDecimalType(): string;
5+
export function hasBigDecimal(): boolean;
6+
export function hasDecimal(): boolean;
7+
export function isBigDecimal(value: unknown): boolean;
8+
export function isBigInt(value: unknown): boolean;
9+
export function isBigNumber(value: unknown): boolean;
10+
export function isDecimal(value: unknown): boolean;
11+
export function isNumberOrBigNumber(value: unknown): boolean;
12+
export function setBigDecimal(bigDoubleClass: any): void;
13+
export function setDecimal(decimalClass: any): void;
14+
export function stringify(value: any, options: JsonZOptions, preferredQuote: Quote, noSuffix: boolean)
15+
export function toBigDecimal(s: string): any;
16+
export function toBigInt(s: string): BigInt;
17+
export function toDecimal(s: string): any;

lib/index.d.ts

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,13 @@
1-
export type JsonZReviver = (key: string, value: any, holder?: any) => any;
2-
3-
export type JsonZReplacer = (holder: any, key: string, value: any) => any;
4-
export type JsonZAllowedKeys = (string | number)[];
5-
6-
export enum Quote {
7-
DOUBLE,
8-
SINGLE,
9-
PREFER_DOUBLE,
10-
PREFER_SINGLE
11-
}
12-
13-
export enum OptionSet {
14-
MAX_COMPATIBILITY = 0,
15-
RELAXED = 1,
16-
THE_WORKS = 2
17-
}
18-
19-
export enum ExtendedTypeMode {
20-
OFF,
21-
AS_FUNCTIONS,
22-
AS_OBJECTS
23-
}
24-
25-
export interface JsonZOptions {
26-
extendedPrimitives?: boolean,
27-
extendedTypes?: ExtendedTypeMode,
28-
primitiveBigDecimal?: boolean;
29-
primitiveBigInt?: boolean;
30-
primitiveDecimal?: boolean;
31-
quote?: '"' | "'" | Quote;
32-
quoteAllKeys?: boolean;
33-
replacer?: JsonZReplacer | JsonZAllowedKeys;
34-
revealHiddenArrayProperties?: boolean;
35-
space?: string | number | String | Number;
36-
sparseArrays?: boolean;
37-
trailingComma?: boolean;
38-
typePrefix?: string;
39-
}
40-
41-
export interface JsonZTypeHandler {
42-
name: string;
43-
test: (instance: any, options?: JsonZOptions) => boolean;
44-
creator: (value: any) => any;
45-
serializer: (instance: any, options?: JsonZOptions) => any;
46-
}
47-
48-
export interface JsonZParseOptions {
49-
reviveTypedContainers?: boolean;
50-
reviver?: JsonZReviver;
51-
}
52-
53-
export function parse(text: string, options?: JsonZParseOptions): any;
54-
export function parse(text: string, reviver?: JsonZReviver, options?: JsonZParseOptions): any;
55-
56-
export function stringify(value: any, replacer?: JsonZReplacer | JsonZAllowedKeys,
57-
space?: string | number | String | Number): string;
58-
export function stringify(value: any, options?: JsonZOptions | OptionSet,
59-
space?: string | number | String | Number): string;
60-
61-
export function setOptions(options: JsonZOptions | OptionSet, extraOptions?: JsonZOptions): void;
62-
export function resetOptions(): void;
63-
64-
export function setParseOptions(options: JsonZParseOptions): void;
65-
66-
export function resetParseOptions(): void;
67-
68-
export function addTypeHandler(handler: JsonZTypeHandler): void;
69-
export function removeTypeHandler(typeName: string): void;
70-
export function resetStandardTypeHandlers(): void;
71-
export function restoreStandardTypeHandlers(): void;
72-
export function globalizeTypeHandlers(prefix?: string): void;
73-
export function removeGlobalizedTypeHandlers(): void;
74-
75-
export const DELETE: Symbol;
76-
export const UNDEFINED: Symbol;
77-
export const LITERALLY_AS: (value: string) => any;
78-
79-
export function hasBigDecimal(): boolean;
80-
81-
export function setBigDecimal(bigDoubleClass: any): void;
82-
83-
export function hasDecimal(): boolean;
84-
85-
export function setDecimal(decimalClass: any): void;
1+
export { hasBigDecimal, hasDecimal, setBigDecimal, setDecimal } from './bignumber-util';
2+
export {
3+
addTypeHandler, ExtendedTypeMode, globalizeTypeHandlers, OptionSet, JsonZOptions, JsonZParseOptions,
4+
removeGlobalizedTypeHandlers, removeTypeHandler, resetOptions, resetParseOptions, resetStandardTypeHandlers,
5+
restoreStandardTypeHandlers, reviveTypeValue, Quote, serializeExtendedType, setOptions, setParseOptions
6+
} from './options-manager';
7+
export { parse } from './parse';
8+
export { stringify } from './stringify';
9+
export { DELETE, LITERALLY_AS, UNDEFINED } from './util';
10+
11+
declare const JSONZ;
12+
13+
export = JSONZ;

lib/options-manager.d.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { JsonZReviver } from './parse';
2+
3+
export const enum Quote {
4+
DOUBLE,
5+
SINGLE,
6+
PREFER_DOUBLE,
7+
PREFER_SINGLE
8+
}
9+
10+
export const enum ExtendedTypeMode {
11+
OFF,
12+
AS_FUNCTIONS,
13+
AS_OBJECTS
14+
}
15+
16+
export type JsonZAllowedKeys = (string | number)[];
17+
18+
export type JsonZReplacer = (holder: any, key: string, value: any) => any;
19+
20+
export const enum OptionSet {
21+
MAX_COMPATIBILITY,
22+
RELAXED,
23+
THE_WORKS
24+
}
25+
26+
export interface JsonZOptions {
27+
extendedPrimitives?: boolean,
28+
extendedTypes?: ExtendedTypeMode,
29+
primitiveBigDecimal?: boolean;
30+
primitiveBigInt?: boolean;
31+
primitiveDecimal?: boolean;
32+
quote?: '"' | "'" | Quote;
33+
quoteAllKeys?: boolean;
34+
replacer?: JsonZReplacer | JsonZAllowedKeys;
35+
revealHiddenArrayProperties?: boolean;
36+
space?: string | number | String | Number;
37+
sparseArrays?: boolean;
38+
trailingComma?: boolean;
39+
typePrefix?: string;
40+
}
41+
42+
export interface JsonZTypeHandler {
43+
name: string;
44+
test: (instance: any, options?: JsonZOptions) => boolean;
45+
creator: (value: any) => any;
46+
serializer: (instance: any, options?: JsonZOptions) => any;
47+
}
48+
49+
export interface JsonZParseOptions {
50+
reviveTypedContainers?: boolean;
51+
reviver?: JsonZReviver;
52+
}
53+
54+
export function addTypeHandler(handler: JsonZTypeHandler): void;
55+
export function getOptions(): JsonZOptions;
56+
export function getOptionSet(set: OptionSet): JsonZOptions;
57+
export function getParseOptions(): JsonZParseOptions;
58+
export function globalizeTypeHandlers(prefix?: string): void;
59+
export function removeGlobalizedTypeHandlers(): void;
60+
export function removeTypeHandler(typeName: string): void;
61+
export function resetOptions(): void;
62+
export function resetParseOptions(): void;
63+
export function resetStandardTypeHandlers(): void;
64+
export function restoreStandardTypeHandlers(): void;
65+
export function reviveTypeValue(typeNameOrContainer: any, value: any): any;
66+
export function serializeExtendedType(value: any, options: JsonZOptions, stringify: (...args) => any): string | undefined;
67+
export function setOptions(options: JsonZOptions | OptionSet, extraOptions?: JsonZOptions): void;
68+
export function setParseOptions(options: JsonZParseOptions): void;

lib/parse.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { JsonZParseOptions } from './options-manager';
2+
3+
export type JsonZReviver = (key: string, value: any, extra?: (any | { source: string }),
4+
noContext?: boolean) => any;
5+
6+
export function parse<T = any>(text: string, options?: JsonZParseOptions): T;
7+
export function parse<T = any>(text: string, reviver?: JsonZReviver, options?: JsonZParseOptions): T;

lib/stringify.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { JsonZAllowedKeys, JsonZOptions, JsonZReplacer, OptionSet } from './options-manager';
2+
3+
export function stringify(value: any, replacer?: JsonZReplacer | JsonZAllowedKeys,
4+
space?: string | number | String | Number): string;
5+
export function stringify(value: any, options?: JsonZOptions | OptionSet,
6+
space?: string | number | String | Number): string;

lib/util.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export declare class LITERALLY_AS_CLASS {
2+
constructor(value)
3+
}
4+
5+
export declare class ValueSourceWrapper {
6+
constructor (source: string, value: any)
7+
}
8+
9+
export type TypeContainer = { _$_: string, _$_value: any };
10+
11+
export declare function createTypeContainer(type: string, value: any): TypeContainer
12+
export declare function isBinaryDigit(c?: string): boolean
13+
export declare function isDigit(c?: string): boolean
14+
export declare function isHexDigit(c?: string): boolean
15+
export declare function isIdContinueChar(c?: string): boolean
16+
export declare function isIdStartChar(c?: string): boolean
17+
export declare function isOctalDigit(c?: string): boolean
18+
export declare function isSpaceSeparator(c?: string): boolean
19+
export declare function isTypeContainer(obj: unknown): boolean
20+
export declare function LITERALLY_AS(text?: string): LITERALLY_AS_CLASS
21+
export declare function unwrap(obj: any): any
22+
23+
export const DELETE: Symbol;
24+
export const LITERALLY_AS: (value: string) => any;
25+
export const UNDEFINED: Symbol;

0 commit comments

Comments
 (0)