Skip to content

Commit a46bc86

Browse files
author
Dmitry Dutikov
committed
parse method arguments changed
1 parent 9457039 commit a46bc86

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,15 @@ export class JSON22 {
145145

146146
/**
147147
* @param {string} text
148-
* @param {Record<string, { new (...args: any) }>} [context]
148+
* @param {{
149+
* context?: Record<string, { new (...args: any) }>;
150+
* }} [options]
149151
* */
150-
static parse(text, context= JSON22.#defaultContext) {
152+
static parse(text, options) {
153+
const context = {};
154+
Object.assign(context, this.#defaultContext);
155+
Object.assign(context, options?.context ?? {});
156+
151157
const it = JSON22.#iterate(text);
152158
let stateStack = new JSON22.#Stack([State.value]);
153159
let valueStack = new JSON22.#Stack();

test/constructors.test.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,70 @@ suite('Constructor values parsing tests', () => {
1313
test('parsing root level empty argument constructor value', () => {
1414
const c = new context.EmptyArgument();
1515
const json = 'EmptyArgument()';
16-
const parsed = JSON22.parse(json, context);
16+
const parsed = JSON22.parse(json, { context });
1717
assert.deepEqual(parsed, c);
1818
});
1919

2020
test('parsing single argument constructor value', () => {
2121
const c = new context.SingleArgument('v1');
2222
const json = 'SingleArgument("v1")';
23-
const parsed = JSON22.parse(json, context);
23+
const parsed = JSON22.parse(json, { context });
2424
assert.deepEqual(parsed, c);
2525
});
2626

2727
test('parsing double argument constructor value', () => {
2828
const c = new context.DoubleArgument('v1', 'v2');
2929
const json = 'DoubleArgument("v1", "v2")';
30-
const parsed = JSON22.parse(json, context);
30+
const parsed = JSON22.parse(json, { context });
3131
assert.deepEqual(parsed, c);
3232
});
3333

3434
test('parsing single argument constructor nested in array', () => {
3535
const c = [new context.SingleArgument('v1')];
3636
const json = '[SingleArgument("v1")]';
37-
const parsed = JSON22.parse(json, context);
37+
const parsed = JSON22.parse(json, { context });
3838
assert.deepEqual(parsed, c);
3939
});
4040

4141
test('parsing single argument constructor nested in object', () => {
4242
const c = { k: new context.SingleArgument('v1') };
4343
const json = '{ "k": SingleArgument("v1") }';
44-
const parsed = JSON22.parse(json, context);
44+
const parsed = JSON22.parse(json, { context });
4545
assert.deepEqual(parsed, c);
4646
});
4747

4848
test('parsing true literal as argument', () => {
4949
const c = new context.SingleArgument(true);
5050
const json = 'SingleArgument(true)';
51-
const parsed = JSON22.parse(json, context);
51+
const parsed = JSON22.parse(json, { context });
5252
assert.deepEqual(parsed, c);
5353
});
5454

5555
test('parsing false literal as argument', () => {
5656
const c = new context.SingleArgument(false);
5757
const json = 'SingleArgument(false)';
58-
const parsed = JSON22.parse(json, context);
58+
const parsed = JSON22.parse(json, { context });
5959
assert.deepEqual(parsed, c);
6060
});
6161

6262
test('parsing null literal as argument', () => {
6363
const c = new context.SingleArgument(null);
6464
const json = 'SingleArgument(null)';
65-
const parsed = JSON22.parse(json, context);
65+
const parsed = JSON22.parse(json, { context });
6666
assert.deepEqual(parsed, c);
6767
});
6868

6969
test('parsing number as argument', () => {
7070
const c = new context.SingleArgument(0.1);
7171
const json = 'SingleArgument(0.1)';
72-
const parsed = JSON22.parse(json, context);
72+
const parsed = JSON22.parse(json, { context });
7373
assert.deepEqual(parsed, c);
7474
});
7575

76+
test('parsing without context', () => {
77+
assert.throws(() => JSON22.parse('EmptyArgument()'));
78+
});
79+
7680
test('check for wrong constructor', () => {
7781
assert.throws(() => JSON22.parse('[SingleArgument("1"]'));
7882
});

0 commit comments

Comments
 (0)