@intuitionrobotics/ts-common
Version:
102 lines • 3.39 kB
JavaScript
;
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepClone = deepClone;
exports._keys = _keys;
exports._values = _values;
exports._setTypedProp = _setTypedProp;
exports.cloneArr = cloneArr;
exports.cloneObj = cloneObj;
exports.compare = compare;
exports.assert = assert;
const index_1 = require("../index");
function deepClone(obj) {
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || typeof obj === 'undefined' || obj === null)
return obj;
if (Array.isArray(obj))
return cloneArr(obj);
return cloneObj(obj);
}
function _keys(instance) {
return Object.keys(instance);
}
function _values(object) {
return Object.values(object);
}
function _setTypedProp(instance, key, value) {
instance[key] = value;
}
function cloneArr(value) {
return value.map(a => deepClone(a));
}
function cloneObj(obj) {
return _keys(obj).reduce((carry, key) => {
carry[key] = deepClone(obj[key]);
return carry;
}, {});
}
function compare(one, two, keys) {
const typeofOne = typeof one;
const typeofTwo = typeof two;
if (typeofOne !== typeofTwo)
return false;
if (one === undefined && two === undefined)
return true;
if (one === undefined || two === undefined)
return false;
if (one === null && two === null)
return true;
if (one === null || two === null)
return false;
if (typeofOne === "function")
throw new index_1.BadImplementationException("This compare meant to compare two POJOs.. nothing more");
if (typeofOne !== "object")
return one === two;
if (Array.isArray(one) && Array.isArray(two)) {
if (one.length !== two.length)
return false;
for (let i = 0; i < one.length; i++) {
if (compare(one[i], two[i]))
continue;
return false;
}
return true;
}
const _one = one;
const _two = two;
const oneKeys = keys || Object.keys(_one);
const twoKeys = keys || Object.keys(_two);
if (oneKeys.length !== twoKeys.length)
return false;
for (const oneKey of oneKeys) {
if (!twoKeys.includes(oneKey))
return false;
}
for (const oneKey of oneKeys) {
if (compare(_one[oneKey], _two[oneKey]))
continue;
return false;
}
return true;
}
function assert(message, expected, actual) {
if (!compare(expected, actual))
throw new index_1.AssertionException(`Assertion Failed:\n -- ${message}\n -- Expected: ${JSON.stringify(expected)}\n -- Actual: ${JSON.stringify(actual)}\n\n`);
}
//# sourceMappingURL=object-tools.js.map