Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
parserOptions:
ecmaVersion: 6
extends: eslint:recommended
env:
node: true
Expand All @@ -7,7 +9,7 @@ rules:
no-shadow: 1
block-scoped-var: 2
callback-return: 2
complexity: [2, 13]
complexity: [2, 15]
curly: [2, multi-or-nest, consistent]
dot-location: [2, property]
dot-notation: 2
Expand All @@ -27,3 +29,5 @@ rules:
valid-jsdoc: [2, requireReturn: false]
globals:
BigInt: false
Map: true
Set: true
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# json-source-map
Parse/stringify JSON and provide source-map for JSON-pointers to all nodes.

NEW: BigInt support.
NEW: supports BigInt, Maps, Sets and Typed arrays.

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

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


## License
Expand Down
47 changes: 43 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ exports.stringify = function (data, _, options) {
var line = 0;
var column = 0;
var pos = 0;
var es6 = options && options.es6 && typeof Map == 'function';
_stringify(data, 0, '');
return {
json: json,
Expand All @@ -295,14 +296,24 @@ exports.stringify = function (data, _, options) {
case 'string':
out(quoted(_data)); break;
case 'object':
if (_data === null)
if (_data === null) {
out('null');
else if (typeof _data.toJSON == 'function')
} else if (typeof _data.toJSON == 'function') {
out(quoted(_data.toJSON()));
else if (Array.isArray(_data))
} else if (Array.isArray(_data)) {
stringifyArray();
else
} else if (es6) {
if (_data.constructor.BYTES_PER_ELEMENT)
stringifyArray();
else if (_data instanceof Map)
stringifyMapSet();
else if (_data instanceof Set)
stringifyMapSet(true);
else
stringifyObject();
} else {
stringifyObject();
}
}
map(ptr, 'valueEnd');

Expand Down Expand Up @@ -350,6 +361,34 @@ exports.stringify = function (data, _, options) {
out('{}');
}
}

function stringifyMapSet(isSet) {
if (_data.size) {
out('{');
var propLvl = lvl + 1;
var first = true;
for (var item of _data.entries()) {
var key = item[0];
var value = isSet ? true : item[1];
if (validType(value)) {
if (!first) out(',');
first = false;
var propPtr = ptr + '/' + escapeJsonPointer(key);
indent(propLvl);
map(propPtr, 'key');
out(quoted(key));
map(propPtr, 'keyEnd');
out(':');
if (whitespace) out(' ');
_stringify(value, propLvl, propPtr);
}
}
indent(lvl);
out('}');
} else {
out('{}');
}
}
}

function out(str) {
Expand Down
3 changes: 1 addition & 2 deletions spec/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
parserOptions:
ecmaVersion: 6
rules:
no-console: 0
quotes: 0
globals:
describe: false
it: false
Symbol: false
Int8Array: false
41 changes: 39 additions & 2 deletions spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,51 @@ describe('stringify', function() {
assert.equal(result.json, '{\n "foo": "bar"\n}');
});

describe('option es6', function() {
it('should strigify Maps', function() {
var data = new Map;
testStringify(data, {}, false, {es6: true});

data.set('foo', 1);
data.set('bar', 2);
testStringify(data, {foo: 1, bar: 2}, false, {es6: true});
testStringify(data, {foo: 1, bar: 2}, false, {es6: true, space: 2});
});

it('should strigify Sets', function() {
var data = new Set;
testStringify(data, {}, false, {es6: true});

data.add('foo');
data.add('bar');
testStringify(data, {foo: true, bar: true}, false, {es6: true});
testStringify(data, {foo: true, bar: true}, false, {es6: true, space: 2});
});

it('should strigify Typed arrays', function() {
var data = new Int8Array(2);
testStringify(data, [0, 0], false, {es6: true});

data[0] = 1;
data[1] = 2;
testStringify(data, [1, 2], false, {es6: true});
testStringify(data, [1, 2], false, {es6: true, space: 2});
});

it('should still strigify Objects', function() {
testStringify({}, {}, false, {es6: true});
testStringify({foo: 1, bar: 2}, {foo: 1, bar: 2}, false, {es6: true});
});
});

function equal(objects) {
for (var i=1; i<objects.length; i++)
assert.deepStrictEqual(objects[0], objects[i]);
}

function testStringify(data, reverseData, skipReverseCheck, whitespace) {
function testStringify(data, reverseData, skipReverseCheck, options) {
if (reverseData === undefined) reverseData = data;
var result = jsonMap.stringify(data, null, whitespace);
var result = jsonMap.stringify(data, null, options);
var json = result.json;
var pointers = result.pointers;

Expand Down