Skip to content

Commit 19f2651

Browse files
committed
library code
1 parent fb25be6 commit 19f2651

File tree

4 files changed

+362
-0
lines changed

4 files changed

+362
-0
lines changed

index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Iterates through the whole json and replaces searchValue with newValue
3+
* Works on simple string, json, array
4+
* @param {JSON/Array/String} input
5+
* @param {String} searchValue
6+
* @param {String} newValue
7+
*/
8+
const jnestedReplace = (input, searchValue, newValue) => {
9+
10+
// Validate for input, searchValue and newValue
11+
// throws error if any value is undefined/null
12+
if (!input || !searchValue || !newValue) {
13+
throw 'JSON, searchValue, newValue cannot be null';
14+
}
15+
16+
// Iterate over the object and find and replace values
17+
for (let key in input) {
18+
19+
// If type is object, call the same function recursively
20+
if (typeof input[key] === 'object') {
21+
input[key] = jnestedReplace(input[key], searchValue, newValue);
22+
continue;
23+
}
24+
25+
// If type is array, call the same function recursively
26+
// for every element of array
27+
if (typeof input[key] === 'array') {
28+
for (let i=0; i<input[key].length; i++) {
29+
input[key][i] = jnestedReplace(input, searchValue, newValue);
30+
}
31+
continue;
32+
}
33+
34+
// Find and replace the value
35+
input[key] = input[key].replace(searchValue, newValue);
36+
}
37+
38+
return input;
39+
}
40+
41+
42+
module.exports = jnestedReplace;

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "json-nested-replace",
3+
"version": "1.0.0",
4+
"description": "Searches and replace values at every level of nested json ",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node_modules/mocha/bin/mocha tests/"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/arshadkazmi42/json-nested-replace.git"
12+
},
13+
"keywords": [
14+
"search",
15+
"replace",
16+
"json-replace",
17+
"json",
18+
"find-and-replace"
19+
],
20+
"author": "Arshad Kazmi",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/arshadkazmi42/json-nested-replace/issues"
24+
},
25+
"homepage": "https://github.com/arshadkazmi42/json-nested-replace#readme",
26+
"devDependencies": {
27+
"chai": "^4.2.0",
28+
"mocha": "^5.2.0"
29+
}
30+
}

tests/test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const expect = require('chai').expect;
2+
3+
const jnestedReplace = require('../index');
4+
5+
const INPUT_JSON = {
6+
'name': 'json-nested-replace',
7+
'author': 'Arshad Kazmi',
8+
'repository': {
9+
'url': 'https://github.com/arshadkazmi42/json-nested-replace',
10+
'language': 'js'
11+
}
12+
};
13+
14+
const REPLACED_JSON = {
15+
'name': 'jnested-replace',
16+
'author': 'Arshad Kazmi',
17+
'repository': {
18+
'url': 'https://github.com/arshadkazmi42/jnested-replace',
19+
'language': 'js'
20+
}
21+
};
22+
23+
const ARRAY_JSON = {
24+
'name': 'json-nested-replace',
25+
'author': 'Arshad Kazmi',
26+
'repository': {
27+
'url': 'https://github.com/arshadkazmi42/jnested-replace',
28+
'language': 'js'
29+
},
30+
'keywords': [
31+
'json-nested',
32+
'json-nested-replace',
33+
'json'
34+
],
35+
'related': [
36+
{
37+
'name': 'jmon'
38+
},
39+
{
40+
'name': 'json-nested-find-and-replace'
41+
}
42+
]
43+
};
44+
45+
const REPLACE_ARRYA_JSON = {
46+
'name': 'jnested-replace',
47+
'author': 'Arshad Kazmi',
48+
'repository': {
49+
'url': 'https://github.com/arshadkazmi42/jnested-replace',
50+
'language': 'js'
51+
},
52+
'keywords': [
53+
'jnested',
54+
'jnested-replace',
55+
'json'
56+
],
57+
'related': [
58+
{
59+
'name': 'jmon'
60+
},
61+
{
62+
'name': 'jnested-find-and-replace'
63+
}
64+
]
65+
};
66+
67+
68+
describe('replace in nested json', () => {
69+
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)
76+
});
77+
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)
80+
});
81+
it('should throw error of empty input values', () => {
82+
try {
83+
jnestedReplace()
84+
} catch (err) {
85+
expect(err).to.equal('JSON, searchValue, newValue cannot be null');
86+
}
87+
});
88+
});

