-
-
Notifications
You must be signed in to change notification settings - Fork 1
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, types
- Loading branch information
Showing
22 changed files
with
317 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,10 @@ | ||
{ | ||
"root": true, | ||
|
||
"extends": "@ljharb", | ||
|
||
"rules": { | ||
"id-length": "off", | ||
"sort-keys": "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/set-proto | ||
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,12 @@ | ||
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 |
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,7 @@ | ||
name: 'Tests: pretest/posttest' | ||
|
||
on: [pull_request, push] | ||
|
||
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,14 @@ | ||
name: 'Tests: node.js 10 - 20' | ||
|
||
on: [pull_request, push] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '>= 10 < 20' | ||
type: minors | ||
command: npm run tests-only |
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,14 @@ | ||
name: 'Tests: node.js >= 20' | ||
|
||
on: [pull_request, push] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '>= 20' | ||
type: minors | ||
command: npm run tests-only |
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 |
---|---|---|
|
@@ -133,3 +133,5 @@ dist | |
npm-shrinkwrap.json | ||
package-lock.json | ||
yarn.lock | ||
|
||
.npmignore |
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,9 @@ | ||
{ | ||
"all": true, | ||
"check-coverage": false, | ||
"reporter": ["text-summary", "text", "html", "json"], | ||
"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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare function setProto<O extends object, P extends object | null>( | ||
object: O, | ||
proto: P, | ||
): O; | ||
|
||
declare const x: typeof setProto | null; | ||
|
||
export = x; |
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 @@ | ||
'use strict'; | ||
|
||
var $Object = require('es-object-atoms'); | ||
|
||
/** @type {import('./Object.setPrototypeOf')} */ | ||
module.exports = $Object.setPrototypeOf || null; |
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,52 @@ | ||
# set-proto | ||
# set-proto <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] | ||
|
||
Robustly set the [[Prototype]] of an object. Uses the best available method. | ||
|
||
## Getting started | ||
|
||
```sh | ||
npm install --save set-proto | ||
``` | ||
|
||
## Usage/Examples | ||
|
||
```js | ||
const assert = require('assert'); | ||
const setProto = require('set-proto'); | ||
|
||
const a = { a: 1, b: 2, [Symbol.toStringTag]: 'foo' }; | ||
const b = { c: 3 }; | ||
|
||
assert.ok(!('c' in a)); | ||
|
||
setProto(a, b); | ||
|
||
assert.ok('c' in a); | ||
``` | ||
|
||
## Tests | ||
|
||
Clone the repo, `npm install`, and run `npm test` | ||
|
||
[package-url]: https://npmjs.org/package/set-proto | ||
[npm-version-svg]: https://versionbadg.es/ljharb/set-proto.svg | ||
[deps-svg]: https://david-dm.org/ljharb/set-proto.svg | ||
[deps-url]: https://david-dm.org/ljharb/set-proto | ||
[dev-deps-svg]: https://david-dm.org/ljharb/set-proto/dev-status.svg | ||
[dev-deps-url]: https://david-dm.org/ljharb/set-proto#info=devDependencies | ||
[npm-badge-png]: https://nodei.co/npm/set-proto.png?downloads=true&stars=true | ||
[license-image]: https://img.shields.io/npm/l/set-proto.svg | ||
[license-url]: LICENSE | ||
[downloads-image]: https://img.shields.io/npm/dm/set-proto.svg | ||
[downloads-url]: https://npm-stat.com/charts.html?package=set-proto | ||
[codecov-image]: https://codecov.io/gh/ljharb/set-proto/branch/main/graphs/badge.svg | ||
[codecov-url]: https://app.codecov.io/gh/ljharb/set-proto/ | ||
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/set-proto | ||
[actions-url]: https://github.com/ljharb/set-proto/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,3 @@ | ||
declare const x: typeof Reflect.setPrototypeOf | null; | ||
|
||
export = x; |
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,4 @@ | ||
'use strict'; | ||
|
||
/** @type {import('./Reflect.setPrototypeOf')} */ | ||
module.exports = (typeof Reflect !== 'undefined' && Reflect.setPrototypeOf) || null; |
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,8 @@ | ||
declare function setProto<O extends object, P extends object | null>( | ||
object: O, | ||
proto: P, | ||
): O; | ||
|
||
declare const x: typeof setProto | null; | ||
|
||
export = x; |
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,25 @@ | ||
'use strict'; | ||
|
||
var reflectSetProto = require('./Reflect.setPrototypeOf'); | ||
var originalSetProto = require('./Object.setPrototypeOf'); | ||
|
||
var setDunderProto = require('dunder-proto/set'); | ||
|
||
var $TypeError = require('es-errors/type'); | ||
|
||
/** @type {import('.')} */ | ||
module.exports = reflectSetProto | ||
? function setProto(O, proto) { | ||
// @ts-expect-error TS can't narrow inside a closure, for some reason | ||
if (reflectSetProto(O, proto)) { | ||
return O; | ||
} | ||
throw new $TypeError('Reflect.setPrototypeOf: failed to set [[Prototype]]'); | ||
} | ||
: originalSetProto || ( | ||
setDunderProto ? function setProto(O, proto) { | ||
// @ts-expect-error TS can't narrow inside a closure, for some reason | ||
setDunderProto(O, proto); | ||
return O; | ||
} : null | ||
); |
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
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,40 @@ | ||
'use strict'; | ||
|
||
var test = require('tape'); | ||
|
||
var setProto = require('../'); | ||
|
||
var isPrototypeOf = Object.prototype.isPrototypeOf; | ||
|
||
test('setProto', function (t) { | ||
t.equal(typeof setProto, 'function', 'is a function'); | ||
|
||
t.test('can set', { skip: !setProto }, function (st) { | ||
var obj = { a: 1 }; | ||
var proto = { b: 2 }; | ||
|
||
st.ok(isPrototypeOf.call(Object.prototype, obj), 'Object.prototype is isPrototypeOf obj'); | ||
st.notOk(isPrototypeOf.call(proto, obj), 'proto is not isPrototypeOf obj'); | ||
st.ok('a' in obj, 'a is in obj'); | ||
st.notOk('b' in obj, 'b is not in obj'); | ||
|
||
// eslint-disable-next-line no-extra-parens | ||
st.equal(/** @type {NonNullable<typeof setProto>} */ (setProto)(obj, proto), obj, 'returns the object'); | ||
|
||
st.ok(isPrototypeOf.call(Object.prototype, obj), 'Object.prototype is isPrototypeOf obj'); | ||
st.ok(isPrototypeOf.call(proto, obj), 'proto is isPrototypeOf obj'); | ||
st.ok('a' in obj, 'a is in obj'); | ||
st.ok('b' in obj, 'b is in obj'); | ||
|
||
st.equal(Object.getPrototypeOf(obj), proto, 'sets the prototype'); | ||
st.end(); | ||
}); | ||
|
||
t.test('can not set', { skip: !!setProto }, function (st) { | ||
st.equal(setProto, null); | ||
|
||
st.end(); | ||
}); | ||
|
||
t.end(); | ||
}); |
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 @@ | ||
{ | ||
"extends": "@ljharb/tsconfig", | ||
"compilerOptions": { | ||
//"target": "es2021", | ||
}, | ||
"exclude": [ | ||
"coverage", | ||
], | ||
} |