mongoose-history-diff
Version:
A mongoose plugin to take diffs and history of your documents
185 lines (184 loc) • 6.42 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepClone = exports.revertArrayChange = exports.arrayRemove = exports.getOrderIndependentHash = exports.hashThisString = exports.realTypeOf = exports.getExcludedFields = exports.excludeFields = void 0;
exports.excludeFields = function (path, key, excludedFields) {
if (path.length === 0 && key === '_id')
return true;
var isFilter = false;
excludedFields.forEach(function (field) {
if (path.length === field.lvl && key === field.key)
isFilter = true;
});
return isFilter;
};
exports.getExcludedFields = function (schema) {
var excludedFields = [];
Object.values(schema.paths).forEach(function (value) {
var _a = value || {}, options = _a.options, path = _a.path;
if ((options === null || options === void 0 ? void 0 : options.track_diff) === false) {
var splittedPath = path.split('.');
var lvl = splittedPath.length - 1;
var key = splittedPath[lvl];
excludedFields.push({ key: key, lvl: lvl });
}
else if (value.instance === 'Array') {
value.options.type.forEach(function (obj) {
var aKey = Object.keys(obj)[0];
var aPath = [path, aKey].join('.');
var aOptions = obj[aKey];
if ((aOptions === null || aOptions === void 0 ? void 0 : aOptions.track_diff) === false) {
var splittedPath = aPath.split('.');
var lvl = splittedPath.length;
var key = splittedPath[lvl - 1];
excludedFields.push({ key: key, lvl: lvl });
}
});
}
});
return excludedFields;
};
exports.realTypeOf = function (obj) {
var type = typeof obj;
if (type !== 'object') {
return type;
}
if (obj === Math) {
return 'math';
}
if (obj === null) {
return 'null';
}
if (Array.isArray(obj)) {
return 'array';
}
if (Object.prototype.toString.call(obj) === '[object Date]') {
return 'date';
}
if (typeof (obj === null || obj === void 0 ? void 0 : obj.toString) === 'function' && /^\/.*\//.test(obj.toString())) {
return 'regexp';
}
return 'object';
};
exports.hashThisString = function (str) {
var hash = 0;
if (str.length === 0) {
return hash;
}
for (var i = 0; i < str.length; i += 1) {
var char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash &= hash;
}
return hash;
};
exports.getOrderIndependentHash = function (obj) {
var accum = 0;
var type = exports.realTypeOf(obj);
if (type === 'array') {
obj.forEach(function (item) {
accum += exports.getOrderIndependentHash(item);
});
var arrayString = "[type: array, hash: " + accum + "]";
return accum + exports.hashThisString(arrayString);
}
if (type === 'object') {
Object.keys(obj).forEach(function (key) {
var keyValueString = "[ type: object, key: " + key + ", value hash: " + exports.getOrderIndependentHash(obj[key]) + "]";
accum += exports.hashThisString(keyValueString);
});
return accum;
}
var stringToHash = "[ type: " + type + " ; value: " + (obj ? obj.toString() : 'hash') + "]";
return accum + exports.hashThisString(stringToHash);
};
exports.arrayRemove = function (arr, from, to) {
var rest = arr.slice((to || from) + 1 || arr.length);
arr.length = from < 0 ? arr.length + from : from;
arr.push.apply(arr, rest);
return arr;
};
exports.revertArrayChange = function (arr, index, change) {
var _a;
if ((_a = change === null || change === void 0 ? void 0 : change.p) === null || _a === void 0 ? void 0 : _a.length) {
var element = arr[index];
var j = void 0;
for (j = 0; j < change.p.length - 1; j += 1) {
element = element[change.p[j]];
}
switch (change.kind) {
case 'A':
exports.revertArrayChange(element[change.p[j]], change.i, change.it);
break;
case 'D':
element[change.p[j]] = change.l;
break;
case 'E':
element[change.p[j]] = change.l;
break;
case 'N':
delete element[change.p[j]];
break;
default:
return [];
}
}
else {
switch (change.k) {
case 'A':
exports.revertArrayChange(arr[index], change.i, change.it);
break;
case 'D':
arr[index] = change.l;
break;
case 'E':
arr[index] = change.l;
break;
case 'N':
arr = exports.arrayRemove(arr, index);
break;
default:
arr = [];
}
}
return arr;
};
exports.deepClone = function (obj) {
if (exports.realTypeOf(obj) === 'object') {
var clone = __assign({}, obj);
for (var k in clone) {
if (Object.prototype.hasOwnProperty.call(clone, k)) {
clone[k] = exports.deepClone(clone[k]);
}
}
return clone;
}
if (exports.realTypeOf(obj) === 'array') {
var clone_1 = __spreadArrays(obj);
clone_1.forEach(function (v, i) {
clone_1[i] = exports.deepClone(v);
});
return clone_1;
}
if (exports.realTypeOf(obj) === 'date') {
return new Date(obj);
}
return obj;
};