Comparing version 0.0.6 to 0.1.0
@@ -1,7 +0,1 @@ | ||
/* | ||
* CJSON - comments enabled json config loader (Commented Javascript Object Notation) | ||
* | ||
* @author Oleg Slobodskoi | ||
*/ | ||
var fs = require('fs'), | ||
@@ -14,8 +8,8 @@ Path = require('path'); | ||
* | ||
* @param {string} str json file contents. | ||
* @return {string} newStr cleaned json. | ||
* @param {String} json file. | ||
* @return {String} json without comments. | ||
*/ | ||
exports.decomment = function(str) { | ||
var i, | ||
curChar, nextChar, | ||
curChar, nextChar, | ||
inString = false, | ||
@@ -62,4 +56,4 @@ inComment = false, | ||
} | ||
return newStr; | ||
@@ -70,4 +64,6 @@ }; | ||
* Decomment the string and parse json. | ||
* @param {string} str json string. | ||
* @param {=Function} optional will be called for every key and value at every level of the final result. | ||
* | ||
* @param {String} json. | ||
* @param {Function} [reviver] will be called for every key and value at every | ||
* level of the final result. | ||
* @return {Object} parsed json object. | ||
@@ -81,16 +77,17 @@ */ | ||
* Replace templates with data. {{toReplace}} | ||
* @param {string} str json string. | ||
* | ||
* @param {String} json. | ||
* @param {Object} data data hash. | ||
* @return {string} json string with replaced data. | ||
* @return {String} json string with replaced data. | ||
*/ | ||
exports.replace = function(str, data) { | ||
return str.replace(/\{\{([^}]+)\}\}/g, function(match, search){ | ||
return str.replace(/\{\{([^}]+)\}\}/g, function(match, search) { | ||
return data[search] ? data[search] : match; | ||
}) | ||
}); | ||
}; | ||
/** | ||
* Merge objects to the first one | ||
* @param {boolean|Object} deep if set true, deep merge will be done. | ||
* | ||
* @param {Boolean|Object} deep if set true, deep merge will be done. | ||
* @param {Object} obj1 any object. | ||
@@ -101,3 +98,2 @@ * @param {Object} obj2 any object. | ||
exports.extend = (function() { | ||
var toString = Object.prototype.toString, | ||
@@ -122,2 +118,5 @@ obj = '[object Object]'; | ||
// create a copy of target object to avoid subobjects changes | ||
target[key] = extend(deep, {}, target[key]); | ||
extend(deep, target[key], args[i][key]); | ||
@@ -134,7 +133,25 @@ } else { | ||
/** | ||
* Freeze the object recursively. | ||
* | ||
* @param {Object} obj. | ||
* @return {Object} | ||
*/ | ||
exports.freeze = function freeze(obj) { | ||
var key; | ||
if (obj instanceof Object) { | ||
for (key in obj) { | ||
freeze(obj[key]); | ||
} | ||
Object.freeze(obj); | ||
} | ||
}; | ||
/** | ||
* Load and parse a config file/files. | ||
* @param {string|Array} path absolute path/paths to the file/files or dir. | ||
* @param {Object|boolean=} options if true, extend all jsons to the first one, | ||
* | ||
* @param {String|Array} path absolute path/paths to the file/files or dir. | ||
* @param {Object|Boolean} [options] if true, extend all jsons to the first one, | ||
* it can be also object {merge: true, replace: {key: 'value'}} | ||
@@ -145,5 +162,5 @@ * @return {Object} conf parsed json object. | ||
var data, paths, conf; | ||
options = options || {}; | ||
options || (options = {}); | ||
if (options === true) { | ||
@@ -168,3 +185,5 @@ options = {merge: true}; | ||
return conf; | ||
} else if (fs.statSync(path).isDirectory()) { | ||
} | ||
if (fs.statSync(path).isDirectory()) { | ||
paths = []; | ||
@@ -180,16 +199,19 @@ fs.readdirSync(path).forEach(function(filename) { | ||
data = fs.readFileSync(path, 'utf8'); | ||
data = fs.readFileSync(path, 'utf-8'); | ||
if (options.replace) { | ||
data = exports.replace(data, options.replace); | ||
} | ||
data = exports.decomment(data); | ||
try { | ||
return JSON.parse(data); | ||
data = exports.parse(data); | ||
} catch(e) { | ||
throw new Error('JSON.parse error - "' + e.message + '"\nFile: "' + path + '"\n'); | ||
} | ||
if (options.freeze) { | ||
exports.freeze(data); | ||
} | ||
return data; | ||
}; |
{ | ||
"name": "cjson", | ||
"description": "cjson - Commented Javascript Object Notation. It is a json loader, which parses only valide json files, but with comments enabled. Usefull for loading configs.", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"repository": "git://github.com/kof/node-cjson.git", | ||
@@ -12,7 +12,7 @@ "keywords": [ "json", "parser", "comments", "config", "loader"], | ||
"licenses": [ | ||
{ | ||
{ | ||
"type": "MIT", | ||
"url" : "http://www.opensource.org/licenses/mit-license.php" | ||
} | ||
] | ||
} | ||
] | ||
} |
@@ -1,2 +0,3 @@ | ||
## CJSON (Commented Javascript Object Notation) is a comments enabled json config loader. | ||
[![build status](https://secure.travis-ci.org/kof/node-cjson.png)](http://travis-ci.org/kof/node-cjson) | ||
## CJSON (Commented Javascript Object Notation) is a comments enabled json config loader. | ||
@@ -14,6 +15,6 @@ JSON has a good spec, is implemented in every language, has easy to read syntax and is much more powerfull then ini files. | ||
Example of such shiny config file: | ||
/* | ||
* This is my app configuration file. | ||
* | ||
* | ||
*/ | ||
@@ -35,5 +36,5 @@ { | ||
Load config file from given path, array of paths or directory. Second parameter is optional and can be a boolean or object. | ||
- `path` {string} absolute path to the file | ||
- `options` {boolean|Object} optional options | ||
- `options` {boolean|Object} optional options | ||
@@ -43,3 +44,4 @@ `options` defaults: | ||
merge: false, | ||
replace: null | ||
replace: null, | ||
freeze: false | ||
} | ||
@@ -49,11 +51,12 @@ | ||
`replace` allows you to do some string replacements, see `cjson.replace`. | ||
`freeze` - freeze config recursively, see `cjson.freeze` | ||
Examples: | ||
// just one config | ||
// just one config | ||
var conf = cjson.load('/path/to/your/config.json'); | ||
// array of configs | ||
// array of configs | ||
var conf = cjson.load(['/path/to/your/config1.json', '/path/to/your/config2.json']); | ||
//output | ||
@@ -64,8 +67,8 @@ { | ||
} | ||
// use optional merge parameter | ||
// array of configs | ||
// array of configs | ||
var conf = cjson.load(['/path/to/your/config1.json', '/path/to/your/config2.json'], true); | ||
// output | ||
@@ -76,7 +79,7 @@ { | ||
} | ||
// load all config files from a directory | ||
var conf = cjson.load('/path/to/your/configs'); | ||
// overwriting dev config with production | ||
@@ -91,3 +94,3 @@ var paths = ['/path/to/conf.json']; | ||
Merge the contents of two or more objects together into the first object. | ||
Merge the contents of two or more objects together into the first object. | ||
@@ -105,3 +108,3 @@ - `deep` If true, the merge becomes recursive. | ||
Remove javascript style comments, singleline - '//' and multiline - '/**/'. It takes care | ||
Remove javascript style comments, singleline - '//' and multiline - '/**/'. It takes care | ||
about comments inside of strings and escaping. | ||
@@ -111,4 +114,4 @@ | ||
Like `JSON.parse`, but it takes care about comments. Optional `reviver` argument | ||
is for `JSON.parse` method and will be called for every key and value at every level | ||
Like `JSON.parse`, but it takes care about comments. Optional `reviver` argument | ||
is for `JSON.parse` method and will be called for every key and value at every level | ||
of the final result | ||
@@ -118,3 +121,3 @@ | ||
Replace all strings `{{key}}` contained in `{key: 'value'}`, where `key` can be any | ||
Replace all strings `{{key}}` contained in `{key: 'value'}`, where `key` can be any | ||
property of passed `obj`. | ||
@@ -124,6 +127,10 @@ | ||
var str = '{"path": "{{root}}/src"}'; // json file contents | ||
cjson.replace(str, {root: '/usr'}); // '{"path": "/usr/src"}' | ||
cjson.replace(str, {root: '/usr'}); // '{"path": "/usr/src"}' | ||
### cjson.freeze(obj) | ||
Recursively freeze an object. | ||
## Installation | ||
npm install cjson | ||
npm install cjson |
@@ -62,3 +62,31 @@ var a = require('assert'); | ||
(function extend() { | ||
a.deepEqual(cjson.extend({test1: 1}, {test2: 2}), {test1: 1, test2: 2}, 'extend 2 simple objects'); | ||
a.deepEqual(cjson.extend({test1: 1}, {test2: 2}, {test3: 3}), {test1: 1, test2: 2, test3: 3}, 'extend 3 simple objects'); | ||
a.deepEqual(cjson.extend({test1: 1}, true), {test1: 1}, '2 arg is not an object'); | ||
a.deepEqual(cjson.extend( true, {test1: {test1: 1}}, {test1: {test2: 2} } ), { test1: {test1: 1, test2: 2} }, 'deep extend' ); | ||
a.deepEqual(cjson.extend( true, {test: {test: 'test'}}, {test: {test: 'test'} } ), {test: {test: 'test'} }, 'deep extend, check endless lop' ); | ||
var data1 = {a: {b: 1}}, | ||
data2 = {a: {b: 2}}; | ||
cjson.extend(true, {}, data1, data2); | ||
a.notDeepEqual(data1, data2, 'original deep object is not mangled'); | ||
}()); | ||
(function freeze() { | ||
var data1 = {a: {b: 1}}, | ||
data2 = {a: {b: 1}}; | ||
cjson.freeze(data1); | ||
data1.abc = 123; | ||
data1.a = 123; | ||
a.deepEqual(data1, data2, 'data1 wasn\'t changed'); | ||
data1 = cjson.load(fixtures + '/conf1.json', {freeze: true}), | ||
data2 = cjson.load(fixtures + '/conf1.json', {freeze: true}); | ||
data1.abc = 123; | ||
data1.a = 123; | ||
a.deepEqual(data1, data2, 'data1 wasn\'t changed'); | ||
}()) | ||
console.log('All tests passed.'); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13355
15
273
126
0