Skip to content

Commit

Permalink
closes #6 fixed extending subobjects if using deep=true
Browse files Browse the repository at this point in the history
  • Loading branch information
kof committed Sep 20, 2012
1 parent 1865827 commit d3eca92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/cjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var fs = require('fs'),
*/
exports.decomment = function(str) {
var i,
curChar, nextChar,
curChar, nextChar,
inString = false,
inComment = false,
newStr = '';
Expand Down Expand Up @@ -58,7 +58,7 @@ exports.decomment = function(str) {

newStr += curChar;
}


return newStr;
};
Expand Down Expand Up @@ -94,7 +94,6 @@ exports.replace = function(str, data) {
* @return {Object} target merged object.
*/
exports.extend = (function() {

var toString = Object.prototype.toString,
obj = '[object Object]';

Expand All @@ -103,7 +102,8 @@ exports.extend = (function() {
var args = arguments,
i = deep === true ? 1 : 0,
key,
target = args[i];
target = args[i],
shallowCopy;

for (++i; i < args.length; ++i) {
for (key in args[i]) {
Expand All @@ -114,6 +114,9 @@ exports.extend = (function() {
toString.call(args[i][key]) === obj &&
toString.call(target[key]) === obj) {

// create a copy of target object to avoid subobjects changes
target[key] = extend(deep, {}, target[key]);

extend(deep, target[key], args[i][key]);
} else {
target[key] = args[i][key];
Expand All @@ -129,15 +132,15 @@ exports.extend = (function() {
/**
* 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 {Object|boolean=} options if true, extend all jsons to the first one,
* it can be also object {merge: true, replace: {key: 'value'}}
* @return {Object} conf parsed json object.
*/
exports.load = function load(path, options) {
var data, paths, conf;

options = options || {};

if (options === true) {
options = {merge: true};
}
Expand Down Expand Up @@ -169,17 +172,17 @@ exports.load = function load(path, options) {
}

data = fs.readFileSync(path, 'utf8');

if (options.replace) {
data = exports.replace(data, options.replace);
}

data = exports.decomment(data);

try {
return JSON.parse(data);
} catch(e) {
throw new Error('JSON.parse error - "' + e.message + '"\nFile: "' + path + '"\n');
}

};
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,15 @@ var str = require('fs').readFileSync(fixtures + '/conf2.json').toString();

a.deepEqual(cjson.parse(str), data.conf2, '.parse method with comments');

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 one = {a: {b: 1}},
two = {a: {b: 2}};
cjson.extend(true, {}, one, two);
a.notDeepEqual(one, two, 'original deep object is not mangled');

console.log('All tests passed.');

0 comments on commit d3eca92

Please sign in to comment.