Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 28, 2018
1 parent 65e9245 commit a05f147
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
unherit.js
unherit.min.js
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
'use strict';
'use strict'

/* Dependencies. */
var xtend = require('xtend');
var inherits = require('inherits');
var xtend = require('xtend')
var inherits = require('inherits')

/* Expose. */
module.exports = unherit;
module.exports = unherit

/* Create a custom constructor which can be modified
* without affecting the original class. */
function unherit(Super) {
var result;
var key;
var value;
var result
var key
var value

inherits(Of, Super);
inherits(From, Of);
inherits(Of, Super)
inherits(From, Of)

/* Clone values. */
result = Of.prototype;
result = Of.prototype

for (key in result) {
value = result[key];
value = result[key]

if (value && typeof value === 'object') {
result[key] = 'concat' in value ? value.concat() : xtend(value);
result[key] = 'concat' in value ? value.concat() : xtend(value)
}
}

return Of;
return Of

/* Constructor accepting a single argument,
* which itself is an `arguments` object. */
function From(parameters) {
return Super.apply(this, parameters);
return Super.apply(this, parameters)
}

/* Constructor accepting variadic arguments. */
function Of() {
if (!(this instanceof Of)) {
return new From(arguments);
return new From(arguments)
}

return Super.apply(this, arguments);
return Super.apply(this, arguments)
}
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,31 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.0",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s unherit > unherit.js",
"build-mangle": "esmangle unherit.js > unherit.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
Expand Down
13 changes: 7 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ npm install unherit
## Usage

```js
var EventEmitter = require('events').EventEmitter;
var EventEmitter = require('events').EventEmitter
var unherit = require('unherit')

/* Create a private class which acts just like
* `EventEmitter`. */
var Emitter = unherit(EventEmitter);
var Emitter = unherit(EventEmitter)

Emitter.prototype.defaultMaxListeners = 0;
Emitter.prototype.defaultMaxListeners = 0
/* Now, all instances of `Emitter` have no maximum
* listeners, without affecting other `EventEmitter`s. */

assert(new Emitter().defaultMaxListeners === 0); // true
assert(new EventEmitter().defaultMaxListeners === undefined); // true
assert(new Emitter() instanceof EventEmitter); // true
new Emitter().defaultMaxListeners === 0 // => true
new EventEmitter().defaultMaxListeners === undefined // => true
new Emitter() instanceof EventEmitter // => true
```

## API
Expand Down
82 changes: 43 additions & 39 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,82 @@
'use strict';
'use strict'

var EventEmitter = require('events').EventEmitter;
var test = require('tape');
var unherit = require('.');
var EventEmitter = require('events').EventEmitter
var test = require('tape')
var unherit = require('.')

test('unherit(Super)', function (t) {
var Emitter = unherit(EventEmitter);
test('unherit(Super)', function(t) {
var Emitter = unherit(EventEmitter)

t.equal(Emitter.prototype.defaultMaxListeners, undefined);
t.equal(Emitter.prototype.defaultMaxListeners, undefined)

Emitter.prototype.defaultMaxListeners = 0;
Emitter.prototype.defaultMaxListeners = 0

t.equal(new Emitter().defaultMaxListeners, 0, 'should work (1)');
t.equal(new EventEmitter().defaultMaxListeners, undefined, 'should work (2)');
t.equal(new Emitter().defaultMaxListeners, 0, 'should work (1)')
t.equal(new EventEmitter().defaultMaxListeners, undefined, 'should work (2)')

t.equal(new Emitter().constructor, Emitter, 'should work (3)');
t.equal(new EventEmitter().constructor, EventEmitter, 'should work (4)');
t.equal(new Emitter().constructor, Emitter, 'should work (3)')
t.equal(new EventEmitter().constructor, EventEmitter, 'should work (4)')

Emitter = unherit(EventEmitter);
Emitter = unherit(EventEmitter)

t.ok(new Emitter() instanceof EventEmitter, 'should fool `instanceof` checks');
t.ok(new Emitter() instanceof EventEmitter, 'should fool `instanceof` checks')

/* Constructor which internally uses an `instanceof`
* check. */
function A(one, two, three) {
t.equal(one, 'foo');
t.equal(two, 'bar');
t.equal(three, 'baz');
t.ok(this instanceof A);
t.equal(one, 'foo')
t.equal(two, 'bar')
t.equal(three, 'baz')
t.ok(this instanceof A)

this.values = [].slice.call(arguments);
this.values = [].slice.call(arguments)
}

var B = unherit(A);
var B = unherit(A)

// eslint-disable-next-line new-cap
var b = B('foo', 'bar', 'baz');
var b = B('foo', 'bar', 'baz')

t.ok(b instanceof A, 'should fool `instanceof` without `new` (1)');
t.ok(b instanceof B, 'should fool `instanceof` without `new` (2)');
t.ok(b instanceof A, 'should fool `instanceof` without `new` (1)')
t.ok(b instanceof B, 'should fool `instanceof` without `new` (2)')

t.deepEqual(
b.values,
['foo', 'bar', 'baz'],
'should fool `instanceof` without `new` (3)'
);
)

var E;
var F;
var E
var F

function C() {}

C.prototype.values = [1, 2];
C.prototype.values = [1, 2]

function Proto() {}

function D() {}
Proto.prototype = C.prototype;
Proto.prototype = C.prototype

D.prototype = new Proto();
D.prototype.values = [1, 2, 3];
D.prototype = new Proto()
D.prototype.values = [1, 2, 3]

E = unherit(D);
E = unherit(D)

/* This failed in 1.0.4 */
t.deepEqual(E.prototype.values, [1, 2, 3], 'shouldn’t fail on inheritance (1)');
t.deepEqual(new E().values, [1, 2, 3], 'shouldn’t fail on inheritance (2)');
t.deepEqual(
E.prototype.values,
[1, 2, 3],
'shouldn’t fail on inheritance (1)'
)
t.deepEqual(new E().values, [1, 2, 3], 'shouldn’t fail on inheritance (2)')

E.prototype.values.push(4);
E.prototype.values.push(4)

F = unherit(D);
F = unherit(D)

t.deepEqual(F.prototype.values, [1, 2, 3], 'shouldn clone values (1)');
t.deepEqual(new F().values, [1, 2, 3], 'shouldn clone values (2)');
t.deepEqual(F.prototype.values, [1, 2, 3], 'shouldn clone values (1)')
t.deepEqual(new F().values, [1, 2, 3], 'shouldn clone values (2)')

t.end();
});
t.end()
})

0 comments on commit a05f147

Please sign in to comment.