UNPKG

piumino

Version:

Piumino, meaning duvet in Italian, is like a duvet over the bed called Angular TestBed. It provides test helpers to test trivial things like inputs and outputs with a single line.

54 lines (53 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectHelper = void 0; class ObjectHelper { static getProperty(obj, path) { const keys = ObjectHelper.pathToKeys(path); const value = obj[keys[0]]; if (value && keys.length > 1) { return this.getProperty(value, keys.slice(1)); } return value; } static setProperty(obj, path, value) { const keys = ObjectHelper.pathToKeys(path); const lastKey = keys.pop(); const parent = keys.length ? this.getProperty(obj, keys) : obj; if (lastKey) { parent[lastKey] = value; } } static hasProperty(obj, path) { const keys = ObjectHelper.pathToKeys(path); const lastKey = keys.pop(); const parent = keys.length ? this.getProperty(obj, keys) : obj; if (lastKey) { return lastKey in parent; } return false; } static replaceFunction(obj, func, implementation) { ObjectHelper.originalFunctions.set(this.getOriginalFunctionKey(obj, func), ObjectHelper.getProperty(obj, func)); obj[func] = implementation; } static restoreFunction(obj, func) { const originalFunctionKey = this.getOriginalFunctionKey(obj, func); const originalImplementation = ObjectHelper.originalFunctions.get(originalFunctionKey); if (originalImplementation) { ObjectHelper.setProperty(obj, func, originalImplementation); ObjectHelper.originalFunctions.delete(originalFunctionKey); } } static isObject(obj) { return typeof obj === 'function' || typeof obj === 'object' && !!obj; } static pathToKeys(path) { return Array.isArray(path) ? path : path.replace(/\[(\d)\]/g, ".$1").split("."); } static getOriginalFunctionKey(obj, func) { return `${obj.constructor.name}:func`; } } exports.ObjectHelper = ObjectHelper; ObjectHelper.originalFunctions = new Map();