All files / src/lib object-utils.ts

93.33% Statements 28/30
88.23% Branches 15/17
100% Functions 6/6
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68          1x       1x   1x 2x 2x 2x             1x 155x 153x 152x   152x 73x 45x     73x                       709x     1x 440x 269x     269x 269x     269x 269x       269x 12x       257x    
/*
 * © Copyright 2022 HP Development Company, L.P.
 * SPDX-License-Identifier: MIT
 */
 
export function mapObject<T extends object, TResult = unknown>(
	obj: T,
	iteratee: (value: T[keyof T], key: keyof T) => TResult
): { [P in keyof T]: TResult } {
	const keys = Object.keys(obj) as Array<keyof T>;
 
	return keys.reduce((acc, key) => {
		const value = obj[key];
		acc[key] = iteratee(value, key);
		return acc;
	}, {} as { [P in keyof T]: TResult });
}
 
export function omit(obj: undefined, keys: Array<string>): undefined;
export function omit(obj: null, keys: Array<string>): null;
export function omit<T extends object>(obj: T, keys: Array<keyof T>): Partial<T>;
export function omit<T extends object>(obj: T | null | undefined, keys: Array<keyof T>): Partial<T> | null | undefined {
	if (typeof obj !== 'object' || typeof obj === 'undefined' || obj === null) return obj;
	if (!keys.length) return obj;
	const objKeys = Object.keys(obj) as Array<keyof T>;
 
	return objKeys.reduce((acc, key) => {
		if (keys.indexOf(key) === -1) {
			acc[key] = obj[key];
		}
 
		return acc;
	}, {} as Partial<T>);
}
 
/*!
 * is-plain-object <https://github.com/jonschlinkert/is-plain-object>
 *
 * Copyright (c) 2014-2017, Jon Schlinkert.
 * Released under the MIT License.
 */
 
function isObject(o: unknown) {
	return Object.prototype.toString.call(o) === '[object Object]';
}
 
export function isPlainObject(o: unknown) {
	if (isObject(o) === false) return false;
	const obj = o as object;
 
	// If it has modified constructor
	const ctor = obj.constructor;
	Iif (ctor === undefined) return true;
 
	// If it has modified prototype
	const prot = ctor.prototype;
	Iif (isObject(prot) === false) return false;
 
	// if constructor does not have an Object-specific method
	// eslint-disable-next-line no-prototype-builtins
	if (prot.hasOwnProperty('isPrototypeOf') === false) {
		return false;
	}
 
	// Most likely a plain Object
	return true;
}