Skip to content

Commit c626130

Browse files
Merge pull request #1 from arshadkazmi42/lib-init
Lib init
2 parents 0a83777 + 19f2651 commit c626130

File tree

8 files changed

+457
-0
lines changed

8 files changed

+457
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.vscode
3+
.idea

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
"node"

CONTRIBUTING.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Contributing
2+
3+
### Thanks, for considering to contribute to this repository!
4+
5+
When contributing to this repository, please first discuss the change you wish to make via issue,
6+
email, or any other method with the owners of this repository before making a change.
7+
8+
If you are looking forward to start contributing to beautiful world of open source.
9+
**Start it right now.**
10+
11+
The basic guidelines for contributing are as follows:
12+
- Fork the repo
13+
- Clone the repo
14+
- Create a branch using
15+
- `git checkout -b feature-branch`
16+
- Make the required changes
17+
- Test the changes by using below command
18+
- `yarn` (Installs all the dependencies)
19+
- `yarn test`
20+
- Create a pull request using below commands
21+
- `git add --all`
22+
- `git commit -m "your commit message"`
23+
- `git push origin feature-branch`
24+
- Go to [Repository](https://github.com/arshadkazmi42/json-nested-replace/)
25+
- Create Pull Request against `master` branch
26+
- Add a suitable title and description to the pull request and tag the issue number in Pull Request description, if the pull request is related to some issue logged here: [Issues](https://github.com/arshadkazmi42/json-nested-replace/issues)
27+
- You're done. Wait for your code to get reviewed and merged
28+
- Optional: Give us a :star: if you like our work :smile:

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,63 @@
11
# json-nested-replace
2+
3+
[![Build Status](https://travis-ci.com/arshadkazmi42/json-nested-replace.svg?branch=master)](https://travis-ci.com/arshadkazmi42/json-nested-replace)
4+
[![Github Repo Size](https://img.shields.io/github/repo-size/arshadkazmi42/json-nested-replace.svg)](https://github.com/arshadkazmi42/json-nested-replace)
5+
[![Contributors](https://img.shields.io/github/contributors/arshadkazmi42/json-nested-replace.svg)](https://github.com/arshadkazmi42/json-nested-replace/graphs/contributors)
6+
[![Commit](https://img.shields.io/github/last-commit/arshadkazmi42/json-nested-replace.svg)](https://github.com/arshadkazmi42/json-nested-replace/commits/master)
7+
28
Searches and replace values at every level of nested json
9+
10+
> Give us a :star: if you like our work :heart:
11+
12+
<a href="https://www.buymeacoffee.com/arshadkazmi42" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
13+
<a href="https://www.patreon.com/bePatron?u=15454240" target="_blank"><img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Become a Patron!" height="40"></a>
14+
15+
Please consider donating, if you like my work
16+
17+
## Install
18+
19+
```
20+
$ npm install json-nested-replace
21+
```
22+
23+
## Usage
24+
25+
```javascript
26+
const jnestedReplace = require('json-nested-replace');
27+
28+
const INPUT_JSON = {
29+
'name': 'json-nested-replace',
30+
'author': 'Arshad Kazmi',
31+
'repository': {
32+
'url': 'https://github.com/arshadkazmi42/json-nested-replace',
33+
'language': 'js'
34+
}
35+
};
36+
37+
const replacedJSONValue = jnestedReplace(INPUT_JSON, 'json-nested-replace', 'jnested-replace');
38+
console.log(replacedJSONValue);
39+
// Output
40+
/** {
41+
* 'name': 'jnested-replace',
42+
* 'author': 'Arshad Kazmi',
43+
* 'repository': {
44+
* 'url': 'https://github.com/arshadkazmi42/jnested-replace',
45+
* 'language': 'js'
46+
* }
47+
* }
48+
**/
49+
50+
51+
const replacedStringValue = jnestedReplace('json-nested-replace', 'json-nested', 'jnested');
52+
console.log(replacedStringValue)
53+
// Output
54+
// jnested-replace
55+
56+
```
57+
58+
## Contributing
59+
60+
Interested in contributing to this project?
61+
You can log any issues or suggestion related to this library [here](https://github.com/arshadkazmi42/json-nested-replace/issues/new)
62+
63+
Read our contributing [guide](CONTRIBUTING.md) on getting started with contributing to the codebase

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+
});

0 commit comments

Comments
 (0)