Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b26afd

Browse files
author
andrew85
committed
Add test codes
1 parent d6280a3 commit 2b26afd

File tree

7 files changed

+620
-3
lines changed

7 files changed

+620
-3
lines changed

‎.travis.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- '10'
4+
script: npm run test

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# json-knife [![NPM version](https://img.shields.io/npm/v/json-knife.svg)](https://www.npmjs.com/package/json-knife) [![](https://data.jsdelivr.com/v1/package/gh/Andrew-Kang-G/json-knife/badge)](https://www.jsdelivr.com/package/gh/Andrew-Kang-G/json-knife)
22
## Overview
3-
Mass update certain key-values recursively in huge, complex and deep JSON string
3+
Mass replace specific properties value with a new value recursively in huge, complex and deep JSON string
44
with a robust regular expression based engine.
55

66
Zero dependency, 7KB

‎gulpfile.babel.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gulp.task('babel', () => {
1616

1717
gulp.task('test', ['babel'], () => {
1818
return gulp.src('test/*.js')
19-
.pipe(mocha())
19+
.pipe(mocha({reporter: 'list'}))
2020
.on('error', () => {
2121
gulp.emit('end');
2222
});

‎public/index.html‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,99 @@
2020
<script src="sample.js"></script>
2121
<script type="text/javascript">
2222

23+
let doc = {
24+
"_id" : "5805dfa519f972b200ea2955",
25+
"s" : {
26+
"id" : 36435
27+
},
28+
"a" : [
29+
{
30+
"XX-(random value)" : {
31+
"V" : 4
32+
},
33+
"V" : 4,
34+
"u" : {
35+
"YY-(random value)" : {
36+
"V" : 4,
37+
"ZZ-(random value)" : {
38+
"V" : 4,
39+
"WW-(random value)" : {
40+
"V" : 4
41+
}
42+
}
43+
}
44+
}
45+
}
46+
]
47+
};
48+
49+
function checkProperties(doc, key, value) {
50+
for (var propertyName in doc) {
51+
//console.log(propertyName);
52+
if (typeof doc[propertyName] == "object") {
53+
doc[propertyName] = checkProperties(doc[propertyName], key, value);
54+
} else {
55+
if (propertyName == key) {
56+
doc[propertyName] = value;
57+
//console.log(doc[propertyName]);
58+
}
59+
}
60+
}
61+
62+
return doc;
63+
64+
}
65+
66+
function changeValue(obj, key ,value) {
67+
if (typeof obj === 'object') {
68+
// iterating over the object using for..in
69+
for (var keys in obj) {
70+
//checking if the current value is an object itself
71+
if (typeof obj[keys] === 'object') {
72+
// if so then again calling the same function
73+
changeValue(obj[keys], key ,value)
74+
} else {
75+
76+
if (keys == key) {
77+
obj[keys] = value
78+
console.log(keys);
79+
//doc[propertyName] = value;
80+
//console.log(doc[propertyName]);
81+
}
82+
// else getting the value and replacing single { with {{ and so on
83+
/* let keyValue = obj[keys].replace('{', '{{').replace('}', '}}');
84+
obj[keys] = keyValue;*/
85+
}
86+
}
87+
}
88+
return obj;
89+
}
90+
91+
function getObjects(obj, key, newVal) {
92+
var newValue = newVal;
93+
var objects = [];
94+
for (var i in obj) {
95+
if (!obj.hasOwnProperty(i)) continue;
96+
if (typeof obj[i] == 'object') {
97+
objects = objects.concat(getObjects(obj[i], key, newValue));
98+
} else if (i == key) {
99+
obj[key] = newValue;
100+
}
101+
}
102+
return obj;
103+
}
23104

24105
let src = JSON.stringify(JSON.parse(JSON.stringify(original2)));
25106

26107
console.log('Test if original2 is a correct JSON');
27108
console.log(src);
28109

29-
let re1 = Pattern.sculptJson(src, 'self', null);
110+
/* let re0 = getObjects(original2, 'Mike {[Gentleman]}', 0);
111+
console.log('0 Bulk update - Mike {[Gentleman]} : null');
112+
console.log(JSON.stringify(re0));*/
113+
114+
115+
let re1 = Pattern.sculptJson(src, 'Mike {[Gentleman]}', null);
30116
console.log('Bulk update - Mike {[Gentleman]} : null');
31117
console.log(re1);
32118
JSON.parse(re1)

‎test/answer.js‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /