Skip to content

Commit 4060a75

Browse files
Merge pull request #5 from arshadkazmi42/skipkeys
skip keys handler and eslint support
2 parents a9e1d2a + 19b9d56 commit 4060a75

File tree

5 files changed

+770
-22
lines changed

5 files changed

+770
-22
lines changed

.eslintrc.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"mocha": true,
5+
"es6": true
6+
},
7+
"parserOptions": {
8+
"arrowFunctions": true,
9+
"ecmaFeatures": true,
10+
"ecmaVersion": 8,
11+
"sourceType": "module"
12+
},
13+
"extends": "eslint:recommended",
14+
"rules": {
15+
"indent": ["error", 2],
16+
"linebreak-style": ["error", "unix"],
17+
"quotes": ["error", "single"],
18+
"semi": ["error", "always"],
19+
"no-cond-assign": ["error", "always"],
20+
"no-console": "off"
21+
}
22+
}

index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,57 @@
55
* @param {String} searchValue
66
* @param {String} newValue
77
*/
8-
const jnestedReplace = (input, searchValue, newValue) => {
8+
const jnestedReplace = (input, searchValue, newValue, skipKeys=[]) => {
99

1010
// Validate for input, searchValue and newValue
1111
// throws error if any value is undefined/null
1212
if (!input || !searchValue || !newValue) {
13-
throw 'JSON, searchValue, newValue cannot be null';
13+
throw new Error('JSON, searchValue, newValue cannot be null');
14+
}
15+
16+
// If input is not json
17+
if (!isObject(input) && !isArray(input)) {
18+
throw new Error('Invalid JSON');
1419
}
1520

1621
// Iterate over the object and find and replace values
1722
for (let key in input) {
1823

1924
// If type is object, call the same function recursively
20-
if (typeof input[key] === 'object') {
21-
input[key] = jnestedReplace(input[key], searchValue, newValue);
25+
if (isObject(input[key])) {
26+
input[key] = jnestedReplace(input[key], searchValue, newValue, skipKeys);
2227
continue;
2328
}
2429

2530
// If type is array, call the same function recursively
2631
// for every element of array
27-
if (typeof input[key] === 'array') {
32+
if (isArray(input[key])) {
2833
for (let i=0; i<input[key].length; i++) {
29-
input[key][i] = jnestedReplace(input, searchValue, newValue);
34+
input[key][i] = jnestedReplace(input, searchValue, newValue, skipKeys);
3035
}
3136
continue;
3237
}
3338

34-
// Find and replace the value
35-
input[key] = input[key].replace(searchValue, newValue);
39+
// If the key needs to be skipped.
40+
// Do not process and continue to next element
41+
if (skipKeys.indexOf(key) === -1) {
42+
input[key] = input[key].replace(searchValue, newValue);
43+
}
3644
}
3745

3846
return input;
39-
}
47+
};
48+
49+
50+
// Checks if data is an object
51+
const isObject = (data) => {
52+
return data instanceof Object;
53+
};
54+
55+
// checks if data is an array
56+
const isArray = (data) => {
57+
return data instanceof Array;
58+
};
4059

4160

4261
module.exports = jnestedReplace;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Searches and replace values at every level of nested json ",
55
"main": "index.js",
66
"scripts": {
7+
"lint": "eslint --ignore-path .gitignore .",
8+
"pretest": "eslint --ignore-path .gitignore .",
79
"test": "node_modules/mocha/bin/mocha tests/"
810
},
911
"repository": {
@@ -25,6 +27,8 @@
2527
"homepage": "https://github.com/arshadkazmi42/json-nested-replace#readme",
2628
"devDependencies": {
2729
"chai": "^4.2.0",
28-
"mocha": "^5.2.0"
30+
"mocha": "^5.2.0",
31+
"eslint": "^5.11.1",
32+
"eslint-config-strongloop": "^2.1.0"
2933
}
3034
}

tests/test.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,38 @@ const REPLACE_ARRYA_JSON = {
6767

6868
describe('replace in nested json', () => {
6969
it('should replace all the occurences in object', () => {
70-
const replacedValue = jnestedReplace(INPUT_JSON, 'json-nested-replace', 'jnested-replace')
71-
expect(replacedValue).to.deep.equal(REPLACED_JSON)
72-
});
73-
it('should replace all the occurences in string', () => {
74-
const replacedValue = jnestedReplace(INPUT_JSON.name, 'json-nested-replace', 'jnested-replace')
75-
expect(replacedValue).to.deep.equal(REPLACED_JSON.name)
70+
const replacedValue = jnestedReplace(Object.assign({}, INPUT_JSON), 'json-nested-replace', 'jnested-replace');
71+
expect(replacedValue).to.deep.equal(REPLACED_JSON);
7672
});
7773
it('should replace all the occurences in object and array', () => {
78-
const replacedValue = jnestedReplace(ARRAY_JSON, 'json-nested', 'jnested')
79-
expect(replacedValue).to.deep.equal(REPLACE_ARRYA_JSON)
74+
const replacedValue = jnestedReplace(Object.assign({}, ARRAY_JSON), 'json-nested', 'jnested');
75+
expect(replacedValue).to.deep.equal(REPLACE_ARRYA_JSON);
8076
});
8177
it('should throw error of empty input values', () => {
8278
try {
83-
jnestedReplace()
79+
jnestedReplace();
80+
} catch (err) {
81+
expect(err.message).to.equal('JSON, searchValue, newValue cannot be null');
82+
}
83+
});
84+
it('should throw error if input is not json', () => {
85+
try {
86+
jnestedReplace(INPUT_JSON.name, 'json-nested-replace', 'jnested-replace');
8487
} catch (err) {
85-
expect(err).to.equal('JSON, searchValue, newValue cannot be null');
88+
expect(err.message).to.equal('Invalid JSON');
8689
}
8790
});
91+
it('should skip keys while replacing', () => {
92+
const INPUT_JSON = {
93+
'name': 'json-nested-replace',
94+
'author': 'Arshad Kazmi',
95+
'repository': {
96+
'url': 'https://github.com/arshadkazmi42/json-nested-replace',
97+
'language': 'js'
98+
}
99+
};
100+
const replacedValue = jnestedReplace(Object.assign({}, INPUT_JSON), 'json-nested-replace', 'jnested-replace', ['url']);
101+
expect(replacedValue.repository.url).to.equal(INPUT_JSON.repository.url);
102+
expect(replacedValue.name).to.be.equal('jnested-replace');
103+
});
88104
});

0 commit comments

Comments
 (0)