@oozcitak/dom
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -16,7 +16,29 @@ import { GlobalStore } from './GlobalStore'; | ||
* | ||
* @param baseClass - class to recieve te mixin | ||
* @param baseClass - class to receive the mixin | ||
* @param mixinClass - mixin class | ||
* @param overrides - an array with names of function overrides. Base class | ||
* functions whose names are in this array will be kept by prepending an | ||
* underscore to their names. | ||
*/ | ||
export declare function applyMixin(baseClass: any, mixinClass: any): void; | ||
export declare function applyMixin(baseClass: any, mixinClass: any, ...overrides: string[]): void; | ||
/** | ||
* Applies default values to the given object. | ||
* | ||
* @param obj - an object | ||
* @param defaults - an object with default values | ||
* @param overwrite - if set to `true` defaults object always overwrites object | ||
* values, whether they are `undefined` or not. | ||
*/ | ||
export declare function applyDefaults<T>(obj: { | ||
[key: string]: any; | ||
} | undefined, defaults: { | ||
[key: string]: any; | ||
}, overwrite?: boolean): T; | ||
/** | ||
* Iterates over items pairs of an array. | ||
* | ||
* @param arr - array to iterate | ||
*/ | ||
export declare function forEachArray<T>(arr: Array<T>): IterableIterator<T>; | ||
/** | ||
* Iterates over key/value pairs of a map or object. | ||
@@ -30,2 +52,28 @@ * | ||
/** | ||
* Returns the number of entries in a map or object. | ||
* | ||
* @param obj - map or object | ||
*/ | ||
export declare function objectLength(obj: Map<string, any> | { | ||
[key: string]: any; | ||
}): number; | ||
/** | ||
* Gets the value of a key from a map or object. | ||
* | ||
* @param obj - map or object | ||
* @param key - the key to retrieve | ||
*/ | ||
export declare function getObjectValue<T>(obj: Map<string, T> | { | ||
[key: string]: T; | ||
}, key: string): T | undefined; | ||
/** | ||
* Removes a property from a map or object. | ||
* | ||
* @param obj - map or object | ||
* @param key - the key to remove | ||
*/ | ||
export declare function removeObjectValue<T>(obj: Map<string, T> | { | ||
[key: string]: T; | ||
}, key: string): void; | ||
/** | ||
* Deep clones the given object. | ||
@@ -77,2 +125,8 @@ * | ||
/** | ||
* Type guard for maps. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
export declare function isMap(x: any): x is Map<string, any>; | ||
/** | ||
* Determines if `x` is an empty Array or an Object with no own properties. | ||
@@ -83,1 +137,17 @@ * | ||
export declare function isEmpty(x: any): boolean; | ||
/** | ||
* Determines if `x` is a plain Object. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
export declare function isPlainObject(x: any): boolean; | ||
/** | ||
* Determines if `x` is an iterable Object. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
export declare function isIterable(x: any): boolean; | ||
/** | ||
* Gets the primitive value of an object. | ||
*/ | ||
export declare function getValue(obj: any): any; |
@@ -30,7 +30,17 @@ "use strict"; | ||
* | ||
* @param baseClass - class to recieve te mixin | ||
* @param baseClass - class to receive the mixin | ||
* @param mixinClass - mixin class | ||
* @param overrides - an array with names of function overrides. Base class | ||
* functions whose names are in this array will be kept by prepending an | ||
* underscore to their names. | ||
*/ | ||
function applyMixin(baseClass, mixinClass) { | ||
function applyMixin(baseClass, mixinClass, ...overrides) { | ||
Object.getOwnPropertyNames(mixinClass.prototype).forEach(name => { | ||
if (overrides.includes(name)) { | ||
const orgPropDesc = Object.getOwnPropertyDescriptor(baseClass.prototype, name); | ||
/* istanbul ignore else */ | ||
if (orgPropDesc) { | ||
Object.defineProperty(baseClass.prototype, "_" + name, orgPropDesc); | ||
} | ||
} | ||
const propDesc = Object.getOwnPropertyDescriptor(mixinClass.prototype, name); | ||
@@ -45,2 +55,36 @@ /* istanbul ignore else */ | ||
/** | ||
* Applies default values to the given object. | ||
* | ||
* @param obj - an object | ||
* @param defaults - an object with default values | ||
* @param overwrite - if set to `true` defaults object always overwrites object | ||
* values, whether they are `undefined` or not. | ||
*/ | ||
function applyDefaults(obj, defaults, overwrite = false) { | ||
const result = clone(obj || {}); | ||
for (const [key, val] of Object.entries(defaults)) { | ||
if (!overwrite && result[key] !== undefined) | ||
continue; | ||
if (isObject(val)) { | ||
result[key] = applyDefaults(result[key], val); | ||
} | ||
else { | ||
result[key] = val; | ||
} | ||
} | ||
return result; | ||
} | ||
exports.applyDefaults = applyDefaults; | ||
/** | ||
* Iterates over items pairs of an array. | ||
* | ||
* @param arr - array to iterate | ||
*/ | ||
function* forEachArray(arr) { | ||
for (let i = 0, len = arr.length; i < len; i++) { | ||
yield arr[i]; | ||
} | ||
} | ||
exports.forEachArray = forEachArray; | ||
/** | ||
* Iterates over key/value pairs of a map or object. | ||
@@ -51,11 +95,62 @@ * | ||
function* forEachObject(obj) { | ||
for (const key in obj) { | ||
/* istanbul ignore next */ | ||
if (!obj.hasOwnProperty(key)) | ||
continue; | ||
yield [key, obj[key]]; | ||
if (isMap(obj)) { | ||
for (const [key, val] of obj.entries()) { | ||
yield [key, val]; | ||
} | ||
} | ||
else { | ||
for (const key in obj) { | ||
/* istanbul ignore next */ | ||
if (!obj.hasOwnProperty(key)) | ||
continue; | ||
yield [key, obj[key]]; | ||
} | ||
} | ||
} | ||
exports.forEachObject = forEachObject; | ||
/** | ||
* Returns the number of entries in a map or object. | ||
* | ||
* @param obj - map or object | ||
*/ | ||
function objectLength(obj) { | ||
if (isMap(obj)) { | ||
return obj.size; | ||
} | ||
else { | ||
return Object.keys(obj).length; | ||
} | ||
} | ||
exports.objectLength = objectLength; | ||
/** | ||
* Gets the value of a key from a map or object. | ||
* | ||
* @param obj - map or object | ||
* @param key - the key to retrieve | ||
*/ | ||
function getObjectValue(obj, key) { | ||
if (isMap(obj)) { | ||
return obj.get(key); | ||
} | ||
else { | ||
return obj[key]; | ||
} | ||
} | ||
exports.getObjectValue = getObjectValue; | ||
/** | ||
* Removes a property from a map or object. | ||
* | ||
* @param obj - map or object | ||
* @param key - the key to remove | ||
*/ | ||
function removeObjectValue(obj, key) { | ||
if (isMap(obj)) { | ||
obj.delete(key); | ||
} | ||
else { | ||
delete obj[key]; | ||
} | ||
} | ||
exports.removeObjectValue = removeObjectValue; | ||
/** | ||
* Deep clones the given object. | ||
@@ -150,2 +245,11 @@ * | ||
/** | ||
* Type guard for maps. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
function isMap(x) { | ||
return x instanceof Map; | ||
} | ||
exports.isMap = isMap; | ||
/** | ||
* Determines if `x` is an empty Array or an Object with no own properties. | ||
@@ -170,2 +274,39 @@ * | ||
exports.isEmpty = isEmpty; | ||
/** | ||
* Determines if `x` is a plain Object. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
function isPlainObject(x) { | ||
if (isObject(x)) { | ||
const proto = Object.getPrototypeOf(x); | ||
const ctor = proto.constructor; | ||
return proto && ctor && | ||
(typeof ctor === 'function') && (ctor instanceof ctor) && | ||
(Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object)); | ||
} | ||
return false; | ||
} | ||
exports.isPlainObject = isPlainObject; | ||
/** | ||
* Determines if `x` is an iterable Object. | ||
* | ||
* @param x - a variable to check | ||
*/ | ||
function isIterable(x) { | ||
return x && (typeof x[Symbol.iterator] === 'function'); | ||
} | ||
exports.isIterable = isIterable; | ||
/** | ||
* Gets the primitive value of an object. | ||
*/ | ||
function getValue(obj) { | ||
if (isFunction(obj.valueOf)) { | ||
return obj.valueOf(); | ||
} | ||
else { | ||
return obj; | ||
} | ||
} | ||
exports.getValue = getValue; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@oozcitak/dom", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "dom", |
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
1095408
21749