X-Git-Url: https://git.ladys.computer/Pisces/blobdiff_plain/2edbddced262b90f8422e77fe8ec61a9f8c82e68..fb3e0d562e2dbe9e3ea911a80bfdabc8851f92b2:/object.js?ds=sidebyside diff --git a/object.js b/object.js index a34423e..f68909e 100644 --- a/object.js +++ b/object.js @@ -573,26 +573,62 @@ export const { * Removes the provided property key from the provided object and * returns the object. * - * ☡ This function differs from Reflect.deleteProperty and the + * ※ This function differs from Reflect.deleteProperty and the * `delete` operator in that it throws if the deletion is * unsuccessful. + * + * ※ This function throws if the first argument is not an object. */ deleteOwnProperty, + /** + * Returns an array of property keys on the provided object. + * + * ※ This is effectively an alias for Reflect.ownKeys, except that + * it does not require that the argument be an object. + */ + getOwnPropertyKeys, + + /** + * Returns the value of the provided property key on the provided + * object. + * + * ※ This is effectively an alias for Reflect.get, except that it + * does not require that the argument be an object. + */ + getPropertyValue, + + /** + * Returns whether the provided property key exists on the provided + * object. + * + * ※ This is effectively an alias for Reflect.has, except that it + * does not require that the argument be an object. + * + * ※ This includes properties present on the prototype chain. + */ + hasProperty, + /** * Sets the provided property key to the provided value on the * provided object and returns the object. * * ※ This function differs from Reflect.set in that it throws if the * setting is unsuccessful. + * + * ※ This function throws if the first argument is not an object. */ setPropertyValue, } = (() => { - const { deleteProperty, set } = Reflect; + const { deleteProperty, get, has, ownKeys, set } = Reflect; return { deleteOwnProperty: (O, P) => { - if (!deleteProperty(O, P)) { + if (type(O) !== "object") { + throw new TypeError( + `Piscēs: Tried to set property but provided value was not an object: ${V}`, + ); + } else if (!deleteProperty(O, P)) { throw new TypeError( `Piscēs: Tried to delete property from object but [[Delete]] returned false: ${P}`, ); @@ -600,8 +636,16 @@ export const { return O; } }, + getOwnPropertyKeys: (O) => ownKeys(toObject(O)), + getPropertyValue: (O, P, Receiver = O) => + get(toObject(O), P, Receiver), + hasProperty: (O, P) => has(toObject(O), P), setPropertyValue: (O, P, V, Receiver = O) => { - if (!set(O, P, V, Receiver)) { + if (type(O) !== "object") { + throw new TypeError( + `Piscēs: Tried to set property but provided value was not an object: ${V}`, + ); + } else if (!set(O, P, V, Receiver)) { throw new TypeError( `Piscēs: Tried to set property on object but [[Set]] returned false: ${P}`, ); @@ -714,46 +758,38 @@ export const { }; })(); -export const { - /** - * Returns an array of property keys on the provided object. - * - * ※ This is an alias for Reflect.ownKeys. - */ - ownKeys: getOwnPropertyKeys, - - /** - * Returns the value of the provided property key on the provided - * object. - * - * ※ This is an alias for Reflect.get. - */ - get: getPropertyValue, - - /** - * Returns whether the provided property key exists on the provided - * object. - * - * ※ This is an alias for Reflect.has. - * - * ※ This includes properties present on the prototype chain. - */ - has: hasProperty, -} = Reflect; +export const getMethod = (V, P) => { + const func = getPropertyValue(V, P); + if (func == null) { + return undefined; + } else if (typeof func !== "function") { + throw new TypeError(`Piscēs: Method not callable: ${P}`); + } else { + return func; + } +}; /** * Returns the provided value converted to an object. * - * Null and undefined are converted to a new, empty object. Other - * primitives are wrapped. Existing objects are returned with no - * modification. + * Existing objects are returned with no modification. * - * ※ This is effectively a nonconstructible version of the Object - * constructor. + * ☡ This function throws a TypeError if its argument is null or + * undefined. */ export const { toObject } = (() => { const makeObject = Object; - return { toObject: ($) => makeObject($) }; + return { + toObject: ($) => { + if ($ == null) { + throw new TypeError( + `Piscēs: Cannot convert ${$} into an object.`, + ); + } else { + return makeObject($); + } + }, + }; })(); /**