New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json.sortify

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json.sortify - npm Package Compare versions

Comparing version

to
2.0.2

LICENSE

4

build/build.js

@@ -18,5 +18,3 @@ 'use strict';

(function(name, factory) {
if (typeof module != 'undefined')
module.exports = factory();
else if (typeof define == 'function' && typeof define.amd == 'object')
if (typeof define == 'function' && typeof define.amd == 'object')
define('json.sortify', factory);

@@ -23,0 +21,0 @@ else

@@ -1,2 +0,2 @@

'use strict';var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj;};(function(name,factory){if(typeof module!='undefined')module.exports=factory();else if(typeof define=='function'&&_typeof(define.amd)=='object')define('json.sortify',factory);else JSON.sortify=factory();})(undefined,function(){ /*!
'use strict';var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj;};(function(name,factory){if(typeof define=='function'&&_typeof(define.amd)=='object')define('json.sortify',factory);else JSON.sortify=factory();})(undefined,function(){ /*!
* Copyright 2015-2016 Thomas Rosenau

@@ -15,2 +15,2 @@ *

* limitations under the License.
*/'use strict';var sortKeys=function sortKeys(o){if(Array.isArray(o)){return o.map(sortKeys);}else if(o instanceof Object){var _ret=function(){var numeric=[];var nonNumeric=[];Object.keys(o).forEach(function(key){if(/^(0|[1-9][0-9]*)$/.test(key)){numeric.push(+key);}else {nonNumeric.push(key);}});return {v:numeric.sort().concat(nonNumeric.sort()).reduce(function(result,key){result[key]=sortKeys(o[key]);return result;},{})};}();if((typeof _ret==='undefined'?'undefined':_typeof(_ret))==="object")return _ret.v;}return o;};var jsonStringify=JSON.stringify.bind(JSON);var sortify=function sortify(value,replacer,space){var native=jsonStringify(value,replacer,0);if(!native||native[0]!=='{'&&native[0]!=='['){return native;}var cleanObj=JSON.parse(native);return jsonStringify(sortKeys(cleanObj),null,space);};return sortify;});
*/'use strict';var sortKeys=function sortKeys(o){if(Array.isArray(o)){return o.map(sortKeys);}else if(o instanceof Object){var _ret=function(){var numeric=[];var nonNumeric=[];Object.keys(o).forEach(function(key){if(/^(0|[1-9][0-9]*)$/.test(key)){numeric.push(+key);}else {nonNumeric.push(key);}});return {v:numeric.sort(function(a,b){return a-b;}).concat(nonNumeric.sort()).reduce(function(result,key){result[key]=sortKeys(o[key]);return result;},{})};}();if((typeof _ret==='undefined'?'undefined':_typeof(_ret))==="object")return _ret.v;}return o;};var jsonStringify=JSON.stringify.bind(JSON);var sortify=function sortify(value,replacer,space){var native=jsonStringify(value,replacer,0);if(!native||native[0]!=='{'&&native[0]!=='['){return native;}var cleanObj=JSON.parse(native);return jsonStringify(sortKeys(cleanObj),null,space);};return sortify;});

