101 lines
2.5 KiB
JavaScript
101 lines
2.5 KiB
JavaScript
|
var cloneImpl = require('clone');
|
||
|
var debug = require('debug')('ff:tcrud:config:util');
|
||
|
var configRegistry = require('./config-registry');
|
||
|
|
||
|
var mod = (function () {
|
||
|
|
||
|
var clone = function(obj) {
|
||
|
return cloneImpl(obj);
|
||
|
};
|
||
|
|
||
|
var filterValuePart = function(value,replaceChar,splitChar,partFn) {
|
||
|
var result = '';
|
||
|
var parts = value.split(splitChar);
|
||
|
for (var i = 0; i < parts.length; i++) {
|
||
|
var part = parts[i];
|
||
|
if (part.length > 1) {
|
||
|
result += partFn(part);
|
||
|
} else {
|
||
|
result += part;
|
||
|
}
|
||
|
if (i < parts.length - 1) {
|
||
|
result += replaceChar;
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
var copyByTemplate = function(prefix,objectDest,objectSrc,copyTemplate) {
|
||
|
var keys = Object.keys(copyTemplate);
|
||
|
var result = [];
|
||
|
for (var i = 0; i < keys.length; i++) {
|
||
|
var key = keys[i];
|
||
|
var value = objectSrc[key];
|
||
|
if (value === null) {
|
||
|
//debug('copyByTemplate '+prefix+'.'+key+' src object has null value.');
|
||
|
objectDest[key] = null;
|
||
|
continue; // no src value
|
||
|
}
|
||
|
var templateValue = copyTemplate[key];
|
||
|
if (templateValue === null) {
|
||
|
if (objectDest[key] !== null) {
|
||
|
//debug('copyByTemplate '+prefix+'.'+key+' has own value: '+objectDest[key]);
|
||
|
continue;
|
||
|
} else {
|
||
|
//debug('copyByTemplate '+prefix+'.'+key+' copy value: '+value);
|
||
|
objectDest[key] = clone(value);
|
||
|
}
|
||
|
} else {
|
||
|
|
||
|
if (objectDest[key] === undefined) {
|
||
|
objectDest[key] = clone(templateValue);
|
||
|
} else {
|
||
|
//debug('copyByTemplate '+prefix+'.'+key+' going deeper');
|
||
|
copyByTemplate(prefix+'.'+key,objectDest[key],value,templateValue)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var countTree = function(obj,result) {
|
||
|
if (typeof obj === 'object') {
|
||
|
for (var key in obj) {
|
||
|
result.keys++;
|
||
|
countTree(obj[key],result)
|
||
|
}
|
||
|
} else {
|
||
|
result.values++;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return function ConfigUtil() {
|
||
|
|
||
|
this.countTree = countTree;
|
||
|
|
||
|
this.copyByTemplate = copyByTemplate;
|
||
|
|
||
|
this.clone = clone;
|
||
|
|
||
|
this.filterValue = function(value,removeChars,replaceChar,partFn) {
|
||
|
var valueOrg = value;
|
||
|
for (var i = 0; i < removeChars.length; i++) {
|
||
|
value = filterValuePart(value,replaceChar,removeChars[i],partFn);
|
||
|
}
|
||
|
//debug('filterValue.result: %s from: %s',value,valueOrg);
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
this.stringHash = function (str) {
|
||
|
var hash = 31; // prime
|
||
|
for (var i = 0; i < str.length; i++) {
|
||
|
hash = ((hash<<5)-hash)+str.charCodeAt(i);
|
||
|
hash = hash & hash; // keep 32b
|
||
|
}
|
||
|
return hash;
|
||
|
};
|
||
|
|
||
|
};
|
||
|
})();
|
||
|
|
||
|
module.exports = new mod();
|