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

sorted-json

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sorted-json - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

test/test.js

9

package.json
{
"name": "sorted-json",
"version": "0.1.2",
"version": "0.2.0",
"description": "a sorting-keys-versiong for `JSON.stringify`",
"main": "sorted-json.js",
"devDependencies": {
"mocha": "2.0.1"
},
"scripts": {
"test": "mocha"
},
"repository": {

@@ -12,2 +18,3 @@ "type": "git",

"json",
"sortify",
"stringify",

@@ -14,0 +21,0 @@ "sort"

# sorted-json-js
a sorting-keys-versiong for `JSON.stringify`
## Quick Example
Sort keys and array elements in a JSON.
## Quick Example:
```js
sortedJSON = require('sorted-json');
var sortedJSON = require('./sorted-json');
obj = {
d: [1.1, false, 'abc'],
c: {
cc: [1, true, function(){}],
cb: 2,
ca: 1
var obj = {
numberArray: [3, 1, 2],
anotherJSON: {
stringArray: ['cat', 'book', 'apple'],
numberA: 2,
numberB: 1
},
b: 2,
a: 1
number1: 2,
number2: 1
};
console.log(sortedJSON.stringify(obj));
var sortedObj = sortedJSON.sortify(obj);
console.log(JSON.stringify(sortedObj, null, ' '));
// Output: `{"a":1,"b":2,"c":{"ca":1,"cb":2,"cc":[1,true,"<FUNCTION>"]},"d":[1.1,false,"abc"]}`
/* Output:
{
"anotherJSON": {
"numberA": 2,
"numberB": 1,
"stringArray": [
"apple",
"book",
"cat"
]
},
"number1": 2,
"number2": 1,
"numberArray": [
1,
2,
3
]
}
*/
```
### Install:
# Functions:
### sortify(obj [, options])
###### obj (object)
The object to sort
###### options (object)
An object which defaults to
```js
{
sortBy : null, // Specifies a function that defines the sort order. Same to `compareFunction` parameter in `Array.prototype.sort(compareFunction)`
sortKey : true, // Sort the keys in `obj` or not
sortArray: true, // Sort the array elements in `obj or not
stringify: false, // Output the stringified `obj` or not (Using `JSON.stringify()`)
replacer : null, // Parameter for `JSON.stringify()`
space : null, // Parameter for `JSON.stringify()`
}
```
### stringify(obj, compareFunction) ***DEPRECATED***
###### obj (object)
The object to sort
###### compareFunction (function)
Specifies a function that defines the sort order. Same to `compareFunction` parameter in `Array.prototype.sort(compareFunction)`.
# Install:
```shell

@@ -30,3 +86,10 @@ npm install sorted-json

### License
# Test:
```shell
npm test
```
# License
[MIT](LICENSE)

@@ -37,2 +100,2 @@

[npm-url]: https://npmjs.org/package/sorted-json
[npm-image]: http://img.shields.io/npm/v/sorted-json.svg
[npm-image]: http://img.shields.io/npm/v/sorted-json.svg
'use strict'
exports.stringify = function(obj, compareFunction) {
var DEBUG = false;
var NON_SORTABLE_TYPES = ['undefined', 'string', 'number', 'boolean', 'function'];
var DEFAULT_SORT_OPTIONS = {
sortby : null,
sortKey : true,
sortArray: true,
stringify: false,
replacer : null,
space : null,
};
var _sortify = function(obj, options) {
for (var i = 0; i < NON_SORTABLE_TYPES.length; i++) {
if (NON_SORTABLE_TYPES[i] === typeof obj || obj === null) {
return obj;
}
}
if (Array.isArray(obj)) {
if (options.sortArray === true) {
obj.sort(options.sortby);
}
for (var i = 0; i < obj.length; i++) {
obj[i] = _sortify(obj[i], options);
}
return obj;
} else {
if (options.sortKey === true) {
var sortedObj = {};
var keyList = [];
for (var k in obj) {
keyList.push(k);
}
keyList.sort(options.sortby);
for (var i = 0; i < keyList.length; i++) {
var k = keyList[i];
var v = obj[k];
sortedObj[k] = _sortify(v, options);
}
return sortedObj;
} else {
for (var k in obj) {
obj[k] = _sortify(obj[k], options);
}
return obj;
}
}
};
exports.sortify = function(obj, options) {
if (!options) {
options = DEFAULT_SORT_OPTIONS;
} else {
for (var k in DEFAULT_SORT_OPTIONS) {
if (typeof options[k] === 'undefined') {
options[k] = DEFAULT_SORT_OPTIONS[k];
}
}
}
var result = _sortify(obj, options);
if (options.stringify === true) {
result = JSON.stringify(result, options.replacer, options.space);
}
return result;
};
/* Deprecated Code below */
var _stringify = function(obj, compareFunction) {
if (typeof obj == 'string') {

@@ -47,1 +127,8 @@ return '"' + obj + '"';

};
exports.stringify = function(obj, compareFunction) {
console.log("The stringify() function is DEPRECATED. Please use sortify() instead.");
return _stringify(obj, compareFunction);
};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc