core-decorators
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -44,2 +44,7 @@ 'use strict'; | ||
exports.debounce = _interopRequire(_debounce); | ||
exports.debounce = _interopRequire(_debounce); | ||
var _mixin = require('./mixin'); | ||
exports.mixin = _interopRequire(_mixin); | ||
exports.mixins = _interopRequire(_mixin); |
@@ -7,35 +7,35 @@ 'use strict'; | ||
exports['default'] = mixin; | ||
var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors; | ||
var defineProperty = Object.defineProperty; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | ||
function handleClass(target, mixins) { | ||
if (!mixins.length) { | ||
throw new SyntaxError('@mixin() class ' + target.name + ' requires at least one mixin as an argument'); | ||
} | ||
var _privateUtils = require('./private/utils'); | ||
for (var i = 0, l = mixins.length; i < l; i++) { | ||
var descs = getOwnPropertyDescriptors(mixins[i]); | ||
/** | ||
* This is not a complete, compliant polyfill | ||
* because it doesn't need to be. | ||
*/ | ||
var assign = Object.assign || function (target) { | ||
// Start off at index 1 because we want to skip the target | ||
for (var i = 1, l = arguments.length; i < l; i++) { | ||
var _mixin = arguments[i]; | ||
for (var key in _mixin) { | ||
target[key] = _mixin[key]; | ||
for (var key in descs) { | ||
if (!(key in target.prototype)) { | ||
defineProperty(target.prototype, key, descs[key]); | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
function handleDescriptor(target, mixins) { | ||
return assign.apply(undefined, [target.prototype].concat(_toConsumableArray(mixins))); | ||
} | ||
function mixin() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
for (var _len = arguments.length, mixins = Array(_len), _key = 0; _key < _len; _key++) { | ||
mixins[_key] = arguments[_key]; | ||
} | ||
return (0, _privateUtils.decorate)(handleDescriptor, args); | ||
if (typeof mixins[0] === 'function') { | ||
return handleClass(mixins[0], []); | ||
} else { | ||
return function (target) { | ||
return handleClass(target, mixins); | ||
}; | ||
} | ||
} | ||
module.exports = exports['default']; |
{ | ||
"name": "core-decorators", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Library of ES7 decorators inspired by languages that come with built-ins like @override, @deprecated, etc", | ||
@@ -5,0 +5,0 @@ "main": "lib/core-decorators.js", |
# core-decorators.js [![Build Status](https://travis-ci.org/jayphelps/core-decorators.js.svg?branch=master)](https://travis-ci.org/jayphelps/core-decorators.js) | ||
Library of [ES2016 (ES7) decorators](https://github.com/wycats/javascript-decorators) inspired by languages that come with built-ins like @override, @deprecate, etc, similar to [pre-defined Annotations in Java](https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html). Note that unlike Java annotations, decorators are functions which are applied at runtime. | ||
It also includes a single class decorator, `@mixin` for applying object descriptors to a given class. | ||
### Get It | ||
@@ -17,2 +19,3 @@ | ||
##### For Methods | ||
* [@autobind](#autobind) | ||
@@ -28,2 +31,6 @@ * [@readonly](#readonly) | ||
##### For Classes | ||
* [@mixin](#mixin) | ||
##### Proposed (not implemented, PRs welcome!): | ||
@@ -234,1 +241,32 @@ * @mixin | ||
Initial implementation included, likely slow. WIP. | ||
### @mixin (alias: @mixins) | ||
Mixes in all property descriptors from the provided Plain Old JavaScript Objects (aka POJOs) as arguments. Mixins are applied in the order they are passed, but do **not** overload descriptors originally on the class, included those inherited traditionally. | ||
```js | ||
import { mixin } from 'core-decorators'; | ||
const SingerMixin = { | ||
sing(sound) { | ||
alert(sound); | ||
} | ||
}; | ||
const FlyMixin = { | ||
fly() {} | ||
land() {} | ||
}; | ||
@mixin(SingerMixin, FlyMixin) | ||
class Bird { | ||
singMatingCall() { | ||
this.sing('tweet tweet'); | ||
} | ||
} | ||
var bird = new Bird(); | ||
bird.singMatingCall(); | ||
// alerts "tweet tweet" | ||
``` |
@@ -9,2 +9,3 @@ export { default as override } from './override'; | ||
export { default as nonconfigurable } from './nonconfigurable'; | ||
export { default as debounce } from './debounce'; | ||
export { default as debounce } from './debounce'; | ||
export { default as mixin, default as mixins } from './mixin'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61084
32
1341
269