Comparing version 0.0.2 to 0.1.1
@@ -0,2 +1,11 @@ | ||
0.1.1 / 2014-05-14 | ||
================== | ||
* Add mocha test for version 0.1.1 | ||
0.1.0 / 2014-05-14 | ||
================== | ||
* Finish full feature, support manipulating nested data | ||
0.0.2 / 2014-03-06 | ||
@@ -3,0 +12,0 @@ ================== |
@@ -13,2 +13,20 @@ /** | ||
/** | ||
* Object#toString() | ||
*/ | ||
var toString = Object.prototype.toString; | ||
/** | ||
* Array#isArray shim | ||
*/ | ||
var isArray = Array.isArray || function (arr) { | ||
return toString.call(arr) == '[object Array]'; | ||
} | ||
/** | ||
* isNumber shim | ||
*/ | ||
var isNumber = function (n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
} | ||
/** | ||
* Initialize a new `EasyJSON`. | ||
@@ -57,22 +75,105 @@ * | ||
/** | ||
* Add the item to JSON | ||
* @param {String} key key of added item | ||
* @param {String} val value of added item | ||
* @param {String} under add item under specific location | ||
* @return {EasyJSON} | ||
* add datas to json file, support nested data | ||
* @param {[String]} str | ||
* @param {[Object]} value | ||
* @param {[Number]} index | ||
* @return{EasyJSON} [description] | ||
*/ | ||
EasyJSON.prototype.add = function (key, val, under) { | ||
EasyJSON.prototype.add = function (str, value, index) { | ||
var chunk = this.read(), | ||
obj = JSON.parse(chunk); | ||
if (typeof under === 'undefined') { | ||
obj[key] = val; | ||
this.write(obj); | ||
obj = JSON.parse(chunk), | ||
nest = this.getNested(str), | ||
pre = this.getPre(str)[0], | ||
ret, | ||
gene, | ||
exist; | ||
if (typeof this.get(str) === 'undefined') { | ||
if (nest.length === 0) { | ||
obj[str] = value; | ||
} else if (nest.length === 1) { | ||
if (typeof this.get(pre) === 'undefined') { | ||
isNumber(nest[0][0]) ? obj[pre] = [value] : (obj[pre] = {}, obj[pre][nest[0][0]] = value); | ||
} else { | ||
(isArray(this.get(pre))) ? obj[pre].splice(nest[0][0], 0, value) : obj[pre][nest[0][0]] = value; | ||
} | ||
} else { | ||
if (typeof this.get(pre) === 'undefined') { | ||
isNumber(nest[nest.length - 1][0]) ? ret = [value] : (ret = {}, ret[nest[nest.length - 1][0]] = value); | ||
for (var i = nest.length - 2; i >= 0; --i) { | ||
if (isNumber(nest[i][0])) { | ||
gene = []; | ||
gene.splice(nest[i][0], 0, ret); | ||
ret = gene; | ||
} else { | ||
gene = {}; | ||
gene[nest[i][0]] = ret; | ||
ret = gene; | ||
} | ||
} | ||
obj[pre] = ret; | ||
} else { | ||
for (var i = nest.length - 2; i >= 0; --i) { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
if (typeof this.get(str) !== 'undefined') { | ||
exist = i; | ||
break; | ||
} else { | ||
continue; | ||
} | ||
} | ||
if (typeof exist === 'undefined') exist = -1; | ||
ret = value; | ||
for (var i = nest.length - 1; i > exist + 1; --i) { | ||
if (isNumber(nest[i][0])) { | ||
gene = []; | ||
gene.splice(nest[i][0], 0, ret); | ||
ret = gene; | ||
} else { | ||
gene = {}; | ||
gene[nest[i][0]] = ret; | ||
ret = gene; | ||
} | ||
} | ||
e_ret = this.get(str); | ||
if (typeof e_ret === 'undefined') { | ||
obj[pre][nest[0][0]] = ret; | ||
} else { | ||
isArray(e_ret) ? e_ret.splice(nest[exist + 1][0], 0, ret) : e_ret[nest[exist + 1][0]] = ret; | ||
for (var i = exist; i >= 0; --i) { | ||
// aaa[bbb][ccc] => aaa[bbb] | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
ret = this.get(str); | ||
ret[nest[i][0]] = e_ret; | ||
e_ret = ret; | ||
} | ||
obj[pre] = e_ret; | ||
} | ||
} | ||
} | ||
} else if (isArray(this.get(str))) { // push the value to array | ||
if (nest.length === 0) { | ||
(typeof index === 'undefined') ? obj[pre].push(value) : obj[pre].splice(index, 0, value); | ||
} else if (nest.length === 1) { | ||
parse = this.get(pre); | ||
(typeof index === 'undefined') ? parse[nest[0][0]].push(value) : parse[nest[0][0]].splice(index, 0, value); | ||
obj[pre] = parse; | ||
} else { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
parse = this.get(str); | ||
(typeof index === 'undefined') ? parse[nest[nest.length - 1][0]].push(value) : parse[nest[nest.length - 1][0]].splice(index, 0, value); | ||
for (var i = nest.length - 2; i >= 0; --i) { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
ret = this.get(str); | ||
ret[nest[i][0]] = parse; | ||
parse = ret; | ||
} | ||
obj[pre] = parse; | ||
} | ||
} else { | ||
if (typeof obj[under] === 'object') { | ||
obj[under][key] = val; | ||
this.write(obj); | ||
} | ||
console.log('key has already exist!') | ||
} | ||
this.write(obj); | ||
return this; | ||
@@ -82,22 +183,55 @@ } | ||
/** | ||
* Delete the specific JSON item | ||
* Delete the specific JSON item,support nested data | ||
* @param {String} keyToDel | ||
* @return {EasyJSON} | ||
*/ | ||
EasyJSON.prototype.del = function (keyToDel) { | ||
EasyJSON.prototype.del = function (str) { | ||
var chunk = this.read(), | ||
obj; | ||
obj = JSON.parse(chunk), | ||
nest = this.getNested(str), | ||
pre = this.getPre(str)[0], | ||
ret; | ||
obj = JSON.parse(chunk, function (key, val) { | ||
if (key === keyToDel) { | ||
return undefined; | ||
if (nest.length === 0) { | ||
obj = JSON.parse(chunk, function (key, val) { | ||
if (key === str) { | ||
return undefined; | ||
} | ||
return val; | ||
}); | ||
} else if (nest.length === 1) { | ||
parse = this.get(pre); | ||
if (isArray(parse)) { | ||
parse.splice(nest[0][0], 1); | ||
} else { | ||
delete parse[nest[0][0]] | ||
} | ||
return val; | ||
}); | ||
obj[pre] = parse; | ||
} else { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
parse = this.get(str); | ||
if (isArray(parse)) { | ||
parse.splice(nest[nest.length - 1][0], 1); | ||
} else { | ||
delete parse[nest[nest.length - 1][0]] | ||
} | ||
for (var i = nest.length - 2; i >= 0; --i) { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
ret = this.get(str); | ||
ret[nest[i][0]] = parse; | ||
parse = ret; | ||
} | ||
obj[pre] = parse; | ||
} | ||
this.write(obj); | ||
return this; | ||
} | ||
/** | ||
* Modify the value of specific JSON item | ||
* Modify the value of specific JSON item, support nested data | ||
* @param {String} keyToChange | ||
@@ -107,12 +241,33 @@ * @param {String} valToChange | ||
*/ | ||
EasyJSON.prototype.modify = function (keyToChange, valToChange) { | ||
EasyJSON.prototype.modify = function (str, value) { | ||
var chunk = this.read(), | ||
obj; | ||
obj = JSON.parse(chunk), | ||
nest = this.getNested(str), | ||
pre = this.getPre(str)[0], | ||
ret; | ||
obj = JSON.parse(chunk, function (key, val) { | ||
if (key === keyToChange) { | ||
return valToChange; | ||
if (nest.length === 0) { | ||
obj = JSON.parse(chunk, function (key, val) { | ||
if (key === str) { | ||
return value; | ||
} | ||
return val; | ||
}); | ||
} else if (nest.length === 1) { | ||
parse = this.get(pre); | ||
parse[nest[0][0]] = value; | ||
obj[pre] = parse; | ||
} else { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
parse = this.get(str); | ||
parse[nest[nest.length - 1][0]] = value | ||
for (var i = nest.length - 2; i >= 0; --i) { | ||
str = str.replace(/\[[a-zA-Z_$][a-zA-Z0-9_$]*\]$|\[0\]$|\[[1-9]+[0-9]*\]$/,'') | ||
ret = this.get(str); | ||
ret[nest[i][0]] = parse; | ||
parse = ret; | ||
} | ||
return val; | ||
}); | ||
obj[pre] = parse; | ||
} | ||
this.write(obj); | ||
@@ -134,19 +289,27 @@ return this; | ||
* Get the value of specific JSON item | ||
* @param {String} keyToGet | ||
* @param {String} under get item under specific location | ||
* @return {String} return EasyJSON if get failed | ||
* @param {String} str | ||
* @return {String} | ||
*/ | ||
EasyJSON.prototype.get = function (keyToGet, under) { | ||
EasyJSON.prototype.get = function (str) { | ||
var chunk = this.read(), | ||
obj = JSON.parse(chunk); | ||
if (typeof under === 'undefined') { | ||
return obj[keyToGet]; | ||
var nest = this.getNested(str); | ||
var pre = this.getPre(str)[0]; | ||
var ret; | ||
if (nest.length === 0) { | ||
return obj[str]; | ||
} else { | ||
if (typeof obj[under] === 'object') { | ||
return obj[under][keyToGet]; | ||
ret = obj[pre]; | ||
for (var i = 0; i < nest.length; ++i) { | ||
if (typeof ret === 'undefined') { | ||
return undefined; | ||
} else if (i != nest.length - 1) { | ||
ret = ret[nest[i][0]]; | ||
} else { | ||
return ret[nest[i][0]]; | ||
} | ||
} | ||
} | ||
return this; | ||
} | ||
@@ -161,2 +324,31 @@ | ||
return JSON.stringify(obj, null, 4) | ||
} | ||
/** | ||
* Get the nested data. aaa[bbb][ccc] => [['bbb',index:3,input:'aaa[bbb][ccc]'],['ccc',index:8,input:'aaa[bbb][ccc]']] | ||
* @param {[String]} str nested data | ||
* @return {[Array]} | ||
*/ | ||
EasyJSON.prototype.getNested = function (str) { | ||
var reg = /\[[a-zA-Z_$][a-zA-Z0-9_$]*\]|0|[1-9]+[0-9]*/g; | ||
var brace = []; | ||
var ret; | ||
while ((ret = reg.exec(str)) !== null) { | ||
if (/\[/.test(ret[0])) { | ||
ret[0] = ret[0].substr(1, ret[0].length - 2); | ||
} | ||
brace.push(ret); | ||
} | ||
return brace; | ||
} | ||
/** | ||
* Get the prefix of nested data. aaa[bbb][ccc] => aaa | ||
* @param {[String]} str | ||
* @return {[Array]} | ||
*/ | ||
EasyJSON.prototype.getPre = function (str) { | ||
var reg = /^[a-zA-Z_$][a-zA-Z0-9_$]*/; | ||
var ret = reg.exec(str); | ||
return ret; | ||
} |
{ | ||
"name": "easyjson", | ||
"version": "0.0.2", | ||
"description": "An easy way to manipulate JSON file with add/delete/modify/get json data.", | ||
"version": "0.1.1", | ||
"description": "An easy way to manipulate JSON file with add/delete/modify/get json data, support nested data.", | ||
"main": "index.js", | ||
@@ -17,2 +17,6 @@ "scripts": { | ||
], | ||
"devDependencies": { | ||
"should": "3.1.4", | ||
"mocha": ">= 1.13.0 < 2" | ||
}, | ||
"author": { | ||
@@ -19,0 +23,0 @@ "name": "Tinple", |
# easyjson | ||
tiny node.js module to manipulate JSON file with add/delete/modify/get json data easily. | ||
Tiny node.js module to manipulate JSON file with add/delete/modify/get json data easily. Also support nested datas. | ||
@@ -11,7 +11,6 @@ ## Installation | ||
##EasyJSON | ||
## EasyJSON | ||
With `EasyJSON` you can simply invoke the exported function to manipulate JSON file. | ||
At first, passing a path to `path` function to choose JSON file. And `EasyJSON` support | ||
the chain call. | ||
At first, passing a path to `path` function to choose a JSON file. And `EasyJSON` support the chain call. | ||
@@ -23,7 +22,8 @@ Example app.js: | ||
// it should output your JSON file | ||
easyjson.path('test') | ||
.express(); | ||
.express(); | ||
``` | ||
You can also add, modify or delete a item with your JSON file. | ||
You can also add, modify or delete a item with your JSON file. And `EasyJSON` support chained invoke. | ||
@@ -34,11 +34,10 @@ ```js | ||
easyjson.path('test') | ||
.add('license', 'MIT') | ||
.express() | ||
//specify blog is under author which is a object | ||
.add('blog', 'http://tinple.me', 'author') | ||
.express() | ||
.del('version') | ||
.express() | ||
.modify('project', 'EasyJSON') | ||
.express(); | ||
.add('license', 'MIT') | ||
.express() | ||
.add('blog', 'http://tinple.me') | ||
.express() | ||
.del('version') | ||
.express() | ||
.modify('project', 'EasyJSON') | ||
.express(); | ||
``` | ||
@@ -50,8 +49,72 @@ | ||
var easyjson = require('easyjson'), | ||
author = easyjson.path('test').get('author'), | ||
email = easyjson.path('test').get('email'); | ||
author = easyjson.path('test').get('author'), | ||
email = easyjson.path('test').get('email'); | ||
``` | ||
## Most Useful(support nested data) | ||
Actually, `EasyJSON` support nested data, that means you | ||
can manipulate your JSON file most effectivly. | ||
```js | ||
var easyjson = require('easyjson').path('test'); | ||
/** | ||
* "author": { | ||
* "name": "Tinple" | ||
* } | ||
* ==> | ||
* "author": { | ||
* "name": "Tinple", | ||
* "friend": { | ||
* "name": "Kristine" | ||
* } | ||
* } | ||
*/ | ||
easyjson.add('author[friend][name]', 'Kristine'); | ||
/** | ||
* push 'path.json' to existed array files | ||
* "files": ["index.js"] | ||
* ==> | ||
* "files": ["index.js", "path.json"] | ||
*/ | ||
easyjson.add('files', 'path.json'); | ||
// it will delete whole friend object, the same as array | ||
easyjson.del('author[friend]'); | ||
// modify the array, only index support | ||
easyjson.modify('files[1]', 'path2.json'); | ||
// modify the obj key | ||
easyjson.modify('author[friend][name]', 'Panda'); | ||
/** More complex | ||
* "author": { | ||
* "name": "Tinple" | ||
* } | ||
* ==> | ||
* "author": { | ||
* "name": "Tinple", | ||
* "friend": [{ | ||
* "name": "Kristine" | ||
* }] | ||
* } | ||
*/ | ||
easyjson.add('author[friend][0][name]', 'Kristine'); | ||
``` | ||
## Warning | ||
`EasyJSON` will do nothing when you pass a nonexistent | ||
key to `modify()` and `del()`. As for `add(key, value)`, `EasyJSON` will judge whether the key exists firstly. If not, it will added normally. | ||
If it has already existed, there are two situations that | ||
the key should be treated. | ||
1. The key is an array, then the value will be pushed. | ||
2. The key is other type, like Object or String, then `EasyJSON` do nothing. | ||
## License | ||
MIT |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15456
392
116
2
10
1