daniel-san
Version:
a node-based budget-projection engine that helps your routines and finances find balance. The program features aggregates, terminal and file-based reporting output, multi-currency conversion capability and multi-frequency accounting triggers, including: o
89 lines (74 loc) • 2.26 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _require = require('./validation'),
isArray = _require.isArray,
isMap = _require.isMap,
isSet = _require.isSet,
isObject = _require.isObject,
isUndefinedOrNull = _require.isUndefinedOrNull;
function objectCopy(object) {
var newObject = {};
Object.keys(object).forEach(function (key) {
newObject[key] = cloneStrategy(object[key]);
});
return newObject;
}
function mapCopy(map) {
var tempArray = [];
var keys = (0, _toConsumableArray2["default"])(map.keys());
var values = (0, _toConsumableArray2["default"])(map.values());
keys.forEach(function (key, index) {
var newValue = cloneStrategy(values[index]); // value might be a reference to an iterable structure
tempArray.push([key, newValue]); // the key should always be a primitive
});
var newMap = new Map(tempArray);
return newMap;
}
function setCopy(set) {
var tempArray = [];
var setArray = (0, _toConsumableArray2["default"])(set);
setArray.forEach(function (element) {
tempArray.push(cloneStrategy(element));
});
var newSet = new Set(tempArray);
return newSet;
}
function arrayCopy(array) {
var newArray = [];
array.forEach(function (element) {
newArray.push(cloneStrategy(element));
});
return newArray;
}
function cloneStrategy(entity) {
var newEntity;
if (isUndefinedOrNull(entity)) {
newEntity = entity; // immediately return undefined or null
} else if (isMap(entity)) {
newEntity = mapCopy(entity);
}
if (isSet(entity)) {
newEntity = setCopy(entity);
}
if (isArray(entity)) {
newEntity = arrayCopy(entity);
} else if (isObject(entity)) {
newEntity = objectCopy(entity);
} else {
newEntity = entity; // whatever it was it is and nothing further can or need be done about it
}
return newEntity;
}
function deepCopy(entity) {
var newEntity = cloneStrategy(entity);
return newEntity;
}
module.exports = {
deepCopy: deepCopy,
objectCopy: objectCopy,
mapCopy: mapCopy,
setCopy: setCopy,
arrayCopy: arrayCopy,
cloneStrategy: cloneStrategy
};