Skip to content

Commit da3471a

Browse files
committed
Improvement: Using mocha & chai for tests + fixes with descending sort
1 parent 6c5969a commit da3471a

File tree

5 files changed

+362
-3
lines changed

5 files changed

+362
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# json-keys-sort
3-
3+
[![Build Status](https://travis-ci.org/DawnImpulse/json-keys-sort.svg?branch=master)](https://travis-ci.org/DawnImpulse/json-keys-sort)
44
> Sorting a json object based on keys either ascending or descending & even recursively
55
66

@@ -54,6 +54,9 @@ Output will be -
5454

5555
### Versions
5656

57+
+ `v1.1.0`
58+
+ Bug Fixed : descending sort fix
59+
+ Improvement: auto build testing via travis-ci
5760
+ `v1.0.1`
5861
+ Bug Fixed : data null checks & default true fix
5962
+ `v1.0.0`

bash.exe.stackdump

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Stack trace:
2+
Frame Function Args
3+
000FFFF5E18 0018005CE9E (001802391AD, 0018021AC26, 000FFFF5E18, 000FFFF4D10)
4+
000FFFF5E18 00180046559 (00000000000, 00000000000, 00000000000, 001005EDD44)
5+
000FFFF5E18 00180046592 (00180239269, 000FFFF5CC8, 000FFFF5E18, 00000000000)
6+
000FFFF5E18 001800A948F (00000000000, 00000000000, 00000000000, 00000000000)
7+
000FFFF5E18 001800A96DD (000FFFF5E30, 00000000000, 00000000000, 00000000000)
8+
000FFFF5EB0 001800AA98C (000FFFF5E30, 00000000000, 00000000000, 00000000000)
9+
End of stack trace

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function jsonSort(data, sort) {
4343
keys,
4444
newData = {};
4545

46-
if (!sort)
46+
if (sort === undefined)
4747
sort = true;
4848

4949
keys = Object.keys(data).sort();

index.test.js

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
const chai = require('chai');
2+
const index = require('./index');
3+
const expect = chai.expect;
4+
chai.should();
5+
6+
describe('asc & desc tests', () => {
7+
let json0, json1, json2, json3;
8+
before(() => {
9+
json0 = {
10+
"go": 0,
11+
"zebra": 0,
12+
"apple": 0,
13+
"mango": 0
14+
};
15+
json1 = {
16+
"go": 0,
17+
"zebra": {
18+
"demons": 0,
19+
"kettles": 0,
20+
"blues": 0
21+
},
22+
"apple": 0,
23+
"mango": 0
24+
};
25+
json2 = {
26+
"go": 0,
27+
"zebra": {
28+
"demons": 0,
29+
"kettles": 0,
30+
"blues": {
31+
"road": 0,
32+
"monster": 0,
33+
"useless": 0
34+
}
35+
},
36+
"apple": 0,
37+
"mango": 0
38+
};
39+
json3 = {
40+
"go": 0,
41+
"zebra": {
42+
"demons": 0,
43+
"kettles": 0,
44+
"blues": {
45+
"road": 0,
46+
"monster": {
47+
"ghost": 0,
48+
"scotland": 0,
49+
"looming": 0
50+
},
51+
"useless": 0
52+
}
53+
},
54+
"apple": 0,
55+
"mango": 0
56+
}
57+
});
58+
59+
describe('ascending tests', () => {
60+
before(() => {
61+
});
62+
63+
describe('1. sorting json with no nesting', () => {
64+
let sorted;
65+
before(() => {
66+
sorted = index.sort(json0)
67+
});
68+
it('should have 4 keys', function () {
69+
Object.keys(sorted).should.have.lengthOf(4)
70+
});
71+
it('should have first key apple', function () {
72+
Object.keys(sorted)[0].should.equal("apple")
73+
});
74+
it('should have last key zebra', function () {
75+
Object.keys(sorted)[3].should.equal("zebra")
76+
});
77+
});
78+
79+
describe('2. sorting json with single nested key', () => {
80+
let sorted;
81+
before(() => {
82+
sorted = index.sort(json1)
83+
});
84+
it('should have 4 keys', function () {
85+
Object.keys(sorted).should.have.lengthOf(4)
86+
});
87+
it('should have first key apple', function () {
88+
Object.keys(sorted)[0].should.equal("apple")
89+
});
90+
it('should have last key zebra', function () {
91+
Object.keys(sorted)[3].should.equal("zebra")
92+
});
93+
it('expect zebra to be an object', function () {
94+
expect(sorted.zebra).to.be.an('object')
95+
});
96+
it('expect zebra to have length 3', function () {
97+
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
98+
});
99+
it('expect zebra to have first key blues', function () {
100+
Object.keys(sorted.zebra)[0].should.equal("blues")
101+
});
102+
it('expect zebra to have last key kettles', function () {
103+
Object.keys(sorted.zebra)[2].should.equal("kettles")
104+
});
105+
});
106+
107+
describe('3. sorting json with 2 nesting in a key', () => {
108+
let sorted;
109+
before(() => {
110+
sorted = index.sort(json2)
111+
});
112+
it('should have 4 keys', function () {
113+
Object.keys(sorted).should.have.lengthOf(4)
114+
});
115+
it('should have first key apple', function () {
116+
Object.keys(sorted)[0].should.equal("apple")
117+
});
118+
it('should have last key zebra', function () {
119+
Object.keys(sorted)[3].should.equal("zebra")
120+
});
121+
it('expect zebra to be an object', function () {
122+
expect(sorted.zebra).to.be.an('object')
123+
});
124+
it('expect zebra to have length 3', function () {
125+
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
126+
});
127+
it('expect zebra to have first key blues', function () {
128+
Object.keys(sorted.zebra)[0].should.equal("blues")
129+
});
130+
it('expect zebra to have last key kettles', function () {
131+
Object.keys(sorted.zebra)[2].should.equal("kettles")
132+
});
133+
it('expect blues to be an object', function () {
134+
expect(sorted.zebra.blues).to.be.an('object')
135+
});
136+
it('expect blues to have length 3', function () {
137+
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
138+
});
139+
it('expect blues to have first key monster', function () {
140+
Object.keys(sorted.zebra.blues)[0].should.equal("monster")
141+
});
142+
it('expect blues to have last key useless', function () {
143+
Object.keys(sorted.zebra.blues)[2].should.equal("useless")
144+
});
145+
});
146+
147+
describe('4. sorting json with 3 nesting in a key', () => {
148+
let sorted;
149+
before(() => {
150+
sorted = index.sort(json3)
151+
});
152+
it('should have 4 keys', function () {
153+
Object.keys(sorted).should.have.lengthOf(4)
154+
});
155+
it('should have first key apple', function () {
156+
Object.keys(sorted)[0].should.equal("apple")
157+
});
158+
it('should have last key zebra', function () {
159+
Object.keys(sorted)[3].should.equal("zebra")
160+
});
161+
it('expect zebra to be an object', function () {
162+
expect(sorted.zebra).to.be.an('object')
163+
});
164+
it('expect zebra to have length 3', function () {
165+
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
166+
});
167+
it('expect zebra to have first key blues', function () {
168+
Object.keys(sorted.zebra)[0].should.equal("blues")
169+
});
170+
it('expect zebra to have last key kettles', function () {
171+
Object.keys(sorted.zebra)[2].should.equal("kettles")
172+
});
173+
it('expect blues to be an object', function () {
174+
expect(sorted.zebra.blues).to.be.an('object')
175+
});
176+
it('expect blues to have length 3', function () {
177+
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
178+
});
179+
it('expect blues to have first key monster', function () {
180+
Object.keys(sorted.zebra.blues)[0].should.equal("monster")
181+
});
182+
it('expect blues to have last key useless', function () {
183+
Object.keys(sorted.zebra.blues)[2].should.equal("useless")
184+
});
185+
it('expect monster to be an object', function () {
186+
expect(sorted.zebra.blues.monster).to.be.an('object')
187+
});
188+
it('expect monster to have length 3', function () {
189+
expect(Object.keys(sorted.zebra.blues.monster)).to.have.lengthOf(3)
190+
});
191+
it('expect monster to have first key ghost', function () {
192+
Object.keys(sorted.zebra.blues.monster)[0].should.equal("ghost")
193+
});
194+
it('expect monster to have last key scotland', function () {
195+
Object.keys(sorted.zebra.blues.monster)[2].should.equal("scotland")
196+
});
197+
});
198+
});
199+
200+
describe('descending tests', () => {
201+
before(() => {
202+
});
203+
204+
describe('1. sorting json with no nesting', () => {
205+
let sorted;
206+
before(() => {
207+
sorted = index.sort(json0,false);
208+
});
209+
it('should have 4 keys', function () {
210+
Object.keys(sorted).should.have.lengthOf(4)
211+
});
212+
it('should have first key zebra', function () {
213+
Object.keys(sorted)[0].should.equal("zebra")
214+
});
215+
it('should have last key apple', function () {
216+
Object.keys(sorted)[3].should.equal("apple")
217+
});
218+
});
219+
220+
describe('2. sorting json with single nested key', () => {
221+
let sorted;
222+
before(() => {
223+
sorted = index.sort(json1,false)
224+
});
225+
it('should have 4 keys', function () {
226+
Object.keys(sorted).should.have.lengthOf(4)
227+
});
228+
it('should have first key zebra', function () {
229+
Object.keys(sorted)[0].should.equal("zebra")
230+
});
231+
it('should have last key apple', function () {
232+
Object.keys(sorted)[3].should.equal("apple")
233+
});
234+
it('expect zebra to be an object', function () {
235+
expect(sorted.zebra).to.be.an('object')
236+
});
237+
it('expect zebra to have length 3', function () {
238+
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
239+
});
240+
it('expect zebra to have first key kettles', function () {
241+
Object.keys(sorted.zebra)[0].should.equal("kettles")
242+
});
243+
it('expect zebra to have last key blues', function () {
244+
Object.keys(sorted.zebra)[2].should.equal("blues")
245+
});
246+
});
247+
248+
describe('3. sorting json with 2 nesting in a key', () => {
249+
let sorted;
250+
before(() => {
251+
sorted = index.sort(json2,false)
252+
});
253+
it('should have 4 keys', function () {
254+
Object.keys(sorted).should.have.lengthOf(4)
255+
});
256+
it('should have first key zebra', function () {
257+
Object.keys(sorted)[0].should.equal("zebra")
258+
});
259+
it('should have last key apple', function () {
260+
Object.keys(sorted)[3].should.equal("apple")
261+
});
262+
it('expect zebra to be an object', function () {
263+
expect(sorted.zebra).to.be.an('object')
264+
});
265+
it('expect zebra to have length 3', function () {
266+
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
267+
});
268+
it('expect zebra to have first key kettles', function () {
269+
Object.keys(sorted.zebra)[0].should.equal("kettles")
270+
});
271+
it('expect zebra to have last key blues', function () {
272+
Object.keys(sorted.zebra)[2].should.equal("blues")
273+
});
274+
it('expect blues to be an object', function () {
275+
expect(sorted.zebra.blues).to.be.an('object')
276+
});
277+
it('expect blues to have length 3', function () {
278+
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
279+
});
280+
it('expect blues to have first key useless', function () {
281+
Object.keys(sorted.zebra.blues)[0].should.equal("useless")
282+
});
283+
it('expect blues to have last key monster', function () {
284+
Object.keys(sorted.zebra.blues)[2].should.equal("monster")
285+
});
286+
});
287+
288+
describe('4. sorting json with 3 nesting in a key', () => {
289+
let sorted;
290+
before(() => {
291+
sorted = index.sort(json3,false)
292+
});
293+
it('should have 4 keys', function () {
294+
Object.keys(sorted).should.have.lengthOf(4)
295+
});
296+
it('should have first key zebra', function () {
297+
Object.keys(sorted)[0].should.equal("zebra")
298+
});
299+
it('should have last key apple', function () {
300+
Object.keys(sorted)[3].should.equal("apple")
301+
});
302+
it('expect zebra to be an object', function () {
303+
expect(sorted.zebra).to.be.an('object')
304+
});
305+
it('expect zebra to have length 3', function () {
306+
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
307+
});
308+
it('expect zebra to have first key kettles', function () {
309+
Object.keys(sorted.zebra)[0].should.equal("kettles")
310+
});
311+
it('expect zebra to have last key blues', function () {
312+
Object.keys(sorted.zebra)[2].should.equal("blues")
313+
});
314+
it('expect blues to be an object', function () {
315+
expect(sorted.zebra.blues).to.be.an('object')
316+
});
317+
it('expect blues to have length 3', function () {
318+
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
319+
});
320+
it('expect blues to have first key useless', function () {
321+
Object.keys(sorted.zebra.blues)[0].should.equal("useless")
322+
});
323+
it('expect blues to have last key monster', function () {
324+
Object.keys(sorted.zebra.blues)[2].should.equal("monster")
325+
});
326+
it('expect monster to be an object', function () {
327+
expect(sorted.zebra.blues.monster).to.be.an('object')
328+
});
329+
it('expect monster to have length 3', function () {
330+
expect(Object.keys(sorted.zebra.blues.monster)).to.have.lengthOf(3)
331+
});
332+
it('expect monster to have first key scotland', function () {
333+
Object.keys(sorted.zebra.blues.monster)[0].should.equal("scotland")
334+
});
335+
it('expect monster to have last key ghost', function () {
336+
Object.keys(sorted.zebra.blues.monster)[2].should.equal("ghost")
337+
});
338+
});
339+
});
340+
});

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "1.0.1",
44
"description": "Sorting a json object based on keys either ascending or descending & even recursively",
55
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha index.test.js"
8+
},
69
"repository": {
710
"type": "git",
811
"url": "https://github.com/DawnImpulse/json-keys-sort-js.git"
@@ -15,5 +18,9 @@
1518
"key-sort-json"
1619
],
1720
"author": "DawnImpulse",
18-
"license": "ISC"
21+
"license": "ISC",
22+
"devDependencies": {
23+
"chai": "4.1.2",
24+
"mocha": "5.2.0"
25+
}
1926
}

0 commit comments

Comments
 (0)