From: Lady Date: Wed, 22 Nov 2023 02:43:10 +0000 (-0500) Subject: Refactor some functions in function.js to simplify X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/aa1ae4c089cec40d79c3087ed7d6007dade77815?ds=inline Refactor some functions in function.js to simplify --- diff --git a/function.js b/function.js index d58d290..d02fe6e 100644 --- a/function.js +++ b/function.js @@ -58,9 +58,7 @@ export const { bind: functionBind, call: functionCall, } = functionPrototype; - const callBind = Reflect.apply(functionBind, functionCall, [ - functionBind, - ]); + const { apply: reflectApply } = Reflect; const { create: objectCreate, defineProperty: defineOwnProperty, @@ -69,6 +67,9 @@ export const { getPrototypeOf: getPrototype, setPrototypeOf: setPrototype, } = Object; + const callBind = reflectApply(functionBind, functionCall, [ + functionBind, + ]); const { [ITERATOR]: arrayIterator } = Array.prototype; const { next: arrayIteratorNext, @@ -112,12 +113,9 @@ export const { } else { // Properties were provided; apply them. const { length, name, prototype } = override; - if ( - !isConstructor($) || !hasOwnProperty($, "prototype") || - prototype === undefined - ) { - // The provided function is not a constructor, has no - // `.prototype`, or no prototype value was provided. + if (!hasOwnProperty($, "prototype") || prototype === undefined) { + // The provided function has no `.prototype` or no prototype + // value was provided. // // Do not modify the prototype property of the provided // function. @@ -156,11 +154,7 @@ export const { createArrowFunction: ($, propertyOverride = undefined) => applyProperties( applyBaseFunction( - (...$s) => - $(...objectCreate( - argumentIterablePrototype, - { args: { value: $s } }, - )), + (...$s) => reflectApply($, undefined, $s), $, ), propertyOverride, @@ -174,70 +168,41 @@ export const { { args: { value: $s } }, )[ITERATOR](); const { value: thisValue } = iterator.next(); - return call($, thisValue, [...iterator]); + return reflectApply($, thisValue, [...iterator]); }, $, 1, ), propertyOverride, ), - createIllegalConstructor: ($, propertyOverride = undefined) => { - const constructor = applyProperties( - applyBaseFunction( - function () { - throw new TypeError("Illegal constructor"); - }, - $, + createIllegalConstructor: ($, propertyOverride = undefined) => + defineOwnProperty( + applyProperties( + applyBaseFunction( + function () { + throw new TypeError("Illegal constructor"); + }, + $, + ), + propertyOverride, ), - propertyOverride, - ); - return defineOwnProperty(constructor, "prototype", { - writable: false, - }); - }, - }; -})(); - -export const { - /** - * Calls the provided function with the provided this value and - * arguments list. - * - * ☡ This is effectively an alias for `Reflect.apply`—the arguments - * must be passed as an arraylike. - */ - call, - - /** - * Constructs the provided function with the provided arguments list - * and new target. - * - * ☡ This is effectively an alias for `Reflect.construct`—the - * arguments must be passed as an arraylike. - */ - construct, - - /** Returns whether the provided value is a constructor. */ - isConstructor, -} = (() => { - const { apply, construct } = Reflect; - return { - call: (target, thisArgument, argumentsList) => - apply(target, thisArgument, argumentsList), - construct: (target, argumentsList, ...args) => - args.length > 0 - ? construct(target, argumentsList, args[0]) - : construct(target, argumentsList), - isConstructor: ($) => - completesNormally(() => - // Try constructing a new object with the provided value as its - // `new.target`. This will throw if the provided value is not a - // constructor. - construct(function () {}, [], $) + "prototype", + { writable: false }, ), }; })(); +/** + * Calls the provided function with the provided this value and + * arguments list. + * + * ☡ This is effectively an alias for `Reflect.apply`—the arguments + * must be passed as an arraylike. + */ +export const call = createArrowFunction(Reflect.apply, { + name: "call", +}); + /** * Returns whether calling the provided function with no `this` value * or arguments completes normally; that is, does not throw an error. @@ -264,6 +229,15 @@ export const completesNormally = ($) => { } }; +/** + * Constructs the provided function with the provided arguments list + * and new target. + * + * ☡ This is effectively an alias for `Reflect.construct`—the + * arguments must be passed as an arraylike. + */ +export const construct = createArrowFunction(Reflect.construct); + /** * Returns the provided value. * @@ -279,6 +253,15 @@ export const identity = function ($) { /** Returns whether the provided value is callable. */ export const isCallable = ($) => typeof $ === "function"; +/** Returns whether the provided value is a constructor. */ +export const isConstructor = ($) => + completesNormally(() => + // Try constructing a new object with the provided value as its + // `new.target`. This will throw if the provided value is not a + // constructor. + construct(function () {}, [], $) + ); + /** * Returns whether the provided object inherits from the prototype of * the provided function.