-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation, tests, readme
- Loading branch information
Showing
15 changed files
with
687 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"root": true, | ||
|
||
"extends": "@ljharb", | ||
|
||
"rules": { | ||
"complexity": 0, | ||
"id-length": 0, | ||
"new-cap": ["error", { | ||
"capIsNewExceptions": [ | ||
"GetIntrinsic", | ||
], | ||
}], | ||
}, | ||
|
||
"overrides": [ | ||
{ | ||
"files": "test/**", | ||
"rules": { | ||
"max-lines-per-function": "off", | ||
}, | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# These are supported funding model platforms | ||
|
||
github: [ljharb] | ||
patreon: # Replace with a single Patreon username | ||
open_collective: # Replace with a single Open Collective username | ||
ko_fi: # Replace with a single Ko-fi username | ||
tidelift: npm/define-data-property | ||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry | ||
liberapay: # Replace with a single Liberapay username | ||
issuehunt: # Replace with a single IssueHunt username | ||
otechie: # Replace with a single Otechie username | ||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: 'Tests: node.js < 10' | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '< 10' | ||
type: minors | ||
command: npm run tests-only | ||
skip-ls-check: true # io.js's npm can't handle `@types/*` deps in `npm ls` | ||
|
||
node: | ||
name: 'node < 10' | ||
needs: [tests] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: 'echo tests completed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: 'Tests: pretest/posttest' | ||
|
||
on: [pull_request, push] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/pretest.yml@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: 'Tests: node.js >= 10' | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '>= 10' | ||
type: minors | ||
command: npm run tests-only | ||
|
||
node: | ||
name: 'node >= 10' | ||
needs: [tests] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: 'echo tests completed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name: Automatic Rebase | ||
|
||
on: [pull_request_target] | ||
|
||
jobs: | ||
_: | ||
uses: ljharb/actions/.github/workflows/rebase.yml@main | ||
secrets: | ||
token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: Require “Allow Edits” | ||
|
||
on: [pull_request_target] | ||
|
||
jobs: | ||
_: | ||
name: "Require “Allow Edits”" | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: ljharb/require-allow-edits@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
package-lock=false | ||
allow-same-version=true | ||
message=v%s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"all": true, | ||
"check-coverage": false, | ||
"reporter": ["text-summary", "text", "html", "json"], | ||
"lines": 86, | ||
"statements": 85.93, | ||
"functions": 82.43, | ||
"branches": 76.06, | ||
"exclude": [ | ||
"coverage", | ||
"test" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,62 @@ | ||
# define-data-property | ||
# define-data-property <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
|
||
[![github actions][actions-image]][actions-url] | ||
[![coverage][codecov-image]][codecov-url] | ||
[![License][license-image]][license-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
|
||
[![npm badge][npm-badge-png]][package-url] | ||
|
||
Define a data property on an object. Will fall back to assignment in an engine without descriptors. | ||
|
||
## Usage | ||
|
||
```javascript | ||
var defineDataProperty = require('define-data-property'); | ||
var assert = require('assert'); | ||
|
||
var obj = {}; | ||
defineDataProperty(obj, 'key', 'value'); | ||
defineDataProperty( | ||
obj, | ||
'key2', | ||
'value', | ||
true, // nonEnumerable | ||
false, // nonWritable | ||
true // nonConfigurable | ||
); | ||
|
||
assert.deepEqual( | ||
Object.getOwnPropertyDescriptors(obj), | ||
{ | ||
key: { | ||
configurable: true, | ||
enumerable: true, | ||
value: 'value', | ||
writable: true, | ||
}, | ||
key2: { | ||
configurable: false, | ||
enumerable: false, | ||
value: 'value', | ||
writable: true, | ||
}, | ||
} | ||
); | ||
``` | ||
|
||
[package-url]: https://npmjs.org/package/define-data-property | ||
[npm-version-svg]: https://versionbadg.es/ljharb/define-data-property.svg | ||
[deps-svg]: https://david-dm.org/ljharb/define-data-property.svg | ||
[deps-url]: https://david-dm.org/ljharb/define-data-property | ||
[dev-deps-svg]: https://david-dm.org/ljharb/define-data-property/dev-status.svg | ||
[dev-deps-url]: https://david-dm.org/ljharb/define-data-property#info=devDependencies | ||
[npm-badge-png]: https://nodei.co/npm/define-data-property.png?downloads=true&stars=true | ||
[license-image]: https://img.shields.io/npm/l/define-data-property.svg | ||
[license-url]: LICENSE | ||
[downloads-image]: https://img.shields.io/npm/dm/define-data-property.svg | ||
[downloads-url]: https://npm-stat.com/charts.html?package=define-data-property | ||
[codecov-image]: https://codecov.io/gh/ljharb/define-data-property/branch/main/graphs/badge.svg | ||
[codecov-url]: https://app.codecov.io/gh/ljharb/define-data-property/ | ||
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/define-data-property | ||
[actions-url]: https://github.com/ljharb/define-data-property/actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
var hasPropertyDescriptors = require('has-property-descriptors')(); | ||
|
||
var GetIntrinsic = require('get-intrinsic'); | ||
|
||
var $defineProperty = hasPropertyDescriptors && GetIntrinsic('%Object.defineProperty%', true); | ||
|
||
var $SyntaxError = GetIntrinsic('%SyntaxError%'); | ||
var $TypeError = GetIntrinsic('%TypeError%'); | ||
|
||
var gopd = require('gopd'); | ||
|
||
/** @type {(obj: Record<PropertyKey, unknown>, property: PropertyKey, value: unknown, nonEnumerable?: boolean, nonWritable?: boolean, nonConfigurable?: boolean) => void} */ | ||
module.exports = function defineDataProperty( | ||
obj, | ||
property, | ||
value | ||
) { | ||
if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) { | ||
throw new $TypeError('`obj` must be an object or a function`'); | ||
} | ||
if (typeof property !== 'string' && typeof property !== 'symbol') { | ||
throw new $TypeError('`property` must be a string or a symbol`'); | ||
} | ||
if (arguments.length > 3 && typeof arguments[3] !== 'boolean') { | ||
throw new $TypeError('`nonEnumerable`, if provided, must be a boolean`'); | ||
} | ||
if (arguments.length > 4 && typeof arguments[4] !== 'boolean') { | ||
throw new $TypeError('`nonWritable`, if provided, must be a boolean`'); | ||
} | ||
if (arguments.length > 5 && typeof arguments[5] !== 'boolean') { | ||
throw new $TypeError('`nonConfigurable`, if provided, must be a boolean`'); | ||
} | ||
|
||
var nonEnumerable = arguments.length > 3 ? arguments[3] : null; | ||
var nonWritable = arguments.length > 4 ? arguments[4] : null; | ||
var nonConfigurable = arguments.length > 5 ? arguments[5] : null; | ||
|
||
/* @type {false | TypedPropertyDescriptor<unknown>} */ | ||
var desc = !!gopd && gopd(obj, property); | ||
|
||
if ($defineProperty) { | ||
$defineProperty(obj, property, { | ||
configurable: nonConfigurable === null && desc ? desc.configurable : !nonConfigurable, | ||
enumerable: nonEnumerable === null && desc ? desc.enumerable : !nonEnumerable, | ||
value: value, | ||
writable: nonWritable === null && desc ? desc.writable : !nonWritable | ||
}); | ||
} else if (!nonEnumerable && !nonWritable && !nonConfigurable) { | ||
// must fall back to [[Set]], and was not explicitly asked to make non-enumerable, non-writable, or non-configurable | ||
obj[property] = value; // eslint-disable-line no-param-reassign | ||
} else { | ||
throw new $SyntaxError('This environment does not support defining a property as non-configurable, non-writable, or non-enumerable.'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.