Skip to content

Commit

Permalink
Initial implementation, tests, readme, types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 30, 2024
1 parent deffdae commit 7036fc9
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .eslintrc
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",
},
}
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
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']
12 changes: 12 additions & 0 deletions .github/workflows/node-aught.yml
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
7 changes: 7 additions & 0 deletions .github/workflows/node-pretest.yml
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
14 changes: 14 additions & 0 deletions .github/workflows/node-tens.yml
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
14 changes: 14 additions & 0 deletions .github/workflows/node-twenties.yml
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
9 changes: 9 additions & 0 deletions .github/workflows/rebase.yml
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 }}
12 changes: 12 additions & 0 deletions .github/workflows/require-allow-edits.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ dist
npm-shrinkwrap.json
package-lock.json
yarn.lock

.npmignore
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package-lock=false
allow-same-version=true
message=v%s
9 changes: 9 additions & 0 deletions .nycrc
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"
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
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).
8 changes: 8 additions & 0 deletions Object.setPrototypeOf.d.ts
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;
6 changes: 6 additions & 0 deletions Object.setPrototypeOf.js
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;
52 changes: 51 additions & 1 deletion README.md
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
3 changes: 3 additions & 0 deletions Reflect.setPrototypeOf.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const x: typeof Reflect.setPrototypeOf | null;

export = x;
4 changes: 4 additions & 0 deletions Reflect.setPrototypeOf.js
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;
8 changes: 8 additions & 0 deletions index.d.ts
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;
25 changes: 25 additions & 0 deletions index.js
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
);
56 changes: 54 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
"main": "index.js",
"exports": {
".": "./index.js",
"./Reflect.setPrototypeOf": "./Reflect.setPrototypeOf.js",
"./Object.setPrototypeOf": "./Object.setPrototypeOf.js",
"./package.json": "./package.json"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prepublishOnly": "safe-publish-latest",
"pretest": "npm run --silent lint",
"test": "npm run tests-only",
"posttest": "npx npm@\">=10.2\" audit --production",
"tests-only": "nyc tape 'test/**/*.js'",
"prelint": "evalmd README.md",
"lint": "eslint --ext=js,mjs .",
"postlint": "tsc && attw -P",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"repository": {
"type": "git",
Expand All @@ -26,5 +39,44 @@
"bugs": {
"url": "https://github.com/ljharb/set-proto/issues"
},
"homepage": "https://github.com/ljharb/set-proto#readme"
"homepage": "https://github.com/ljharb/set-proto#readme",
"dependencies": {
"dunder-proto": "^1.0.1",
"es-errors": "^1.3.0",
"es-object-atoms": "^1.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.2",
"@ljharb/eslint-config": "^21.1.1",
"@ljharb/tsconfig": "^0.2.3",
"@types/tape": "^5.8.0",
"auto-changelog": "^2.5.0",
"eslint": "=8.8.0",
"evalmd": "^0.0.19",
"in-publish": "^2.0.1",
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.9.0",
"typescript": "next"
},
"engines": {
"node": ">= 0.4"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
},
"publishConfig": {
"ignore": [
".github/workflows"
]
},
"testling": {
"files": "test/index.js"
}
}
40 changes: 40 additions & 0 deletions test/index.js
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();
});
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@ljharb/tsconfig",
"compilerOptions": {
//"target": "es2021",
},
"exclude": [
"coverage",
],
}

0 comments on commit 7036fc9

Please sign in to comment.