yarn.lock

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
assertion-error@^1.1.0:
6+
version "1.1.0"
7+
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
8+
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==
9+
10+
balanced-match@^1.0.0:
11+
version "1.0.0"
12+
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
13+
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
14+
15+
brace-expansion@^1.1.7:
16+
version "1.1.11"
17+
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
18+
integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==
19+
dependencies:
20+
balanced-match "^1.0.0"
21+
concat-map "0.0.1"
22+
23+
browser-stdout@1.3.1:
24+
version "1.3.1"
25+
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60"
26+
integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==
27+
28+
chai@^4.2.0:
29+
version "4.2.0"
30+
resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"
31+
integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==
32+
dependencies:
33+
assertion-error "^1.1.0"
34+
check-error "^1.0.2"
35+
deep-eql "^3.0.1"
36+
get-func-name "^2.0.0"
37+
pathval "^1.1.0"
38+
type-detect "^4.0.5"
39+
40+
check-error@^1.0.2:
41+
version "1.0.2"
42+
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
43+
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=
44+
45+
commander@2.15.1:
46+
version "2.15.1"
47+
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f"
48+
integrity sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==
49+
50+
concat-map@0.0.1:
51+
version "0.0.1"
52+
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
53+
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
54+
55+
debug@3.1.0:
56+
version "3.1.0"
57+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
58+
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
59+
dependencies:
60+
ms "2.0.0"
61+
62+
deep-eql@^3.0.1:
63+
version "3.0.1"
64+
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
65+
integrity sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==
66+
dependencies:
67+
type-detect "^4.0.0"
68+
69+
diff@3.5.0:
70+
version "3.5.0"
71+
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
72+
integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==
73+
74+
escape-string-regexp@1.0.5:
75+
version "1.0.5"
76+
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
77+
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
78+
79+
fs.realpath@^1.0.0:
80+
version "1.0.0"
81+
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
82+
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
83+
84+
get-func-name@^2.0.0:
85+
version "2.0.0"
86+
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
87+
integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=
88+
89+
glob@7.1.2:
90+
version "7.1.2"
91+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
92+
integrity sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==
93+
dependencies:
94+
fs.realpath "^1.0.0"
95+
inflight "^1.0.4"
96+
inherits "2"
97+
minimatch "^3.0.4"
98+
once "^1.3.0"
99+
path-is-absolute "^1.0.0"
100+
101+
growl@1.10.5:
102+
version "1.10.5"
103+
resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e"
104+
integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==
105+
106+
has-flag@^3.0.0:
107+
version "3.0.0"
108+
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
109+
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=
110+
111+
he@1.1.1:
112+
version "1.1.1"
113+
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
114+
integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0=
115+
116+
inflight@^1.0.4:
117+
version "1.0.6"
118+
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
119+
integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
120+
dependencies:
121+
once "^1.3.0"
122+
wrappy "1"
123+
124+
inherits@2:
125+
version "2.0.3"
126+
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
127+
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=
128+
129+
minimatch@3.0.4, minimatch@^3.0.4:
130+
version "3.0.4"
131+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
132+
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
133+
dependencies:
134+
brace-expansion "^1.1.7"
135+
136+
minimist@0.0.8:
137+
version "0.0.8"
138+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
139+
integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=
140+
141+
mkdirp@0.5.1:
142+
version "0.5.1"
143+
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
144+
integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=
145+
dependencies:
146+
minimist "0.0.8"
147+
148+
mocha@^5.2.0:
149+
version "5.2.0"
150+
resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.2.0.tgz#6d8ae508f59167f940f2b5b3c4a612ae50c90ae6"
151+
integrity sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==
152+
dependencies:
153+
browser-stdout "1.3.1"
154+
commander "2.15.1"
155+
debug "3.1.0"
156+
diff "3.5.0"
157+
escape-string-regexp "1.0.5"
158+
glob "7.1.2"
159+
growl "1.10.5"
160+
he "1.1.1"
161+
minimatch "3.0.4"
162+
mkdirp "0.5.1"
163+
supports-color "5.4.0"
164+
165+
ms@2.0.0:
166+
version "2.0.0"
167+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
168+
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=
169+
170+
once@^1.3.0:
171+
version "1.4.0"
172+
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
173+
integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
174+
dependencies:
175+
wrappy "1"
176+
177+
path-is-absolute@^1.0.0:
178+
version "1.0.1"
179+
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
180+
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
181+
182+
pathval@^1.1.0:
183+
version "1.1.0"
184+
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
185+
integrity sha1-uULm1L3mUwBe9rcTYd74cn0GReA=
186+
187+
supports-color@5.4.0:
188+
version "5.4.0"
189+
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54"
190+
integrity sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==
191+
dependencies:
192+
has-flag "^3.0.0"
193+
194+
type-detect@^4.0.0, type-detect@^4.0.5:
195+
version "4.0.8"
196+
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
197+
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==
198+
199+
wrappy@1:
200+
version "1.0.2"
201+
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
202+
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

0 commit comments

Comments
 (0)