Comparing version 0.2.0 to 1.0.0
@@ -11,3 +11,3 @@ var copy = require('./copy.js'); | ||
* @param {...object} from - object(s) to merge with | ||
* @returns {object} mixed result object | ||
* @returns {object} - mixed result object | ||
*/ | ||
@@ -14,0 +14,0 @@ function mixAppend(to) |
@@ -7,6 +7,5 @@ // Public API | ||
* | ||
* @private | ||
* @param {function} to - source object to copy properties to | ||
* @param {function} from - source object to copy properties from | ||
* @returns {function} – augmented function | ||
* @param {object} to - source object to copy properties to | ||
* @param {object} from - source object to copy properties from | ||
* @returns {object} – augmented `to` object | ||
*/ | ||
@@ -13,0 +12,0 @@ function mixCopy(to, from) |
@@ -15,2 +15,4 @@ var chain = require('./chain.js') | ||
* | ||
* Note: *Destroys target's prototype* | ||
* | ||
* @param {function} target - Function (class) to update prototype on | ||
@@ -17,0 +19,0 @@ * @param {function} super_ - Superclass to be prototype donor |
@@ -7,3 +7,3 @@ var append = require('./append.js'); | ||
/** | ||
* Creates immutable (shallow) copy of the provided properties | ||
* Creates immutable (shallow) copy of the provided objects | ||
* | ||
@@ -10,0 +10,0 @@ * @param {...object} from - object(s) to merge with |
23
index.js
@@ -1,8 +0,10 @@ | ||
var copy = require('./copy.js') | ||
var mixin = require('./mixin.js') | ||
, append = require('./append.js') | ||
, chain = require('./chain.js') | ||
, clone = require('./clone.js') | ||
, copy = require('./copy.js') | ||
, extend = require('./extend.js') | ||
, append = require('./append.js') | ||
, funky = require('./funky.js') | ||
, immutable = require('./immutable.js') | ||
, inherit = require('./inherit.js') | ||
, immutable = require('./immutable.js') | ||
; | ||
@@ -12,11 +14,12 @@ | ||
// use clone (property copying) mixin by default | ||
module.exports = clone; | ||
// use mixin (property copying to prototypes) as default function | ||
module.exports = mixin; | ||
// expose everything with explicit names | ||
module.exports.append = append; | ||
module.exports.chain = chain; | ||
module.exports.copy = copy; | ||
module.exports.chain = chain; | ||
module.exports.clone = clone; | ||
module.exports.extend = extend; | ||
module.exports.append = append; | ||
module.exports.funky = funky; | ||
module.exports.immutable = immutable; | ||
module.exports.inherit = inherit; | ||
module.exports.immutable = immutable; |
@@ -9,3 +9,3 @@ // Public API | ||
* | ||
* *Destroys target's prototype* | ||
* Note: *Destroys target's prototype* | ||
* | ||
@@ -12,0 +12,0 @@ * @param {function} target - Function (class) to update prototype on |
{ | ||
"name": "mixly", | ||
"version": "0.2.0", | ||
"description": "Mixins for prototype inheritance, works with both objects and functions.", | ||
"version": "1.0.0", | ||
"description": "Collection of mixin tools for objects and functions", | ||
"main": "index.js", | ||
"scripts": { | ||
"clean": "rimraf coverage", | ||
"lint": "eslint *.js test/*.js", | ||
"test": "nyc --reporter=lcov --reporter=text tape ./test/*.js | tap-dot", | ||
"posttest": "nyc check-coverage --lines 98 --functions 100 --branches 98", | ||
"browser": "browserify test/*.js | ghostface | tap-dot" | ||
"cover": "istanbul cover --reporter=json tape -- test/*.js | tap-spec", | ||
"test": "tape test/*.js | tap-spec", | ||
"browser": "browserify -t browserify-istanbul test/*.js | obake --coverage | tap-spec", | ||
"report": "istanbul report lcov", | ||
"size": "browserify index.js | size-table mixly", | ||
"toc": "toc-md README.md", | ||
"files": "pkgfiles --sort=name" | ||
}, | ||
"pre-commit": [ | ||
"clean", | ||
"lint", | ||
"test", | ||
"browser" | ||
"cover", | ||
"browser", | ||
"report", | ||
"size", | ||
"toc" | ||
], | ||
@@ -29,3 +38,4 @@ "engines": { | ||
"objects", | ||
"functions" | ||
"functions", | ||
"collection" | ||
], | ||
@@ -39,15 +49,21 @@ "author": "Alex Indigo <iam@alexindigo.com>", | ||
"devDependencies": { | ||
"browserify": "^13.0.0", | ||
"codacy-coverage": "^1.1.3", | ||
"coveralls": "^2.11.6", | ||
"eslint": "^2.2.0", | ||
"ghostface": "^1.5.0", | ||
"nyc": "^5.6.0", | ||
"phantomjs-prebuilt": "^2.1.4", | ||
"pre-commit": "^1.1.2", | ||
"browserify": "^13.1.0", | ||
"browserify-istanbul": "^2.0.0", | ||
"coveralls": "^2.11.12", | ||
"eslint": "^2.13.1", | ||
"istanbul": "^0.4.5", | ||
"obake": "^0.1.2", | ||
"phantomjs-prebuilt": "^2.1.12", | ||
"pkgfiles": "^2.3.0", | ||
"pre-commit": "^1.1.3", | ||
"reamde": "^1.1.0", | ||
"tap-dot": "^1.0.4", | ||
"tap-nyan": "0.0.2", | ||
"tape": "^4.4.0" | ||
"rimraf": "^2.5.4", | ||
"size-table": "^0.2.0", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.6.0", | ||
"toc-md": "^0.2.0" | ||
}, | ||
"dependencies": { | ||
"fulcon": "^1.0.1" | ||
} | ||
} |
318
README.md
# mixly [](https://www.npmjs.com/package/mixly) | ||
Mixins for prototype inheritance, works with both objects and functions. | ||
Collection of mixin tools for objects and functions. | ||
@@ -14,12 +14,320 @@ [](https://travis-ci.org/alexindigo/mixly) | ||
| compression | size | | ||
| :-------------- | ------: | | ||
| mixly.js | 8.94 kB | | ||
| mixly.min.js | 2.93 kB | | ||
| mixly.min.js.gz | 964 B | | ||
## Table of Contents | ||
<!-- TOC --> | ||
- [Install](#install) | ||
- [API](#api) | ||
- [mixly](#mixly) | ||
- [append](#append) | ||
- [chain](#chain) | ||
- [copy](#copy) | ||
- [extend](#extend) | ||
- [funky](#funky) | ||
- [immutable](#immutable) | ||
- [inherit](#inherit) | ||
- [License](#license) | ||
<!-- TOC END --> | ||
## Install | ||
```shell | ||
$ npm install --save mixly | ||
``` | ||
$ npm install mixly --save | ||
## API | ||
### mixly | ||
_`mixly(object[, object[, ...object]])`_ | ||
Alias: _`mixly/mixin`_ | ||
Creates prototype chain with the properties from the provided objects, by (shallow) copying own properties from each object onto respective elements in the chain. | ||
```javascript | ||
var mixly = require('mixly'); | ||
var o1 = { O1: true, commonThing: 'o1' } | ||
, o2 = { O2: true, commonThing: 'o2' } | ||
, o3 = { O3: true, commonThing: 'o3' } | ||
; | ||
// (o1) -> (o2) -> (o3) | ||
var o0 = mixly(o1, o2, o3); | ||
assert.notStrictEqual(Object.getPrototypeOf(o0), o1, 'Object `o0` does not have prototype set to object `o1`'); | ||
assert.notStrictEqual(Object.getPrototypeOf(o1), o2, 'Object `o1` does not have prototype set to object `o2`'); | ||
assert.notStrictEqual(Object.getPrototypeOf(o2), o3, 'Object `o2` does not have prototype set to object `o3`'); | ||
assert.strictEqual(o0.O1, true, 'copied properties from the first object'); | ||
assert.strictEqual(o0.O2, true, 'has access to the properties of the second object'); | ||
assert.strictEqual(o0.O3, true, 'has access to the properties of the third object'); | ||
assert.strictEqual(o0.commonThing, 'o1', 'shared properties from first object "win"'); | ||
assert.strictEqual(Object.getPrototypeOf(o0).commonThing, 'o2', 'has access to the shared properties of the second object'); | ||
assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(o0)).commonThing, 'o3', 'has access to the shared properties of the third object'); | ||
assert.strictEqual(o0.hasOwnProperty('commonThing'), true, 'has shared properties from first object as own'); | ||
assert.strictEqual(o0.hasOwnProperty('O1'), true, 'has shared properties from first object as own'); | ||
assert.strictEqual(o0.hasOwnProperty('O2'), false, 'does not own properties from the second object'); | ||
assert.strictEqual(o0.hasOwnProperty('O3'), false, 'does not own properties from the third object'); | ||
``` | ||
## Examples | ||
More details in [test/mixin.js](test/mixin.js) test file. | ||
TBW. | ||
### append | ||
Meanwhile please check [test/](test/) folder. | ||
_`mixly.append(object[, object[, ...object]])`_ | ||
Aliases: _`mixly/append`_, _`mixly/flat`_ | ||
Appends objects' properties (shallow copy) into the first object. | ||
```javascript | ||
var append = require('mixly/append'); | ||
var o1 = { O1: true, commonThing: 'o1' } | ||
, o2 = { O2: true, commonThing: 'o2' } | ||
, o3 = { O3: true, commonThing: 'o3' } | ||
; | ||
// o1 + o2 + o3 | ||
var oX = append(o1, o2, o3); | ||
assert.strictEqual(oX, o1, 'first argument was modified'); | ||
assert.strictEqual(oX.O1, true, 'kept properties from the first object'); | ||
assert.strictEqual(oX.O2, true, 'obtained properties from the second object'); | ||
assert.strictEqual(oX.O3, true, 'obtained properties from the third object'); | ||
assert.strictEqual(oX.commonThing, 'o3', 'last object in the list overrides shared properties'); | ||
``` | ||
More details in [test/append.js](test/append.js) test file. | ||
### chain | ||
_`mixly.chain(object[, object[, ...object]])`_ | ||
Aliases: _`mixly/chain`_, _`mixly/proto`_ | ||
Modifies prototype chain for the provided objects, based on the order of the arguments. | ||
```javascript | ||
var chain = require('mixly/chain'); | ||
var o1 = { O1: true, commonThing: 'o1' } | ||
, o2 = { O2: true, commonThing: 'o2' } | ||
, o3 = { O3: true, commonThing: 'o3' } | ||
; | ||
// o1 -> o2 -> o3 | ||
mixly.chain(o1, o2, o3); | ||
assert.strictEqual(Object.getPrototypeOf(o1), o2, 'Object `o1` has prototype set to object `o2`'); | ||
assert.strictEqual(Object.getPrototypeOf(o2), o3, 'Object `o2` has prototype set to object `o3`'); | ||
assert.strictEqual(o1.O1, true, 'kept properties from the first object'); | ||
assert.strictEqual(o1.O2, true, 'has access to the properties of the second object'); | ||
assert.strictEqual(o1.O3, true, 'has access to the properties of the third object'); | ||
assert.strictEqual(o1.commonThing, 'o1', 'kept own shared properties'); | ||
assert.strictEqual(Object.getPrototypeOf(o1).commonThing, 'o2', 'has access to the shared properties of the second object'); | ||
assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(o1)).commonThing, 'o3', 'has access to the shared properties of the third object'); | ||
assert.strictEqual(o1.hasOwnProperty('commonThing'), true, 'kept own shared properties'); | ||
assert.strictEqual(o1.hasOwnProperty('O1'), true, 'kept own unique properties'); | ||
assert.strictEqual(o1.hasOwnProperty('O2'), false, 'does not own properties from the second object'); | ||
assert.strictEqual(o1.hasOwnProperty('O3'), false, 'does not own properties from the third object'); | ||
``` | ||
More details in [test/chain.js](test/chain.js) test file. | ||
### copy | ||
_`mixly.copy(object, object)`_ | ||
Alias: _`mixly/copy`_ | ||
Copies (shallow) own properties between provided objects. _Used internally by other `mixly` methods._ | ||
```javascript | ||
var copy = require('mixly/copy'); | ||
var o1 = { O1: true, commonThing: 'o1' } | ||
, o2 = { O2: true, commonThing: 'o2' } | ||
; | ||
// o1 + o2 | ||
var oX = copy(o1, o2); | ||
assert.strictEqual(oX, o1, 'first argument was modified'); | ||
assert.strictEqual(oX.O1, true, 'kept properties from the first object'); | ||
assert.strictEqual(oX.O2, true, 'obtained properties from the second object'); | ||
assert.strictEqual(oX.commonThing, 'o2', 'last object in the list overrides shared properties'); | ||
``` | ||
More details in [test/copy.js](test/copy.js) test file. | ||
### extend | ||
_`mixly.extend(function, function)`_ | ||
Alias: _`mixly/extend`_ | ||
Extends target class with superclass, assigns superclass prototype as prototype of the target class and adds superclass itself as `__proto__` of the target class, allowing "static" methods inheritance. | ||
```javascript | ||
var extend = require('mixly/extend'); | ||
function F1() | ||
{ | ||
F1.super_.apply(this, arguments); | ||
this.p0 += 'd2'; | ||
} | ||
F1.prototype.p1 = 'f1'; | ||
F1.static1 = true; | ||
function F2() | ||
{ | ||
this.p0 = 'r2'; | ||
} | ||
F2.prototype.p2 = 'f2'; | ||
F2.static2 = true; | ||
// F1 -> F2 | ||
extend(F1, F2); | ||
assert.strictEqual('p1' in F1.prototype, false, 'original prototype is gone'); | ||
assert.strictEqual('p2' in F1.prototype, true, 'replaced with new prototype'); | ||
assert.strictEqual(F1.static1, true, 'original static property is accessible'); | ||
assert.strictEqual(F1.static2, true, 'extended static property is accessible'); | ||
// new instance | ||
var f1 = new F1(); | ||
assert.strictEqual(f1.p0, 'r2d2', 'constructor executes provided super constructor'); | ||
``` | ||
More details in [test/extend.js](test/extend.js) test file. | ||
### funky | ||
_`mixly.funky(function[, function[, ...function]])`_ | ||
Alias: _`mixly/funky`_ | ||
Creates prototype chain from the provided functions, by (shallow) copying prototypes from each function onto respective elements in the chain. | ||
```javascript | ||
var funky = require('mixly/funky'); | ||
function F1() | ||
{ | ||
this.super_.apply(this, arguments); | ||
this.f1 = true; | ||
} | ||
F1.prototype.f1p = true; | ||
function F2() | ||
{ | ||
// this.super_ is reference to itself | ||
// we're in the f0 context | ||
// don't call super_.super_ | ||
this.f2 = true; | ||
} | ||
F2.prototype.f2p = true; | ||
function F3() | ||
{ | ||
// never gets here | ||
this.f3 = true; | ||
} | ||
F3.prototype.f3p = true; | ||
// F0 -> (F1) -> (F2) -> (F3) | ||
var F0 = mixly.clone(F1, F2, F3); | ||
var f0 = new F0(); | ||
assert.notStrictEqual(F0.prototype, F1.prototype, 'is not exactly F1'); | ||
assert.strictEqual(F0.prototype.f1p, true, 'but close'); | ||
F0.prototype.bla = 42; | ||
assert.strictEqual('bla' in F1.prototype, false, 'F1 stays untouched'); | ||
assert.strictEqual(f0.f1, true, 'executed F1 constructor'); | ||
assert.strictEqual(f0.f2, true, 'executed F2 constructor'); | ||
assert.strictEqual('f3' in f0, false, 'skipped F3 constructor'); | ||
assert.strictEqual(f0.f1p, true, 'inherited F1 prototype properties'); | ||
assert.strictEqual(f0.f2p, true, 'inherited F2 prototype properties'); | ||
assert.strictEqual(f0.f3p, true, 'inherited F3 prototype properties'); | ||
``` | ||
More details in [test/funky.js](test/funky.js) test file. | ||
### immutable | ||
_`mixly.immutable(object[, object[, ...object]])`_ | ||
Alias: _`mixly/immutable`_ | ||
Creates immutable (shallow) copy of the provided objects. Similar to [`append`](#append), but doesn't modify any of the provided objects. | ||
```javascript | ||
var immutable = require('mixly/immutable'); | ||
var o1 = { O1: true, commonThing: 'o1' } | ||
, o2 = { O2: true, commonThing: 'o2' } | ||
, o3 = { O3: true, commonThing: 'o3' } | ||
; | ||
// oX + o1 + o2 + o3 | ||
var oX = immutable(o1, o2, o3); | ||
assert.notStrictEqual(oX, o1, 'first argument was not modified'); | ||
assert.strictEqual(oX.O1, true, 'kept properties from the first object'); | ||
assert.strictEqual(oX.O2, true, 'obtained properties from the second object'); | ||
assert.strictEqual(oX.O3, true, 'obtained properties from the third object'); | ||
assert.strictEqual(oX.commonThing, 'o3', 'last object in the list overrides shared properties'); | ||
``` | ||
More details in [test/immutable.js](test/immutable.js) test file. | ||
### inherit | ||
_`mixly.inherit(function, function)`_ | ||
Alias: _`mixly/inherit`_ | ||
Assigns prototype from the superclass to the target class, compatible with node's builtin version (`util.inherit`), but browser-friendly (without browserify magic, i.e. works with other packagers). | ||
```javascript | ||
var inherit = require('mixly/inherit'); | ||
function Child() { Parent.call(this); } | ||
function Parent() {} | ||
// Child -> Parent | ||
inherit(Child, Parent); | ||
// create instance | ||
var child = new Child(); | ||
assert.strictEqual(child.constructor.super_, Parent, 'child has reference to the Parent constructor'); | ||
assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(child)), Parent.prototype, 'child has Parent in the prototype chain'); | ||
assert.strictEqual(child instanceof Child, true, 'child instance of Child'); | ||
assert.strictEqual(child instanceof Parent, true, 'child instance of Parent'); | ||
``` | ||
More details in [test/inherit.js](test/inherit.js) test file. | ||
## License | ||
Mixly is released under the [MIT](LICENSE) license. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
21443
14
281
0
333
1
15
2
+ Addedfulcon@^1.0.1
+ Addedfulcon@1.0.2(transitive)