Skip to content

Commit b26af7e

Browse files
committed
Use ESM
1 parent bd1df13 commit b26af7e

File tree

6 files changed

+41
-60
lines changed

6 files changed

+41
-60
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
unherit.js
7-
unherit.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
unherit.js
3-
unherit.min.js
4-
*.json
52
*.md

index.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
'use strict'
2-
3-
var xtend = require('xtend')
4-
var inherits = require('inherits')
5-
6-
module.exports = unherit
1+
import inherits from 'inherits'
72

83
// Create a custom constructor which can be modified without affecting the
94
// original class.
10-
function unherit(Super) {
11-
var result
5+
export function unherit(Super) {
6+
var proto
127
var key
138
var value
149

1510
inherits(Of, Super)
1611
inherits(From, Of)
1712

1813
// Clone values.
19-
result = Of.prototype
14+
proto = Of.prototype
2015

21-
for (key in result) {
22-
value = result[key]
16+
// We specifically want to get *all* fields, not just own fields.
17+
// eslint-disable-next-line guard-for-in
18+
for (key in proto) {
19+
value = proto[key]
2320

2421
if (value && typeof value === 'object') {
25-
result[key] = 'concat' in value ? value.concat() : xtend(value)
22+
proto[key] = 'concat' in value ? value.concat() : Object.assign({}, value)
2623
}
2724
}
2825

@@ -36,10 +33,8 @@ function unherit(Super) {
3633

3734
// Constructor accepting variadic arguments.
3835
function Of() {
39-
if (!(this instanceof Of)) {
40-
return new From(arguments)
41-
}
42-
43-
return Super.apply(this, arguments)
36+
return this instanceof Of
37+
? Super.apply(this, arguments)
38+
: new From(arguments)
4439
}
4540
}

package.json

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,28 @@
1919
"contributors": [
2020
"Titus Wormer <[email protected]> (https://wooorm.com)"
2121
],
22+
"sideEffects": false,
23+
"type": "module",
24+
"main": "index.js",
2225
"files": [
2326
"index.js"
2427
],
2528
"dependencies": {
26-
"inherits": "^2.0.0",
27-
"xtend": "^4.0.0"
29+
"inherits": "^2.0.0"
2830
},
2931
"devDependencies": {
30-
"browserify": "^17.0.0",
31-
"nyc": "^15.0.0",
32+
"c8": "^7.0.0",
3233
"prettier": "^2.0.0",
3334
"remark-cli": "^9.0.0",
3435
"remark-preset-wooorm": "^8.0.0",
3536
"tape": "^5.0.0",
36-
"tinyify": "^3.0.0",
3737
"xo": "^0.38.0"
3838
},
3939
"scripts": {
4040
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
41-
"build-bundle": "browserify . -s unherit -o unherit.js",
42-
"build-mangle": "browserify . -s unherit -p tinyify -o unherit.min.js",
43-
"build": "npm run build-bundle && npm run build-mangle",
4441
"test-api": "node test",
45-
"test-coverage": "nyc --reporter lcov tape test.js",
46-
"test": "npm run format && npm run build && npm run test-coverage"
42+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
43+
"test": "npm run format && npm run test-coverage"
4744
},
4845
"prettier": {
4946
"tabWidth": 2,
@@ -55,14 +52,11 @@
5552
},
5653
"xo": {
5754
"prettier": true,
58-
"esnext": false,
5955
"rules": {
60-
"unicorn/prefer-reflect-apply": "off",
61-
"guard-for-in": "off"
62-
},
63-
"ignores": [
64-
"unherit.js"
65-
]
56+
"no-var": "off",
57+
"prefer-arrow-callback": "off",
58+
"unicorn/prefer-reflect-apply": "off"
59+
}
6660
},
6761
"remarkConfig": {
6862
"plugins": [

readme.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class.
1010

1111
## Install
1212

13+
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
14+
instead of `require`d.
15+
1316
[npm][]:
1417

1518
```sh
@@ -19,8 +22,8 @@ npm install unherit
1922
## Use
2023

2124
```js
22-
var EventEmitter = require('events').EventEmitter
23-
var unherit = require('unherit')
25+
import {EventEmitter} from 'events'
26+
import {unherit} from 'unherit'
2427

2528
// Create a private class which acts just like `EventEmitter`.
2629
var Emitter = unherit(EventEmitter)
@@ -36,19 +39,13 @@ new Emitter() instanceof EventEmitter // => true
3639

3740
## API
3841

39-
### `unherit(Super)`
40-
41-
Create a custom constructor which can be modified without affecting the original
42-
class.
43-
44-
###### Parameters
42+
This package exports the following identifiers: `unherit`.
43+
There is no default export.
4544

46-
* `Super` (`Function`) — Super-class
47-
48-
###### Returns
45+
### `unherit(Super)`
4946

50-
`Function` — Constructor acting like `Super`, which can be modified without
51-
affecting the original class.
47+
Create a custom constructor (`Function`) from `Super` (`Function`) which can be
48+
modified without affecting the original class.
5249

5350
## License
5451

test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
var EventEmitter = require('events').EventEmitter
4-
var test = require('tape')
5-
var unherit = require('.')
1+
import {EventEmitter} from 'events'
2+
import test from 'tape'
3+
import {unherit} from './index.js'
64

75
test('unherit(Super)', function (t) {
86
var Emitter = unherit(EventEmitter)
@@ -59,6 +57,7 @@ test('unherit(Super)', function (t) {
5957

6058
D.prototype = new Proto()
6159
D.prototype.values = [1, 2, 3]
60+
D.prototype.object = {a: true}
6261

6362
E = unherit(D)
6463

@@ -74,8 +73,10 @@ test('unherit(Super)', function (t) {
7473

7574
F = unherit(D)
7675

77-
t.deepEqual(F.prototype.values, [1, 2, 3], 'shouldn clone values (1)')
78-
t.deepEqual(new F().values, [1, 2, 3], 'shouldn clone values (2)')
76+
t.deepEqual(F.prototype.values, [1, 2, 3], 'should clone values (1)')
77+
t.deepEqual(new F().values, [1, 2, 3], 'should clone values (2)')
78+
79+
t.deepEqual(new F().object, {a: true}, 'should clone values (3)')
7980

8081
t.end()
8182
})

0 commit comments

Comments
 (0)