{"id":22021581,"url":"https://github.com/lamansky/def-props","last_synced_at":"2026-05-07T03:32:31.476Z","repository":{"id":57225774,"uuid":"221891676","full_name":"lamansky/def-props","owner":"lamansky","description":"[Node.js] Defines multiple object properties all at once, optionally with shared settings.","archived":false,"fork":false,"pushed_at":"2019-11-15T09:46:43.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-27T05:05:04.633Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lamansky.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-15T09:36:52.000Z","updated_at":"2019-11-15T09:46:45.000Z","dependencies_parsed_at":"2022-08-24T10:40:26.426Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/def-props","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fdef-props","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fdef-props/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fdef-props/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fdef-props/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/def-props/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245085088,"owners_count":20558331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-30T06:13:05.466Z","updated_at":"2026-05-07T03:32:31.448Z","avatar_url":"https://github.com/lamansky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# def-props\n\nDefines multiple object properties all at once, optionally with shared settings.\n\nRead more about property descriptor settings at [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).\n\n## Installation\n\nRequires [Node.js](https://nodejs.org/) 8.3.0 or above.\n\n```bash\nnpm i def-props\n```\n\n## API\n\nThe module exports a single function.\n\n### Parameters\n\n1. `obj` (object): The object (or function) to which you want to add properties.\n2. `props` (object): This must be one of the following:\n    * An object whose keys are property names and whose values are property descriptors for the new object.\n    * An array (or other iterable) of key-value pairs for the object.\n    * An array (or other iterable) of key-descriptor pairs for the object’s properties (if `descs` is set to `true`).\n2. Optional: Object argument:\n    * `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`.\n    * `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`.\n    * `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.\n\n### Return Value\n\nReturns the modified `obj`.\n\n## Examples\n\n### Object of Key-Descriptor Pairs\n\nIn this example, we define two getters on an object.\n\n```javascript\nconst defProps = require('def-props')\n\nconst obj = {}\ndefProps(obj, {\n  a: {get: () =\u003e 1},\n  b: {get: () =\u003e 2},\n})\n\nobj.a // 1\nobj.b // 2\n```\n\n### Supplementing Descriptors with Default Settings\n\nWhenever you’re using the module in a property descriptor mode, you can specify settings to be applied to all property descriptors using `descDefaults`.\n\n```javascript\nconst defProps = require('def-props')\n\nconst obj = {}\ndefProps(obj, {\n  a: {get: () =\u003e 1}, // `writable` (below) will be ignored for this property\n  b: {value: 2},\n}, {\n  descDefaults: {configurable: true, writable: true},\n})\n```\n\nSince 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.\n\n### Array of Key-Value Pairs\n\nYou 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.\n\n```javascript\nconst defProps = require('def-props')\n\nconst obj = {}\ndefProps(\n  obj,\n  [['a', 1], ['b', 2]],\n  {descDefaults: {writable: false}}\n)\n\nobj.a // 1\nobj.b // 2\n\nobj.a = 123 // TypeError: Cannot set property a of #\u003cObject\u003e which has only a getter\n```\n\n### Array of Key-Descriptor Pairs\n\nIn 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`.\n\n```javascript\nconst defProps = require('def-props')\n\nconst obj = {}\ndefProps(\n  obj,\n  [\n    ['a', {get: () =\u003e 1}],\n    ['b', {get: () =\u003e 2}],\n  ],\n  {descs: true}\n)\n\nobj.a // 1\nobj.b // 2\n\nobj.a = 123 // TypeError: Cannot set property a of #\u003cObject\u003e which has only a getter\n```\n\n## Related\n\n* [new-object](https://github.com/lamansky/new-object): Same as this module, but creates a new object instead of modifying an existing one.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fdef-props","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2Fdef-props","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fdef-props/lists"}