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

def-props

v1.0.0

Published

Defines multiple object properties all at once, optionally with shared settings.

Downloads

6,139

Readme

def-props

Defines multiple object properties all at once, optionally with shared settings.

Read more about property descriptor settings at Object.defineProperty().

Installation

Requires Node.js 8.3.0 or above.

npm i def-props

API

The module exports a single function.

Parameters

  1. obj (object): The object (or function) to which you want to add properties.
  2. props (object): This must be one of the following:
    • An object whose keys are property names and whose values are property descriptors for the new object.
    • An array (or other iterable) of key-value pairs for the object.
    • An array (or other iterable) of key-descriptor pairs for the object’s properties (if descs is set to true).
  3. Optional: Object argument:
    • descDefaults (plain object): A collection of descriptor settings (namely, configurable, enumerable, and/or writable) that will be applied to every property created on the new object. If you would like all these settings to be undefined, provide an empty object. Omit this option if you have specified complete descriptors in props or if you want the module to use simple assignment. If you provided property descriptors in props, the contents of descDefaults will serve as default values for those descriptors. If you specify writable in descDefaults, it will be skipped automatically for descriptors that contain getters or setters, since such descriptors cannot contain writable.
    • descs (boolean): Used to disambiguate the purpose of the values in props. If set to true, the values in props will be interpreted as property descriptor objects. If set to false, the values in props will be used as values for the new object. Defaults to false if props is an iterable; otherwise defaults to true.
    • throwIfEquivKeys (Error, string, or boolean): Only applies if props is an array. Set this to throw an error if a props array contains keys that would be considered duplicates in the context of an object. For example: a Map can have keys that are objects, but those keys will all likely evaluate to [object Object] and overwrite each other if made property keys in an object. Similarly, whereas a Map can have distinct 1 (number) and '1' (string) keys, these would be considered the same in an object context. If such a conflict exists and if this option is set to an Error object, the provided Error will be thrown as-is. An error message string will be used to construct a TypeError. A value of true will throw a TypeError with a default error message. A value of false is the same as the default behavior, which is that later equivalent keys will silently overwrite the earlier ones.

Return Value

Returns the modified obj.

Examples

Object of Key-Descriptor Pairs

In this example, we define two getters on an object.

const defProps = require('def-props')

const obj = {}
defProps(obj, {
  a: {get: () => 1},
  b: {get: () => 2},
})

obj.a // 1
obj.b // 2

Supplementing Descriptors with Default Settings

Whenever you’re using the module in a property descriptor mode, you can specify settings to be applied to all property descriptors using descDefaults.

const defProps = require('def-props')

const obj = {}
defProps(obj, {
  a: {get: () => 1}, // `writable` (below) will be ignored for this property
  b: {value: 2},
}, {
  descDefaults: {configurable: true, writable: true},
})

Since trying to set writable in a descriptor that also specifies a getter or setter will throw an error, the module will ignore the value of writable in descDefaults when creating the descriptor for a get/set property, such as the a property in the example above.

Array of Key-Value Pairs

You can also use this module together with an entries array. For example, this snippet defines key-value pairs on an object as read-only properties.

const defProps = require('def-props')

const obj = {}
defProps(
  obj,
  [['a', 1], ['b', 2]],
  {descDefaults: {writable: false}}
)

obj.a // 1
obj.b // 2

obj.a = 123 // TypeError: Cannot set property a of #<Object> which has only a getter

Array of Key-Descriptor Pairs

In addition to providing an array of key-value pairs as in the two examples above, you can also provide an array of keys paired with object property descriptors, if descs is set to true.

const defProps = require('def-props')

const obj = {}
defProps(
  obj,
  [
    ['a', {get: () => 1}],
    ['b', {get: () => 2}],
  ],
  {descs: true}
)

obj.a // 1
obj.b // 2

obj.a = 123 // TypeError: Cannot set property a of #<Object> which has only a getter

Related

  • new-object: Same as this module, but creates a new object instead of modifying an existing one.