core-decorators
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -8,4 +8,8 @@ 'use strict'; | ||
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); } } | ||
var _privateUtils = require('./private/utils'); | ||
var defineProperty = Object.defineProperty; | ||
function bind(fn, context) { | ||
@@ -45,3 +49,16 @@ if (fn.bind) { | ||
function handleDescriptor(target, key, _ref) { | ||
function autobindClass(target) { | ||
var descs = (0, _privateUtils.getOwnPropertyDescriptors)(target.prototype); | ||
for (var key in descs) { | ||
var desc = descs[key]; | ||
if (typeof desc.value !== 'function' || key === 'constructor') { | ||
continue; | ||
} | ||
defineProperty(target.prototype, key, autobindMethod(target, key, desc)); | ||
} | ||
} | ||
function autobindMethod(target, key, _ref) { | ||
var fn = _ref.value; | ||
@@ -74,3 +91,3 @@ | ||
Object.defineProperty(this, key, { | ||
defineProperty(this, key, { | ||
configurable: true, | ||
@@ -89,2 +106,10 @@ writable: true, | ||
function handle(args) { | ||
if (args.length === 1) { | ||
return autobindClass.apply(undefined, _toConsumableArray(args)); | ||
} else { | ||
return autobindMethod.apply(undefined, _toConsumableArray(args)); | ||
} | ||
} | ||
function autobind() { | ||
@@ -95,5 +120,11 @@ for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
return (0, _privateUtils.decorate)(handleDescriptor, args); | ||
if (args.length === 0) { | ||
return function () { | ||
return handle(arguments); | ||
}; | ||
} else { | ||
return handle(args); | ||
} | ||
} | ||
module.exports = exports['default']; |
{ | ||
"name": "core-decorators", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "Library of ES2016 (ES7) JavaScript decorators inspired by languages that come with built-ins like @override, @deprecate, @autobind, @mixin and more! Works great with React/Angular/more!", | ||
@@ -36,3 +36,4 @@ "main": "lib/core-decorators.js", | ||
"lodash", | ||
"mixin" | ||
"mixin", | ||
"mixins" | ||
], | ||
@@ -39,0 +40,0 @@ "author": "Jay Phelps <hello@jayphelps>", |
# 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 [JavaScript decorators](https://github.com/wycats/javascript-decorators) (sometimes erroneously stated as ES2016 or ES7) 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. | ||
Library of [JavaScript decorators](https://github.com/wycats/javascript-decorators) (aka ES2016/ES7 decorators) inspired by languages that come with built-ins like @override, @deprecate, @autobind, @mixin and more. Popular with React/Angular, but is framework agnostic. Similar to [Annotations in Java](https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html) but 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. | ||
_*compiled code is intentionally not checked into this repo_ | ||
@@ -24,7 +22,7 @@ | ||
##### For Properties only | ||
##### For Properties | ||
* [@nonenumerable](#nonenumerable) | ||
* [@lazyInitialize](#lazyInitialize) :new: | ||
##### For Methods only | ||
##### For Methods | ||
* [@autobind](#autobind) | ||
@@ -37,5 +35,6 @@ * [@deprecate](#deprecate-alias-deprecated) | ||
* [@throttle](#throttle) :new: | ||
* [@instrument](#instrument) :new: | ||
* [@time](#time) :new: | ||
##### For Classes | ||
* [@autobind](#autobind) | ||
* [@mixin](#mixin-alias-mixins) :new: | ||
@@ -54,2 +53,4 @@ | ||
Individual methods: | ||
```js | ||
@@ -66,3 +67,3 @@ import { autobind } from 'core-decorators'; | ||
let person = new Person(); | ||
let getPerson = person.getPerson; | ||
let { getPerson } = person; | ||
@@ -73,2 +74,27 @@ getPerson() === person; | ||
Entire Class: | ||
```js | ||
import { autobind } from 'core-decorators'; | ||
@autobind | ||
class Person { | ||
getPerson() { | ||
return this; | ||
} | ||
getPersonAgain() { | ||
return this; | ||
} | ||
} | ||
let person = new Person(); | ||
let { getPerson, getPersonAgain } = person; | ||
getPerson() === person; | ||
// true | ||
getPersonAgain() === person; | ||
// true | ||
``` | ||
### @readonly | ||
@@ -383,3 +409,3 @@ | ||
### @instrument | ||
### @time | ||
@@ -390,3 +416,3 @@ Uses `console.time` and `console.timeEnd` to provide function timings with a unique label whose default prefix is `ClassName.method`. Supply a first argument to override the prefix: | ||
class Bird { | ||
@instrument('sing') | ||
@time('sing') | ||
sing() { | ||
@@ -393,0 +419,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { decorate, createDefaultSetter } from './private/utils'; | ||
import { decorate, createDefaultSetter, getOwnPropertyDescriptors } from './private/utils'; | ||
const { defineProperty } = Object; | ||
@@ -40,3 +41,16 @@ function bind(fn, context) { | ||
function handleDescriptor(target, key, { value: fn }) { | ||
function autobindClass(target) { | ||
const descs = getOwnPropertyDescriptors(target.prototype); | ||
for (const key in descs) { | ||
const desc = descs[key]; | ||
if (typeof desc.value !== 'function' || key === 'constructor') { | ||
continue; | ||
} | ||
defineProperty(target.prototype, key, autobindMethod(target, key, desc)); | ||
} | ||
} | ||
function autobindMethod(target, key, { value: fn }) { | ||
if (typeof fn !== 'function') { | ||
@@ -67,3 +81,3 @@ throw new SyntaxError(`@autobind can only be used on functions, not: ${fn}`); | ||
Object.defineProperty(this, key, { | ||
defineProperty(this, key, { | ||
configurable: true, | ||
@@ -82,4 +96,18 @@ writable: true, | ||
function handle(args) { | ||
if (args.length === 1) { | ||
return autobindClass(...args); | ||
} else { | ||
return autobindMethod(...args); | ||
} | ||
} | ||
export default function autobind(...args) { | ||
return decorate(handleDescriptor, args); | ||
if (args.length === 0) { | ||
return function () { | ||
return handle(arguments); | ||
}; | ||
} else { | ||
return handle(args); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
87423
1873
436