mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 07:33:12 +09:00
moved libs to ./lib/
This commit is contained in:
1 parent
cd44b3572b
commit
6e95c86fb1
3 files changed
+1
-1
No files matched your search
@@ -0,0 +1,48 @@
|
||||
function copyChildren(d){
|
||||
var copied = clone(d);
|
||||
copy = [];
|
||||
if(copied.children){
|
||||
copied.children.forEach(function(h){
|
||||
copy.push(h);
|
||||
});
|
||||
}
|
||||
if(copied._children){
|
||||
copied._children.forEach(function(h){
|
||||
copy.push(h);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var clone = function(obj) {
|
||||
if (null == obj || "object" != typeof obj) return obj;
|
||||
var copy = obj.constructor();
|
||||
for (var attr in obj) {
|
||||
if (obj.hasOwnProperty(attr) && attr != "parent"){
|
||||
if(attr == "children" && obj[attr] != null){
|
||||
copy[attr] = obj[attr].map(function (d) {
|
||||
var subCopy = clone(d);
|
||||
return subCopy;
|
||||
});
|
||||
}
|
||||
else if(attr == "_children" && obj[attr] != null){
|
||||
copy[attr] = obj[attr].map(function(d) {
|
||||
var subCopy = clone(d);
|
||||
return subCopy;
|
||||
});
|
||||
}
|
||||
else{
|
||||
copy[attr] = obj[attr];
|
||||
}
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
var deepclone = function(obj){
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
clone:clone,
|
||||
deepclone:deepclone,
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
const sinon = require("sinon");
|
||||
var assert = require('assert');
|
||||
const libomega = require('./libomega');
|
||||
/*
|
||||
describe('The webpage module', function () {
|
||||
it('saves the content', function * () {
|
||||
const url = 'google.com'
|
||||
const content = '<h1>title</h1>'
|
||||
const writeFileStub = this.sandbox.stub(fs, 'writeFile', function (filePath, fileContent, cb) {
|
||||
cb(null)
|
||||
})
|
||||
|
||||
const requestStub = this.sandbox.stub(request, 'get', function (url, cb) {
|
||||
cb(null, null, content)
|
||||
})
|
||||
|
||||
const result = yield webpage.saveWebpage(url)
|
||||
|
||||
expect(writeFileStub).to.be.calledWith()
|
||||
expect(requestStub).to.be.calledWith(url)
|
||||
expect(result).to.eql('page')
|
||||
})
|
||||
})
|
||||
*/
|
||||
describe("libomega.js", function(){
|
||||
describe("libomega.clone", function(){
|
||||
it("clones javascript objects", function(){
|
||||
var object1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}};
|
||||
var object2 = {};
|
||||
object2 = libomega.clone(object1);
|
||||
|
||||
assert.equal(object2.cat.name, object1.cat.name)
|
||||
assert.equal(object1.hello, object2.hello)
|
||||
});
|
||||
it("does not deep clone objects", function(){
|
||||
var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}};
|
||||
var obj2 = {};
|
||||
obj2 = libomega.clone(obj1);
|
||||
|
||||
obj1.cat.colors = "black";
|
||||
assert.equal(obj1.cat.colors, "black");
|
||||
assert.notEqual(obj2.cat.colors[1], "yellow");
|
||||
});
|
||||
});
|
||||
describe("libomega.deepclone", function(){
|
||||
it("deep clones javascript objects", function(){
|
||||
var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}};
|
||||
var obj2 = {};
|
||||
obj2 = libomega.deepclone(obj1);
|
||||
|
||||
obj1.cat.colors = "black";
|
||||
assert.equal(obj1.cat.colors, "black");
|
||||
assert.equal(obj2.cat.colors[1], "yellow");
|
||||
});
|
||||
it("throws on non JSON parameters", function(){
|
||||
x = function(){};
|
||||
assert.throws(function(){
|
||||
y = libomega.deepclone(x)
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user