Skip to content

Commit 841dae6

Browse files
committed
Release : Initial
0 parents  commit 841dae6

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/*
2+
exclude/*

LICENSE.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ISC License
2+
3+
Copyright 2018, Saksham (DawnImpulse)
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
6+
provided that the above copyright notice and this permission notice appear in all copies.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
9+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
11+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
12+
OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
# json-key-sort
3+
4+
> Sorting a json object based on keys either ascending or descending & even recursively
5+
6+
7+
### Example -
8+
> Note : focus on **keys** not values
9+
10+
~~~~
11+
const json = require('json-key-sort');
12+
var data = {
13+
"zoho": 4,
14+
"drake": 2,
15+
"yogo": {
16+
"jout": "3.2",
17+
"aou": {
18+
"yoyo": "3.1.3",
19+
"aalo": "3.1.1",
20+
"go": "3.1.2"
21+
}
22+
},
23+
"abc": 1
24+
}
25+
26+
json.sort(data,true)
27+
~~~~
28+
29+
Output will be -
30+
~~~~
31+
{
32+
"abc": 1,
33+
"drake": 2,
34+
"yogo": {
35+
"aou": {
36+
"aalo": "3.1.1",
37+
"go": "3.1.2",
38+
"yoyo": "3.1.3"
39+
},
40+
"jout": "3.2"
41+
},
42+
"zoho": 4
43+
}
44+
~~~~
45+
46+
+ function parameters
47+
- data : the json object to sort
48+
- sort :
49+
- true (default) : ascending sort , since it is default you can only call `json.sort(data)`
50+
- false : descending sort
51+
> Note : the function assumes that you always provide a json object
52+
53+
+ The function work recursively and sort all the inner json objects too.
54+
55+
### Versions
56+
57+
+ `v0.9.0`
58+
+ Initial release - containing basic `sort` function only.
59+
60+
> Pull requests are always welcomed (kindly sign commits with GPG keys. **THANKS**)
61+
62+
63+
64+
### Contact
65+
66+
+ Twitter - [@dawnimpulse](https://twitter.com/dawnimpulse)
67+
68+
69+
70+
### License (ISC)
71+
~~~~
72+
73+
ISC Licence
74+
75+
76+
77+
Copyright 2018 Saksham (DawnImpulse)
78+
79+
80+
81+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
82+
83+
provided that the above copyright notice and this permission notice appear in all copies.
84+
85+
86+
87+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
88+
89+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
90+
91+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
92+
93+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
94+
95+
OR PERFORMANCE OF THIS SOFTWARE.
96+
97+
~~~~

index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
ISC License
3+
4+
Copyright 2018, Saksham (DawnImpulse)
5+
6+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
7+
provided that the above copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
12+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
13+
OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
16+
// ----- PRIVATE -----
17+
18+
/**
19+
* Used to check if data type is json or not
20+
* @param data
21+
* @return {string}
22+
*/
23+
const typeOf = function (data) {
24+
const objectConstructor = {}.constructor;
25+
if (data.constructor === objectConstructor) {
26+
return "OBJECT";
27+
} else {
28+
return "";
29+
}
30+
};
31+
32+
// ------ EXPORT -----
33+
34+
/**
35+
* json sorting based on keys
36+
* > considering data is a json object only (not array or any thing else)
37+
* @param data - json to be sorted
38+
* @param sort - asc (true) / desc (false)
39+
* @return {{}} - a sorted json object
40+
*/
41+
function jsonSort(data, sort) {
42+
var newKeys = [],
43+
keys,
44+
newData = {};
45+
46+
if (!sort)
47+
sort = false;
48+
49+
keys = Object.keys(data).sort();
50+
51+
if (!sort) {
52+
for (var i = keys.length - 1; i >= 0; i--)
53+
newKeys.push(keys[i])
54+
keys = newKeys
55+
}
56+
57+
for (var j = 0; j < keys.length; j++) {
58+
var key = keys[j];
59+
if (typeOf(data[key]) === "OBJECT")
60+
newData[key] = jsonSort(data[key], sort);
61+
else
62+
newData[key] = data[key]
63+
}
64+
65+
return newData
66+
}
67+
68+
// exporting with name as sort
69+
exports.sort = jsonSort;

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "json-key-sort",
3+
"version": "0.9.0",
4+
"description": "Sorting a json object based on keys either ascending or descending & even recursively",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/DawnImpulse/json-key-sort-js.git"
9+
},
10+
"keywords": [
11+
"json",
12+
"sorting",
13+
"json-sort",
14+
"sort-json",
15+
"key-sort-json"
16+
],
17+
"author": "DawnImpulse",
18+
"license": "ISC"
19+
}

0 commit comments

Comments
 (0)