property-definer
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

PropertyDefiner

Module based on Object.defineProperty;

Install

npm install property-definer;

Import

import 
    define, { 
        defineValue, 
        defineAccessor, 
        bindDefineValue,
        bindDefineAccessor,
        UNCONFIGURABLE,
        UNENUMERABLE,
        UNWRITABLE
    }
    from 'property-definer'

Description

Here are some enumerations that can generate descriptor more quickly.

  1. UNCONFIGURABLE (value: 1) means { configurable: false };
  2. UNENUMERABLE (value: 2) means { enumerable: false };
  3. UNWRITABLE (value: 4) means { writable: false };

You can generate complete descriptor by mixing these enumerations.

import { UNCONFIGURABLE, UNENUMERABLE } from "property-definer";
const descriptor = UNCONFIGURABLE | UNENUMERABLE; // description = 1 | 2;
const Joker = defineValue({}, "name", "Joker", descriptor);
// "{\"name\":{\"value\":\"Joker\",\"writable\":false,\"enumerable\":false,\"configurable\":true}}"
console.log(JSON.stringify(Object.getOwnPropertyDescriptors(Joker)));
  1. You can use UNCONFIGURABLE + UNENUMERABLE instead of UNCONFIGURABLE | UNENUMERABLE, because they have equal values;
  2. But you can’t use UNCONFIGURABLE + UNCONFIGURABLE instead of UNCONFIGURABLE | UNCONFIGURABLE;

Here we go

define

Define properties in bulk

function define(target: object, defines: object, description?: number | string): T;

The define method needs to accept 2~3 arguments,

  1. arguments[0]: target Object on which to add or modify the property;
  2. arguments[1]: defines JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property;
  3. arguments[2]: description (Optional) Can be a number or a string, See the description section above to learn more;
    import define from 'property-definer';
    var Joker = {};
    define(Joker, {
        name: "Joker",
        gender: { get(){ return "male" } },
        doSomething: { value: "Commit a crime" }
    }, 0);
    console.log({...Joker}); // { name: "Joker", gender: "male", doSomething: "Commit a crime" }

defineValue

Something like Object.defineProperty(target, key, { value: "some value" });

function defineValue(target: object, key: PropertyKey, value: any, description?: number | string): object;

The defineValue method needs to accept 3~4 arguments,

  1. arguments[0]: target Object on which to add or modify the property;
  2. arguments[1]: key The property name, expect string / number / symbol;
  3. arguments[2]: value Value of target[key];
  4. arguments[3]: description (Optional) Can be a number or a string, See the description section above to learn more;
    /**
     * @description You can also use:
     * @example 
     *      import { define } from 'property-definer';
     *      const { defineValue } = define;
     */
    import { defineValue } from 'property-definer';
    var Joker = {};
    defineValue(Joker, "name", "Joker", UNCONFIGURABLE);
    defineValue(Joker, "gender", "male", UNENUMERABLE);
    defineValue(Joker, "doSomething", "Commit a crime", UNWRITABLE);
    Joker.doSomething = "Be a friendly Gotham citizen"; // Will not take effect
    console.log({...Joker}); // { name: "Joker", doSomething: "Commit a crime" }

defineAccessor

Something like `Object.defineProperty(target, key, { get(){ return "some thing" }, set(value){ /** do something */ } })

function defineValue(target: object, key: PropertyKey, accessor: { get?(): any; set?:(value: any): void; }, description?: number | string): object;

The defineAccessor method needs to accept 3~4 arguments,

  1. arguments[0]: target Object on which to add or modify the property;
  2. arguments[1]: key The property name, expect string / number / symbol;
  3. arguments[2]: accessor Object containing Getter or Setter;
  4. arguments[3]: description (Optional) Can be a number or a string, See the description section above to learn more;
    /**
     * @description You can also use:
     * @example 
     *      import { define } from 'property-definer';
     *      const { defineAccessor } = define;
     */
    import { defineAccessor } from 'property-definer';
    var Joker = {};
    defineValue(Joker, "name", { get(){ return "Joker" } }, UNCONFIGURABLE);
    defineValue(Joker, "gender", { get(){ return "male" } }, UNENUMERABLE);
    console.log({...Joker}); // { name: "Joker", gender: "male" }

bindDefineValue

Bind the descriptor, and can also be further bound to the target and key

function bindDefineValue(description: number | string): (target: object, key: PropertyKey, value: any) => object;
function bindDefineValue(description: number | string, target: object): (key: PropertyKey, value: any) => object;
function bindDefineValue(description: number | string, target: object, key: PropertyKey): (value: any) => object;

The bindDefineValue method needs to accept 1~3 arguments,

  1. arguments[0]: description Can be a number or a string, See the description section above to learn more;
  2. arguments[1]: target (Optional) Object on which to add or modify the property;
  3. arguments[2]: key (Optional) The property name, expect string / number / symbol;
    /**
     * @description You can also use:
     * @example 
     *      import { define } from 'property-definer';
     *      const { bindDefineValue } = define;
     */
    import { bindDefineValue } from 'property-definer';
    var Joker = {};
    const bound = bindDefineValue(UNCONFIGURABLE | UNENUMERABLE | UNWRITABLE);
    const boundTarget = bindDefineValue(UNCONFIGURABLE | UNENUMERABLE | UNWRITABLE, Joker);
    const boundTargetAndKey = bindDefineValue(UNCONFIGURABLE | UNENUMERABLE | UNWRITABLE, Joker, "name");
    boundTargetAndKey("Joker");
    boundTarget("gender", "male");
    bound(Joker, "doSomething", "Commit a crime");
    console.log({...Joker}); // { name: "Joker", gender: "male", doSomething: "Commit a crime" }

bindDefineAccessor

Bind the descriptor, and can also be further bound to the target and key

function bindDefineAccessor(description: number | string): (target: object, key: PropertyKey, accessor: { get?(): any; set?:(value: any): void; }) => object;
function bindDefineAccessor(description: number | string, target: object): (key: PropertyKey, accessor: { get?(): any; set?:(value: any): void; }) => object;
function bindDefineAccessor(description: number | string, target: object, key: PropertyKey): (accessor: { get?(): any; set?:(value: any): void; }) => object;

The bindDefineValue method needs to accept 1~3 arguments,

  1. arguments[0]: description Can be a number or a string, See the description section above to learn more;
  2. arguments[1]: target (Optional) Object on which to add or modify the property;
  3. arguments[2]: key (Optional) The property name, expect string / number / symbol;
    /**
     * @description You can also use:
     * @example 
     *      import { define } from 'property-definer';
     *      const { bindDefineAccessor } = define;
     */
    import { bindDefineAccessor } from 'property-definer';
    var Joker = {};
    const bound = bindDefineAccessor(UNCONFIGURABLE | UNENUMERABLE);
    const boundTarget = bindDefineAccessor(UNCONFIGURABLE | UNENUMERABLE, Joker);
    const boundTargetAndKey = bindDefineAccessor(UNCONFIGURABLE | UNENUMERABLE, Joker, "name");
    boundTargetAndKey({ get(){ return "Joker" } });
    boundTarget("gender", { get(){ return "male" } });
    bound(Joker, "doSomething", { get(){ return "Commit a crime" } });
    console.log({...Joker}); // { name: "Joker", gender: "male", doSomething: "Commit a crime" }

Readme

Keywords

none

Package Sidebar

Install

npm i property-definer

Weekly Downloads

2

Version

1.0.8

License

none

Unpacked Size

28.1 kB

Total Files

6

Last publish

Collaborators

  • galadrielme