@@ -44,3 +44,5 @@ /*!

// do the rearrangement
return numeric.sort().concat(nonNumeric.sort()).reduce((result, key) => {
return numeric.sort(function (a, b) {
return a - b;
}).concat(nonNumeric.sort()).reduce((result, key) => {
result[key] = sortKeys(o[key]); // recurse!

@@ -47,0 +49,0 @@ return result;

{
"name": "json.sortify",
"description": "A deterministic version of JSON.stringify that sorts object keys alphabetically.",
"version": "2.0.1",
"version": "2.0.2",
"engines": {

@@ -38,12 +38,17 @@ "node": ">=0.10.0"

"babel-preset-es2015": "^6.6.0",
"expect": "^1.15.2",
"istanbul": "^0.3.17",
"mocha": "^2.2.5",
"should": "^7.0.2"
"mocha-phantomjs": "^4.0.2"
},
"scripts": {
"prepublish": "node build/build.js",
"test": "mocha --check-leaks --bail --reporter spec test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec test/"
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha --check-leaks --bail --reporter spec test/",
"test:browser": "npm run test:browser:inject && npm run test:browser:amd",
"test:browser:inject": "mocha-phantomjs test/test.html",
"test:browser:amd": "mocha-phantomjs test/test-amd.html",
"test:cov": "istanbul cover _mocha -- --check-leaks --reporter dot test/",
"test:travis": "npm run test:browser && istanbul cover _mocha --report lcovonly -- --check-leaks --reporter spec test/"
}
}

@@ -1,4 +0,10 @@

JSON.sortify = require('../');
var assert = require('assert');
var should = require('should');
if (typeof module !== 'undefined' && module.exports) {
console.info('Running in NodeJS');
JSON.sortify = require('../');
var expect = require('expect');
} else { // Browser
console.info('Running in browser');
global = window;
// libs provided via <script>
}

@@ -8,7 +14,7 @@ describe('JSON.sortify', function () {

it('should define a function', function () {
JSON.sortify.should.be.a.function;
expect(JSON.sortify).toBeA(Function);
});
it('should take precisely three arguments', function () {
assert.equal(JSON.sortify.length, 3);
expect(JSON.sortify.length).toBe(3);
});

@@ -37,3 +43,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture), JSON.stringify(fixture));
expect(JSON.sortify(fixture)).toEqual(JSON.stringify(fixture));
});

@@ -51,3 +57,3 @@

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture), JSON.stringify(fixture));
expect(JSON.sortify(fixture)).toEqual(JSON.stringify(fixture));
});

@@ -62,3 +68,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture), JSON.stringify(fixture));
expect(JSON.sortify(fixture)).toEqual(JSON.stringify(fixture));
});

@@ -73,3 +79,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture), JSON.stringify(fixture));
expect(JSON.sortify(fixture)).toEqual(JSON.stringify(fixture));
});

@@ -86,3 +92,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture), JSON.stringify(fixture));
expect(JSON.sortify(fixture)).toEqual(JSON.stringify(fixture));
});

@@ -101,3 +107,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture[0], fixture[1]), JSON.stringify(fixture[0], fixture[1]));
expect(JSON.sortify(fixture[0], fixture[1])).toEqual(JSON.stringify(fixture[0], fixture[1]));
});

@@ -118,3 +124,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture[0], null, fixture[1]), JSON.stringify(fixture[0], null, fixture[1]));
expect(JSON.sortify(fixture[0], null, fixture[1])).toEqual(JSON.stringify(fixture[0], null, fixture[1]));
});

@@ -130,3 +136,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify.apply(JSON, fixture), JSON.stringify.apply(JSON, fixture));
expect(JSON.sortify.apply(JSON, fixture)).toEqual(JSON.stringify.apply(JSON, fixture));
});

@@ -149,3 +155,3 @@ });

fixtures.forEach(function (fixture) {
assert.throws(JSON.sortify.bind(JSON, fixture), TypeError);
expect(JSON.sortify.bind(JSON, fixture)).toThrow(TypeError);
});

@@ -164,3 +170,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture[0]), fixture[1]);
expect(JSON.sortify(fixture[0])).toEqual(fixture[1]);
});

@@ -191,3 +197,3 @@ });

fixtures.forEach(function (fixture) {
assert.equal(JSON.sortify(fixture[0], fixture[1], fixture[2]), fixture[3]);
expect(JSON.sortify(fixture[0], fixture[1], fixture[2])).toEqual(fixture[3]);
});

@@ -197,2 +203,23 @@

});
describe('interoperability / interchangeability', function () {
var fixtures = [
1,
[1, {a: 1, b: 2, c: [1, 2]}],
{a: {b:1, toJSON:function (key) { return 'x' + key + 'y'; }}}
];
it('should not depend on the “JSON” scope', function () {
var jsonSortify = JSON.sortify;
fixtures.forEach(function (fixture) {
expect(jsonSortify(fixture)).toEqual(JSON.stringify(fixture));
});
});
it('should allow to overwrite “JSON.stringify”', function () {
var jsonStringifyOriginal = JSON.stringify.bind(JSON);
JSON.stringify = JSON.sortify;
fixtures.forEach(function (fixture) {
expect(JSON.stringify(fixture)).toEqual(jsonStringifyOriginal(fixture));
});
});
});
});