npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

property-definer

v1.0.8

Published

Module based on `Object.defineProperty`

Downloads

2

Readme

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" }