@intuitionrobotics/ts-common
Version:
92 lines • 3.16 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.
*/
import { AssertionException, BadImplementationException } from "../index.js";
export 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);
}
export function _keys(instance) {
return Object.keys(instance);
}
export function _values(object) {
return Object.values(object);
}
export function _setTypedProp(instance, key, value) {
instance[key] = value;
}
export function cloneArr(value) {
return value.map(a => deepClone(a));
}
export function cloneObj(obj) {
return _keys(obj).reduce((carry, key) => {
carry[key] = deepClone(obj[key]);
return carry;
}, {});
}
export 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 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;
}
export function assert(message, expected, actual) {
if (!compare(expected, actual))
throw new AssertionException(`Assertion Failed:\n -- ${message}\n -- Expected: ${JSON.stringify(expected)}\n -- Actual: ${JSON.stringify(actual)}\n\n`);
}
//# sourceMappingURL=object-tools.js.map