@hgargg-0710/one
Version:
A tiny npm library purposed for providing beautiful solutions to frequent miniature (one-line/one-expression) tasks for various JS datatypes.
33 lines (32 loc) • 1.52 kB
JavaScript
import { extendPrototype, propertyDescriptors, withoutProperties } from "./main.js";
/**
* Returns a function that returns `new X(...args)`
*/
export const classWrapper = (X) => (...args) => new X(...args);
/**
* Returns a method that returns `this[delegateProp][delegateMethodName](...delegateArgs)`
*/
export const delegateMethod = (delegatePropName) => (delegateMethodName) => function (...delegateArgs) {
return this[delegatePropName][delegateMethodName](...delegateArgs);
};
/**
* Returns a method that returns `this[delegatePropName][propName]`
*/
export const delegateProperty = (delegatePropName) => (propName) => function () {
return this[delegatePropName][propName];
};
/**
* Calls `extendPrototype` for each of the passed `Constructor`s `classes`,
* that are removed their `.constructor` property from.
*
* Useful for multiple inheritance and mixin pattern
*/
export const mixin = (Extended, classes) => classes.forEach((ParentClass) => extendPrototype(Extended, withoutConstructor(propertyDescriptors(ParentClass.prototype))));
/**
* Returns the copy of the given object with the `.constructor` property removed
*/
export const withoutConstructor = withoutProperties("constructor");
/**
* Returns a function that returns `called[delegatePropName][delegateMethodName].call(called, ...delegateArgs)`
*/
export const calledDelegate = (delegatePropName) => (delegateMethodName) => (called, ...delegateArgs) => called[delegatePropName][delegateMethodName].call(called, ...delegateArgs);