core-decorators
Advanced tools
Comparing version 0.1.5 to 0.2.0
@@ -9,5 +9,5 @@ 'use strict'; | ||
var _must = require('must'); | ||
var _chai = require('chai'); | ||
var _must2 = _interopRequireDefault(_must); | ||
var _chai2 = _interopRequireDefault(_chai); | ||
@@ -18,2 +18,4 @@ var _autobind = require('./autobind'); | ||
_chai2['default'].should(); | ||
var Foo = (function () { | ||
@@ -40,3 +42,3 @@ function Foo() { | ||
(0, _must2['default'])(getFoo()).to.equal(foo); | ||
getFoo().should.equal(foo); | ||
}); | ||
@@ -51,5 +53,5 @@ | ||
(0, _must2['default'])(getFoo1()).to.equal(foo1); | ||
(0, _must2['default'])(getFoo2()).to.equal(foo2); | ||
getFoo1().should.equal(foo1); | ||
getFoo2().should.equal(foo2); | ||
}); | ||
}); |
@@ -40,2 +40,6 @@ 'use strict'; | ||
exports.nonconfigurable = _interopRequire(_nonconfigurable); | ||
exports.nonconfigurable = _interopRequire(_nonconfigurable); | ||
var _debounce = require('./debounce'); | ||
exports.debounce = _interopRequire(_debounce); |
@@ -11,2 +11,12 @@ 'use strict'; | ||
var _chai2 = _interopRequireDefault(_chai); | ||
var _sinon = require('sinon'); | ||
var _sinon2 = _interopRequireDefault(_sinon); | ||
var _sinonChai = require('sinon-chai'); | ||
var _sinonChai2 = _interopRequireDefault(_sinonChai); | ||
var _deprecate = require('./deprecate'); | ||
@@ -16,2 +26,5 @@ | ||
_chai2['default'].should(); | ||
_chai2['default'].use(_sinonChai2['default']); | ||
var Foo = (function () { | ||
@@ -23,12 +36,24 @@ function Foo() { | ||
_createDecoratedClass(Foo, [{ | ||
key: 'bar', | ||
key: 'first', | ||
decorators: [_deprecate2['default']], | ||
value: function bar() { | ||
value: function first() { | ||
return 'hello world'; | ||
} | ||
}, { | ||
key: 'car', | ||
value: function car() { | ||
return this.bar(); | ||
key: 'second', | ||
value: function second() { | ||
return this.first(); | ||
} | ||
}, { | ||
key: 'third', | ||
decorators: [(0, _deprecate2['default'])('asdf')], | ||
value: function third() { | ||
return 'hello galaxy'; | ||
} | ||
}, { | ||
key: 'forth', | ||
decorators: [(0, _deprecate2['default'])('fdsa', { url: 'http://example.com/' })], | ||
value: function forth() { | ||
return 'hello universe'; | ||
} | ||
}]); | ||
@@ -40,18 +65,38 @@ | ||
describe('deprecate', function () { | ||
it('console.warn() is called when deprecated function is used', function () { | ||
beforeEach(function () { | ||
_sinon2['default'].spy(console, 'warn'); | ||
}); | ||
afterEach(function () { | ||
console.warn.restore(); | ||
}); | ||
it('console.warn() is called with default warning when the deprecated function is used', function () { | ||
var foo = new Foo(); | ||
console.log('first'); | ||
foo.bar(); | ||
console.log('second', console.warn.calledOnce); | ||
(0, _chai.expect)(console.warn.calledOnce).to.be['true']; | ||
console.log('third', console.warn); | ||
(0, _chai.expect)(console.warn.calledWith('DEPRECATION Foo#bar: This function will be removed in future versions.')).to.be['true']; | ||
foo.car(); | ||
console.log('forth'); | ||
(0, _chai.expect)(console.warn.calledOnce).to.be['true']; | ||
console.log('fifth'); | ||
(0, _chai.expect)(console.warn.calledWith('DEPRECATION Foo#bar: This function will be removed in future versions.')).to.be['true']; | ||
console.log('sixth'); | ||
foo.first().should.equal('hello world'); | ||
console.warn.should.have.been.calledOnce; | ||
console.warn.should.have.been.calledWith('DEPRECATION Foo#first: This function will be removed in future versions.'); | ||
foo.second().should.equal('hello world'); | ||
console.warn.should.have.been.calledTwice; | ||
console.warn.should.have.been.calledWith('DEPRECATION Foo#first: This function will be removed in future versions.'); | ||
}); | ||
it('console.warn() is called with the custom message, when provided', function () { | ||
return; | ||
var foo = new Foo(); | ||
foo.third().should.equal('hello galaxy'); | ||
console.warn.should.have.been.calledOnce; | ||
console.warn.should.have.been.calledWith('asdf'); | ||
}); | ||
it('console.warn() is called with the URL, when provided', function () { | ||
var foo = new Foo(); | ||
foo.forth().should.equal('hello universe'); | ||
console.warn.should.have.been.calledOnce; | ||
console.warn.should.have.been.calledWith('DEPRECATION Foo#forth: fdsa\n\n See http://example.com/ for more details.\n\n'); | ||
}); | ||
}); |
@@ -61,35 +61,19 @@ 'use strict'; | ||
return _extends({}, descriptor, _defineProperty({}, wrapKey, function memoizeWrapper() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var signature = '0'; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
for (var i = 0, l = args.length; i < l; i++) { | ||
var arg = args[i]; | ||
var argRef = toObject(primativeRefCache, arg); | ||
var argKey = argumentCache.get(argRef); | ||
try { | ||
for (var _iterator = arguments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var arg = _step.value; | ||
if (argKey === undefined) { | ||
argKey = ++argumentIdCounter; | ||
argumentCache.set(argRef, argKey); | ||
} | ||
var argRef = toObject(primativeRefCache, arg); | ||
var argKey = argumentCache.get(argRef); | ||
if (argKey === undefined) { | ||
argKey = ++argumentIdCounter; | ||
argumentCache.set(argRef, argKey); | ||
} | ||
signature += argKey; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator['return']) { | ||
_iterator['return'](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
signature += argKey; | ||
} | ||
@@ -102,4 +86,4 @@ | ||
function memoize() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
@@ -106,0 +90,0 @@ |
@@ -9,3 +9,6 @@ 'use strict'; | ||
exports.decorate = decorate; | ||
exports.metaFor = metaFor; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
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); } } | ||
@@ -20,27 +23,6 @@ | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var key = _step.value; | ||
if (desc.hasOwnProperty(key)) { | ||
return true; | ||
} | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
if (desc.hasOwnProperty(keys[i])) { | ||
return true; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator['return']) { | ||
_iterator['return'](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
@@ -59,2 +41,21 @@ | ||
} | ||
} | ||
var Meta = function Meta() { | ||
_classCallCheck(this, Meta); | ||
this.debounceTimeoutIds = {}; | ||
}; | ||
var defineProperty = Object.defineProperty; | ||
function metaFor(obj) { | ||
if (obj.hasOwnProperty('__core_decorators__') === false) { | ||
defineProperty(obj, '__core_decorators__', { | ||
// Defaults: NOT enumerable, configurable, or writable | ||
value: new Meta() | ||
}); | ||
} | ||
return obj.__core_decorators__; | ||
} |
{ | ||
"name": "core-decorators", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "Library of ES7 decorators inspired by languages that come with built-ins like @override, @deprecated, etc", | ||
@@ -42,4 +42,5 @@ "main": "lib/core-decorators.js", | ||
"mocha": "^2.2.5", | ||
"sinon": "^1.15.4" | ||
"sinon": "^1.15.4", | ||
"sinon-chai": "^2.8.0" | ||
} | ||
} |
@@ -21,2 +21,3 @@ # core-decorators.js [![Build Status](https://travis-ci.org/jayphelps/core-decorators.js.svg?branch=master)](https://travis-ci.org/jayphelps/core-decorators.js) | ||
* [@deprecate](#deprecate-alias-deprecated) | ||
* [@debounce](#debounce) | ||
* [@suppressWarnings](#suppresswarnings) | ||
@@ -30,3 +31,2 @@ * [@memoize](#memoize) | ||
* @instrument/profile | ||
* @debounce | ||
* @throttle | ||
@@ -139,2 +139,22 @@ * @private | ||
### @debounce | ||
Creates a new debounced function which will be invoked after `wait` milliseconds since the time it was invoked. Default timeout is 300 ms. | ||
Optional boolean second argument allows to trigger function on the leading instead of the trailing edge of the wait interval. Implementation is insired by similar method from [UnderscoreJS](http://underscorejs.org/#debounce). | ||
```js | ||
import { deprecate } from 'core-decorators'; | ||
class Editor { | ||
content = ''; | ||
@debounce(500) | ||
updateContent(content) { | ||
this.content = content; | ||
} | ||
} | ||
``` | ||
### @suppressWarnings | ||
@@ -141,0 +161,0 @@ |
@@ -1,4 +0,6 @@ | ||
import expect from 'must'; | ||
import chai from 'chai'; | ||
import autobind from './autobind'; | ||
chai.should(); | ||
class Foo { | ||
@@ -16,3 +18,3 @@ @autobind | ||
expect(getFoo()).to.equal(foo); | ||
getFoo().should.equal(foo); | ||
}); | ||
@@ -27,5 +29,5 @@ | ||
expect(getFoo1()).to.equal(foo1); | ||
expect(getFoo2()).to.equal(foo2); | ||
getFoo1().should.equal(foo1); | ||
getFoo2().should.equal(foo2); | ||
}); | ||
}); |
@@ -9,1 +9,2 @@ export { default as override } from './override'; | ||
export { default as nonconfigurable } from './nonconfigurable'; | ||
export { default as debounce } from './debounce'; |
@@ -1,32 +0,67 @@ | ||
import { expect } from 'chai'; | ||
import chai from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import deprecate from './deprecate'; | ||
chai.should(); | ||
chai.use(sinonChai); | ||
class Foo { | ||
@deprecate | ||
bar() { | ||
first() { | ||
return 'hello world'; | ||
} | ||
car() { | ||
return this.bar(); | ||
second() { | ||
return this.first(); | ||
} | ||
@deprecate('asdf') | ||
third() { | ||
return 'hello galaxy'; | ||
} | ||
@deprecate('fdsa', { url: 'http://example.com/' }) | ||
forth() { | ||
return 'hello universe'; | ||
} | ||
} | ||
describe('deprecate', function () { | ||
it('console.warn() is called when deprecated function is used', function () { | ||
beforeEach(function () { | ||
sinon.spy(console, 'warn'); | ||
}); | ||
afterEach(function () { | ||
console.warn.restore(); | ||
}); | ||
it('console.warn() is called with default warning when the deprecated function is used', function () { | ||
const foo = new Foo(); | ||
console.log('first'); | ||
foo.bar(); | ||
console.log('second', console.warn.calledOnce); | ||
expect(console.warn.calledOnce).to.be.true; | ||
console.log('third', console.warn); | ||
expect(console.warn.calledWith('DEPRECATION Foo#bar: This function will be removed in future versions.')).to.be.true; | ||
foo.first().should.equal('hello world'); | ||
console.warn.should.have.been.calledOnce; | ||
console.warn.should.have.been.calledWith('DEPRECATION Foo#first: This function will be removed in future versions.'); | ||
foo.car(); | ||
console.log('forth'); | ||
expect(console.warn.calledOnce).to.be.true; | ||
console.log('fifth'); | ||
expect(console.warn.calledWith('DEPRECATION Foo#bar: This function will be removed in future versions.')).to.be.true; | ||
console.log('sixth'); | ||
foo.second().should.equal('hello world'); | ||
console.warn.should.have.been.calledTwice; | ||
console.warn.should.have.been.calledWith('DEPRECATION Foo#first: This function will be removed in future versions.'); | ||
}); | ||
it('console.warn() is called with the custom message, when provided', function () { | ||
return; | ||
const foo = new Foo(); | ||
foo.third().should.equal('hello galaxy'); | ||
console.warn.should.have.been.calledOnce; | ||
console.warn.should.have.been.calledWith('asdf'); | ||
}); | ||
it('console.warn() is called with the URL, when provided', function () { | ||
const foo = new Foo(); | ||
foo.forth().should.equal('hello universe'); | ||
console.warn.should.have.been.calledOnce; | ||
console.warn.should.have.been.calledWith('DEPRECATION Foo#forth: fdsa\n\n See http://example.com/ for more details.\n\n'); | ||
}); | ||
}); |
@@ -45,6 +45,7 @@ import { decorate } from './private/utils'; | ||
...descriptor, | ||
[wrapKey]: function memoizeWrapper() { | ||
[wrapKey]: function memoizeWrapper(...args) { | ||
let signature = '0'; | ||
for (const arg of arguments) { | ||
for (let i = 0, l = args.length; i < l; i++) { | ||
let arg = args[i]; | ||
let argRef = toObject(primativeRefCache, arg); | ||
@@ -51,0 +52,0 @@ let argKey = argumentCache.get(argRef); |
@@ -8,4 +8,4 @@ export function isDescriptor(desc) { | ||
for (const key of keys) { | ||
if (desc.hasOwnProperty(key)) { | ||
for (let i = 0, l = keys.length; i < l; i++) { | ||
if (desc.hasOwnProperty(keys[i])) { | ||
return true; | ||
@@ -27,1 +27,18 @@ } | ||
} | ||
class Meta { | ||
debounceTimeoutIds = {}; | ||
} | ||
const { defineProperty } = Object; | ||
export function metaFor(obj) { | ||
if (obj.hasOwnProperty('__core_decorators__') === false) { | ||
defineProperty(obj, '__core_decorators__', { | ||
// Defaults: NOT enumerable, configurable, or writable | ||
value: new Meta() | ||
}); | ||
} | ||
return obj.__core_decorators__; | ||
} |
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
59417
32
1353
231
5