@oozcitak/util
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -24,13 +24,18 @@ export { IndexedSet } from './IndexedSet'; | ||
*/ | ||
export declare function applyDefaults<T>(obj: { | ||
export declare function applyDefaults(obj: { | ||
[key: string]: any; | ||
} | undefined, defaults: { | ||
[key: string]: any; | ||
}, overwrite?: boolean): T; | ||
}, overwrite?: boolean): { | ||
[key: string]: any; | ||
}; | ||
/** | ||
* Iterates over items pairs of an array. | ||
* Iterates over items of an array or set. | ||
* | ||
* @param arr - array to iterate | ||
* @param arr - array or set to iterate | ||
* @param callback - a callback function which receives each array item as its | ||
* single argument | ||
* @param thisArg - the value of this inside callback | ||
*/ | ||
export declare function forEachArray<T>(arr: Array<T>): IterableIterator<T>; | ||
export declare function forEachArray<T>(arr: Array<T> | Set<T>, callback: ((item: T) => void), thisArg?: any): void; | ||
/** | ||
@@ -40,7 +45,16 @@ * Iterates over key/value pairs of a map or object. | ||
* @param obj - map or object to iterate | ||
* @param callback - a callback function which receives object key as its first | ||
* argument and object value as its second argument | ||
* @param thisArg - the value of this inside callback | ||
*/ | ||
export declare function forEachObject<T>(obj: Map<string, T> | { | ||
[key: string]: T; | ||
}): IterableIterator<[string, T]>; | ||
}, callback: ((key: string, item: T) => void), thisArg?: any): void; | ||
/** | ||
* Returns the number of entries in an array or set. | ||
* | ||
* @param arr - array or set | ||
*/ | ||
export declare function arrayLength(obj: any[] | Set<any>): number; | ||
/** | ||
* Returns the number of entries in a map or object. | ||
@@ -76,3 +90,5 @@ * | ||
*/ | ||
export declare function clone<T extends Function | any[] | Object>(obj: T): T; | ||
export declare function clone<T extends string | number | boolean | null | undefined | Function | any[] | { | ||
[key: string]: any; | ||
}>(obj: T): T; | ||
/** | ||
@@ -119,2 +135,8 @@ * Type guard for boolean types | ||
/** | ||
* Type guard for sets. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
export declare function isSet(x: any): x is Set<any>; | ||
/** | ||
* Type guard for maps. | ||
@@ -121,0 +143,0 @@ * |
@@ -49,3 +49,3 @@ "use strict"; | ||
const result = clone(obj || {}); | ||
for (const [key, val] of forEachObject(defaults)) { | ||
forEachObject(defaults, (key, val) => { | ||
if (isObject(val)) { | ||
@@ -57,3 +57,3 @@ result[key] = applyDefaults(result[key], val); | ||
} | ||
} | ||
}); | ||
return result; | ||
@@ -63,8 +63,11 @@ } | ||
/** | ||
* Iterates over items pairs of an array. | ||
* Iterates over items of an array or set. | ||
* | ||
* @param arr - array to iterate | ||
* @param arr - array or set to iterate | ||
* @param callback - a callback function which receives each array item as its | ||
* single argument | ||
* @param thisArg - the value of this inside callback | ||
*/ | ||
function* forEachArray(arr) { | ||
yield* arr; | ||
function forEachArray(arr, callback, thisArg) { | ||
arr.forEach(callback, thisArg); | ||
} | ||
@@ -76,13 +79,16 @@ exports.forEachArray = forEachArray; | ||
* @param obj - map or object to iterate | ||
* @param callback - a callback function which receives object key as its first | ||
* argument and object value as its second argument | ||
* @param thisArg - the value of this inside callback | ||
*/ | ||
function* forEachObject(obj) { | ||
function forEachObject(obj, callback, thisArg) { | ||
if (isMap(obj)) { | ||
yield* obj; | ||
obj.forEach((value, key) => callback.call(thisArg, key, value)); | ||
} | ||
else { | ||
for (const key in obj) { | ||
/* istanbul ignore next */ | ||
if (!obj.hasOwnProperty(key)) | ||
continue; | ||
yield [key, obj[key]]; | ||
/* istanbul ignore else */ | ||
if (obj.hasOwnProperty(key)) { | ||
callback.call(thisArg, key, obj[key]); | ||
} | ||
} | ||
@@ -93,2 +99,16 @@ } | ||
/** | ||
* Returns the number of entries in an array or set. | ||
* | ||
* @param arr - array or set | ||
*/ | ||
function arrayLength(obj) { | ||
if (isSet(obj)) { | ||
return obj.size; | ||
} | ||
else { | ||
return obj.length; | ||
} | ||
} | ||
exports.arrayLength = arrayLength; | ||
/** | ||
* Returns the number of entries in a map or object. | ||
@@ -227,2 +247,11 @@ * | ||
/** | ||
* Type guard for sets. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
function isSet(x) { | ||
return x instanceof Set; | ||
} | ||
exports.isSet = isSet; | ||
/** | ||
* Type guard for maps. | ||
@@ -229,0 +258,0 @@ * |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _1 = require("."); | ||
const util_1 = require("util"); | ||
/** | ||
@@ -22,6 +21,3 @@ * Represents a set of objects with indexed access. Uses double the memory of an | ||
let index; | ||
if (_1.isNumber(key)) { | ||
index = key; | ||
} | ||
else if (util_1.isString(key)) { | ||
if (_1.isString(key)) { | ||
const num = Number(key); | ||
@@ -43,6 +39,3 @@ if (!isNaN(num)) | ||
let index; | ||
if (_1.isNumber(key)) { | ||
index = key; | ||
} | ||
else if (util_1.isString(key)) { | ||
if (_1.isString(key)) { | ||
const num = Number(key); | ||
@@ -92,2 +85,3 @@ if (!isNaN(num)) | ||
const nextIndex = this._index.get(nextItem); | ||
/* istanbul ignore else */ | ||
if (nextIndex !== undefined) { | ||
@@ -94,0 +88,0 @@ this._index.set(nextItem, i); |
{ | ||
"name": "@oozcitak/util", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "util", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53671
1270