Skip to content

Commit

Permalink
New : Complete code coverage
Browse files Browse the repository at this point in the history
100% code coverage & throw error if not provided object to sort fn
  • Loading branch information
DawnImpulse committed Dec 23, 2019
1 parent 45e5551 commit 92daddb
Show file tree
Hide file tree
Showing 10 changed files with 2,254 additions and 80 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules/*
exclude/*
exclude/*
.idea/*
.nyc_output/*
coverage/*
11 changes: 11 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"cache": false,
"branches": 100,
"lines": 100,
"functions": 100,
"statements": 100,
"check-coverage": true,
"reporter": ["text", "lcov"],
"sourceMap": true,
"instrument": true
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ node_js:
- 'stable'
install:
- npm install
script:
- npm run coverage
branches:
only:
- master
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ISC License

Copyright 2018, Saksham (DawnImpulse)
Copyright 2018-2019, Saksham (DawnImpulse)

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
provided that the above copyright notice and this permission notice appear in all copies.
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# json-keys-sort
[![Build Status](https://travis-ci.org/DawnImpulse/json-keys-sort.svg?branch=master)](https://travis-ci.org/DawnImpulse/json-keys-sort) ![wait-loop](https://img.shields.io/npm/dt/json-keys-sort.svg)
[![Build Status](https://travis-ci.org/DawnImpulse/json-keys-sort.svg?branch=master)](https://travis-ci.org/DawnImpulse/json-keys-sort) [![Coverage Status](https://coveralls.io/repos/github/DawnImpulse/json-keys-sort/badge.svg?branch=master)](https://coveralls.io/github/DawnImpulse/json-keys-sort?branch=master) ![wait-loop](https://img.shields.io/npm/dt/json-keys-sort.svg)
> Sorting a json object based on keys either ascending or descending & even recursively
### Latest Changes
- Added support for Typescript
### Example -
- **sort** now throws an error if not provided with an object
- added support for Typescript

### Example -
> Note : focus on **keys** not values
~~~~
Expand Down Expand Up @@ -53,6 +55,10 @@ Output will be -
+ The function work recursively and sort all the inner json objects too.

### Versions
+ `v2.0.0`
+ New : **100%** code coverage
+ Improvement : **sort** now throws an error if not provided with an object

+ `v1.3.1`
+ Bug Fix : Fixed type of parameter in sort function from JSON to object

Expand Down
9 changes: 0 additions & 9 deletions bash.exe.stackdump

This file was deleted.

8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ const typeOf = function (data) {
* @return {{}} - a sorted json object
*/
function jsonSort(data, sort) {

if (typeOf(data) === "ARRAY") {
let newData = [];
for (let w = 0; w < data.length; w++) {
let d = data[w];
if (typeOf(d) === "OBJECT" || typeOf(d) === "ARRAY")
newData.push(jsonSort(d, sort));
else
else {
newData.push(d)
}
}
return newData
} else {
} else if (typeOf(data)=== "OBJECT") {
let newKeys = [],
keys,
newData = {};
Expand Down Expand Up @@ -90,6 +92,8 @@ function jsonSort(data, sort) {

return newData
}
else
throw new Error("must be an object/array")
}

// exporting with name as sort
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"name": "json-keys-sort",
"version": "1.3.1",
"version": "2.0.0",
"description": "Sorting a json object based on keys either ascending or descending & even recursively",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "mocha index.test.js"
"test": "mocha",
"test:coverage" : "nyc npm test",
"coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
},
"repository": {
"type": "git",
"url": "https://github.com/DawnImpulse/json-keys-sort-js.git"
"url": "https://github.com/DawnImpulse/json-keys-sort.git"
},
"keywords": [
"json",
Expand All @@ -26,8 +28,11 @@
"author": "DawnImpulse",
"license": "ISC",
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0"
"chai": "4.2.0",
"coveralls": "3.0.9",
"mocha": "6.2.2",
"nyc": "15.0.0",
"rewire": "4.0.1"
},
"dependencies": {}
}
41 changes: 38 additions & 3 deletions index.test.js → test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const chai = require('chai');
const index = require('./index');
const rewire = require("rewire");
const index = require('../index');
const main = rewire('../index');
const expect = chai.expect;
chai.should();

Expand Down Expand Up @@ -357,9 +359,11 @@ describe('json array tests', () => {
{
dear: 3,
abe: 4
}
},
"some string"
]
}
},
"some data"
];
let result;

Expand All @@ -377,10 +381,41 @@ describe('json array tests', () => {
expect(Object.keys(obj)[0]).to.be.equal("boy")
});

it('expect object 1.1 key 1 to be string', function () {
const key = Object.keys(result[1])[0];
const actual = result[1][key][1];
expect(actual).to.equal("some string")
});

it('expect object 1.0 [0] key 0 to be abe', function () {
let key = Object.keys(result[1])[0];
let obj = result[1][key];
expect(Object.keys(obj[0])[0]).to.be.equal("abe")
});

it('expect key 2 to be a string', function () {
expect(result[2]).to.equal("some data")
});
})
});

describe('sort()', () => {
it('given invalid object : should throw error', function () {
const given = "string";
try {
const should = index.sort(given);
expect.fail(should);
} catch (e) {
expect(e.toString()).to.equal("Error: must be an object/array")
}
});
});

describe('typeOf()', () => {

it('given any string : should return STRING', function () {
const given = "any thing";
const should = main.__get__("typeOf")(given);
expect(should).to.equal("STRING");
});
});
Loading

0 comments on commit 92daddb

Please sign in to comment.