hoek
Advanced tools
Comparing version 0.7.2 to 0.7.3
// Load modules | ||
var Fs = require('fs'); | ||
var Escape = require('./escape'); | ||
@@ -13,3 +14,3 @@ | ||
exports.clone = function (obj) { | ||
exports.clone = function (obj, seen) { | ||
@@ -26,4 +27,14 @@ if (obj === null || | ||
seen = seen || { orig: [], copy: [] }; | ||
var lookup = seen.orig.indexOf(obj); | ||
if (lookup !== -1) { | ||
return seen.copy[lookup]; | ||
} | ||
var newObj = (obj instanceof Array) ? [] : {}; | ||
seen.orig.push(obj); | ||
seen.copy.push(newObj); | ||
for (var i in obj) { | ||
@@ -39,3 +50,3 @@ if (obj.hasOwnProperty(i)) { | ||
else { | ||
newObj[i] = exports.clone(obj[i]); | ||
newObj[i] = exports.clone(obj[i], seen); | ||
} | ||
@@ -284,2 +295,26 @@ } | ||
exports.formatStack = function (stack) { | ||
var trace = []; | ||
stack.forEach(function (item) { | ||
trace.push([item.getFileName(), item.getLineNumber(), item.getColumnNumber(), item.getFunctionName(), item.isConstructor()]); | ||
}); | ||
return trace; | ||
}; | ||
exports.formatTrace = function (trace) { | ||
var display = []; | ||
trace.forEach(function (row) { | ||
display.push((row[4] ? 'new ' : '') + row[3] + ' (' + row[0] + ':' + row[1] + ':' + row[2] + ')'); | ||
}); | ||
return display; | ||
}; | ||
exports.callStack = function (slice) { | ||
@@ -301,8 +336,4 @@ | ||
var trace = []; | ||
stack.forEach(function (item) { | ||
var trace = exports.formatStack(stack); | ||
trace.push([item.getFileName(), item.getLineNumber(), item.getColumnNumber(), item.getFunctionName(), item.isConstructor()]); | ||
}); | ||
if (slice) { | ||
@@ -319,9 +350,4 @@ return trace.slice(slice); | ||
var trace = exports.callStack(slice === undefined ? 1 : slice + 1); | ||
var display = []; | ||
trace.forEach(function (row) { | ||
display.push((row[4] ? 'new ' : '') + row[3] + ' (' + row[0] + ':' + row[1] + ':' + row[2] + ')'); | ||
}); | ||
return display; | ||
return exports.formatTrace(trace); | ||
}; | ||
@@ -339,3 +365,6 @@ | ||
var stack = hideStack ? '' : exports.displayStack(1).join('\n\t'); | ||
var stack = ''; | ||
if (!hideStack) { | ||
stack = exports.displayStack(1).join('\n\t'); | ||
} | ||
console.log('ABORT: ' + message + '\n\t' + stack); | ||
@@ -483,27 +512,11 @@ process.exit(1); | ||
// Escape string for inclusion in HTML | ||
exports.escapeHtml = function (string) { | ||
internals.htmlEscaped = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''', | ||
'`': '`' | ||
return Escape.escapeHtml(string); | ||
}; | ||
exports.escapeHtml = function (string) { | ||
if (!string) { | ||
return ''; | ||
} | ||
exports.escapeJavaScript = function (string) { | ||
if (/[&<>"'`]/.test(string) === false) { | ||
return string; | ||
} | ||
return string.replace(/[&<>"'`]/g, function (chr) { | ||
return internals.htmlEscaped[chr]; | ||
}); | ||
return Escape.escapeJavaScript(string); | ||
}; | ||
@@ -510,0 +523,0 @@ |
{ | ||
"name": "hoek", | ||
"description": "General purpose node utilities", | ||
"version": "0.7.2", | ||
"version": "0.7.3", | ||
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "contributors":[ |
@@ -24,2 +24,3 @@ // Load modules | ||
var nestedObj = { | ||
v: [7,8,9], | ||
w: /^something$/igm, | ||
@@ -59,2 +60,120 @@ x: { | ||
}); | ||
it('should not throw on circular reference', function (done) { | ||
var a = {}; | ||
a.x = a; | ||
var test = (function () { | ||
var b = Hoek.clone(a); | ||
}); | ||
expect(test).to.not.throw(); | ||
done(); | ||
}); | ||
it('should properly clone circular reference', function (done) { | ||
var x = { | ||
'z': new Date() | ||
}; | ||
x.y = x; | ||
var b = Hoek.clone(x); | ||
expect(Object.keys(b.y)).to.deep.equal(Object.keys(x)) | ||
expect(b.z).to.not.equal(x.z); | ||
expect(b.y).to.not.equal(x.y); | ||
expect(b.y.z).to.not.equal(x.y.z); | ||
expect(b.y).to.equal(b); | ||
expect(b.y.y.y.y).to.equal(b); | ||
done(); | ||
}); | ||
it('should properly clone deeply nested object', function (done) { | ||
var a = { | ||
x: { | ||
y: { | ||
a: [1, 2, 3], | ||
b: 123456, | ||
c: new Date(), | ||
d: /hi/igm, | ||
e: /hello/ | ||
}, | ||
} | ||
}; | ||
var b = Hoek.clone(a); | ||
expect(a).to.deep.equal(b); | ||
expect(a.x.y.c.getTime()).to.equal(b.x.y.c.getTime()); | ||
done(); | ||
}); | ||
it('should properly clone arrays', function (done) { | ||
var a = [1,2,3]; | ||
var b = Hoek.clone(a); | ||
expect(a).to.deep.equal(b); | ||
done(); | ||
}); | ||
it('should perform actual copy for shallow keys (no pass by reference)', function (done) { | ||
var x = Hoek.clone(nestedObj); | ||
var y = Hoek.clone(nestedObj); | ||
// Date | ||
expect(x.z).to.not.equal(nestedObj.z); | ||
expect(x.z).to.not.equal(y.z); | ||
// Regex | ||
expect(x.w).to.not.equal(nestedObj.w); | ||
expect(x.w).to.not.equal(y.w); | ||
// Array | ||
expect(x.v).to.not.equal(nestedObj.v); | ||
expect(x.v).to.not.equal(y.v); | ||
// Immutable(s) | ||
x.y = 5; | ||
expect(x.y).to.not.equal(nestedObj.y); | ||
expect(x.y).to.not.equal(y.y); | ||
done(); | ||
}); | ||
it('should perform actual copy for deep keys (no pass by reference)', function (done) { | ||
var x = Hoek.clone(nestedObj); | ||
var y = Hoek.clone(nestedObj); | ||
expect(x.x.c).to.not.equal(nestedObj.x.c); | ||
expect(x.x.c).to.not.equal(y.x.c); | ||
expect(x.x.c.getTime()).to.equal(nestedObj.x.c.getTime()); | ||
expect(x.x.c.getTime()).to.equal(y.x.c.getTime()); | ||
done(); | ||
}); | ||
it('copies functions with properties', function (done) { | ||
var a = { | ||
x: function () { return 1; }, | ||
y: {} | ||
}; | ||
a.x.z = 'string in function'; | ||
a.x.v = function () { return 2; }; | ||
a.y.u = a.x; | ||
var b = Hoek.clone(a); | ||
expect(b.x()).to.equal(1); | ||
expect(b.x.v()).to.equal(2); | ||
expect(b.y.u).to.equal(b.x); | ||
expect(b.x.z).to.equal('string in function'); | ||
done(); | ||
}); | ||
}); | ||
@@ -61,0 +180,0 @@ |
96917
15
1207