Skip to content

Commit 3fae6c9

Browse files
authored
feat: strigify Maps, Sets and Typed arrays with option es6: true (#3)
1 parent 6927a67 commit 3fae6c9

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

.eslintrc.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
parserOptions:
2+
ecmaVersion: 6
13
extends: eslint:recommended
24
env:
35
node: true
@@ -7,7 +9,7 @@ rules:
79
no-shadow: 1
810
block-scoped-var: 2
911
callback-return: 2
10-
complexity: [2, 13]
12+
complexity: [2, 15]
1113
curly: [2, multi-or-nest, consistent]
1214
dot-location: [2, property]
1315
dot-notation: 2
@@ -27,3 +29,5 @@ rules:
2729
valid-jsdoc: [2, requireReturn: false]
2830
globals:
2931
BigInt: false
32+
Map: true
33+
Set: true

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# json-source-map
22
Parse/stringify JSON and provide source-map for JSON-pointers to all nodes.
33

4-
NEW: BigInt support.
4+
NEW: supports BigInt, Maps, Sets and Typed arrays.
55

66
[![Build Status](https://travis-ci.org/epoberezkin/json-source-map.svg?branch=master)](https://travis-ci.org/epoberezkin/json-source-map)
77
[![npm version](https://badge.fury.io/js/json-source-map.svg)](https://www.npmjs.com/package/json-source-map)
@@ -131,6 +131,7 @@ Comparison with the standard `JSON.stringify`:
131131

132132
Options:
133133
- _space_: same as `space` parameter.
134+
- _es6_: stringify ES6 Maps, Sets and Typed arrays (as JSON arrays).
134135

135136

136137
## License

index.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ exports.stringify = function (data, _, options) {
279279
var line = 0;
280280
var column = 0;
281281
var pos = 0;
282+
var es6 = options && options.es6 && typeof Map == 'function';
282283
_stringify(data, 0, '');
283284
return {
284285
json: json,
@@ -295,14 +296,24 @@ exports.stringify = function (data, _, options) {
295296
case 'string':
296297
out(quoted(_data)); break;
297298
case 'object':
298-
if (_data === null)
299+
if (_data === null) {
299300
out('null');
300-
else if (typeof _data.toJSON == 'function')
301+
} else if (typeof _data.toJSON == 'function') {
301302
out(quoted(_data.toJSON()));
302-
else if (Array.isArray(_data))
303+
} else if (Array.isArray(_data)) {
303304
stringifyArray();
304-
else
305+
} else if (es6) {
306+
if (_data.constructor.BYTES_PER_ELEMENT)
307+
stringifyArray();
308+
else if (_data instanceof Map)
309+
stringifyMapSet();
310+
else if (_data instanceof Set)
311+
stringifyMapSet(true);
312+
else
313+
stringifyObject();
314+
} else {
305315
stringifyObject();
316+
}
306317
}
307318
map(ptr, 'valueEnd');
308319

@@ -350,6 +361,34 @@ exports.stringify = function (data, _, options) {
350361
out('{}');
351362
}
352363
}
364+
365+
function stringifyMapSet(isSet) {
366+
if (_data.size) {
367+
out('{');
368+
var propLvl = lvl + 1;
369+
var first = true;
370+
for (var item of _data.entries()) {
371+
var key = item[0];
372+
var value = isSet ? true : item[1];
373+
if (validType(value)) {
374+
if (!first) out(',');
375+
first = false;
376+
var propPtr = ptr + '/' + escapeJsonPointer(key);
377+
indent(propLvl);
378+
map(propPtr, 'key');
379+
out(quoted(key));
380+
map(propPtr, 'keyEnd');
381+
out(':');
382+
if (whitespace) out(' ');
383+
_stringify(value, propLvl, propPtr);
384+
}
385+
}
386+
indent(lvl);
387+
out('}');
388+
} else {
389+
out('{}');
390+
}
391+
}
353392
}
354393

355394
function out(str) {

spec/.eslintrc.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
parserOptions:
2-
ecmaVersion: 6
31
rules:
42
no-console: 0
53
quotes: 0
64
globals:
75
describe: false
86
it: false
97
Symbol: false
8+
Int8Array: false

spec/index.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,51 @@ describe('stringify', function() {
618618
assert.equal(result.json, '{\n "foo": "bar"\n}');
619619
});
620620

621+
describe('option es6', function() {
622+
it('should strigify Maps', function() {
623+
var data = new Map;
624+
testStringify(data, {}, false, {es6: true});
625+
626+
data.set('foo', 1);
627+
data.set('bar', 2);
628+
testStringify(data, {foo: 1, bar: 2}, false, {es6: true});
629+
testStringify(data, {foo: 1, bar: 2}, false, {es6: true, space: 2});
630+
});
631+
632+
it('should strigify Sets', function() {
633+
var data = new Set;
634+
testStringify(data, {}, false, {es6: true});
635+
636+
data.add('foo');
637+
data.add('bar');
638+
testStringify(data, {foo: true, bar: true}, false, {es6: true});
639+
testStringify(data, {foo: true, bar: true}, false, {es6: true, space: 2});
640+
});
641+
642+
it('should strigify Typed arrays', function() {
643+
var data = new Int8Array(2);
644+
testStringify(data, [0, 0], false, {es6: true});
645+
646+
data[0] = 1;
647+
data[1] = 2;
648+
testStringify(data, [1, 2], false, {es6: true});
649+
testStringify(data, [1, 2], false, {es6: true, space: 2});
650+
});
651+
652+
it('should still strigify Objects', function() {
653+
testStringify({}, {}, false, {es6: true});
654+
testStringify({foo: 1, bar: 2}, {foo: 1, bar: 2}, false, {es6: true});
655+
});
656+
});
657+
621658
function equal(objects) {
622659
for (var i=1; i<objects.length; i++)
623660
assert.deepStrictEqual(objects[0], objects[i]);
624661
}
625662

626-
function testStringify(data, reverseData, skipReverseCheck, whitespace) {
663+
function testStringify(data, reverseData, skipReverseCheck, options) {
627664
if (reverseData === undefined) reverseData = data;
628-
var result = jsonMap.stringify(data, null, whitespace);
665+
var result = jsonMap.stringify(data, null, options);
629666
var json = result.json;
630667
var pointers = result.pointers;
631668

0 commit comments

Comments
 (0)