From f8357199358a15d3d36912bbbb7270bd4a2f2e1f Mon Sep 17 00:00:00 2001 From: Jay Phelps Date: Mon, 16 Nov 2015 10:32:05 -0800 Subject: [PATCH] Add enumerable decorator, correct nonenumerable docs/test. Fixes #27 --- README.md | 27 +++++++++++++++++++++++++-- src/core-decorators.js | 1 + src/enumerable.js | 10 ++++++++++ src/nonenumerable.js | 2 +- test/unit/enumerable.spec.js | 14 ++++++++++++++ test/unit/nonenumerable.spec.js | 10 ++-------- 6 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/enumerable.js create mode 100644 test/unit/enumerable.spec.js diff --git a/README.md b/README.md index 05ccae1..b9cd573 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This can be consumed by any transpiler that supports decorators like [babel.js]( ## Decorators -##### For Methods +##### For Properties and Methods * [@autobind](#autobind) * [@readonly](#readonly) * [@override](#override) @@ -24,6 +24,7 @@ This can be consumed by any transpiler that supports decorators like [babel.js]( * [@debounce](#debounce) * [@throttle](#throttle) :new: * [@suppressWarnings](#suppresswarnings) +* [@enumerable](#enumerable) :new: * [@nonenumerable](#nonenumerable) * [@nonconfigurable](#nonconfigurable) * [@decorate](#decorate) :new: @@ -211,9 +212,31 @@ person.facepalmWithoutWarning(); // no warning is logged ``` +### @enumerable + +Marks a method as being enumerable. Note that instance properties are _already_ enumerable, so this is only useful for methods. + +```js +import { enumerable } from 'core-decorators'; + +class Meal { + pay() {} + + @enumerable + eat() {} +} + +var dinner = new Meal(); +for (var key in dinner) { + key; + // "eat" only, not "pay" +} + +``` + ### @nonenumerable -Marks a property or method as not being enumerable. +Marks a property as not being enumerable. Note that class methods are _already_ nonenumerable, so this is only useful for instance properties. ```js import { nonenumerable } from 'core-decorators'; diff --git a/src/core-decorators.js b/src/core-decorators.js index 1f25653..d00a13d 100644 --- a/src/core-decorators.js +++ b/src/core-decorators.js @@ -4,6 +4,7 @@ export { default as suppressWarnings } from './suppress-warnings'; export { default as memoize } from './memoize'; export { default as autobind } from './autobind'; export { default as readonly } from './readonly'; +export { default as enumerable } from './enumerable'; export { default as nonenumerable } from './nonenumerable'; export { default as nonconfigurable } from './nonconfigurable'; export { default as debounce } from './debounce'; diff --git a/src/enumerable.js b/src/enumerable.js new file mode 100644 index 0000000..0d4abfa --- /dev/null +++ b/src/enumerable.js @@ -0,0 +1,10 @@ +import { decorate } from './private/utils'; + +function handleDescriptor(target, key, descriptor) { + descriptor.enumerable = true; + return descriptor; +} + +export default function enumerable(...args) { + return decorate(handleDescriptor, args); +} diff --git a/src/nonenumerable.js b/src/nonenumerable.js index 07dd960..71a3622 100644 --- a/src/nonenumerable.js +++ b/src/nonenumerable.js @@ -5,6 +5,6 @@ function handleDescriptor(target, key, descriptor) { return descriptor; } -export default function enumerable(...args) { +export default function nonenumerable(...args) { return decorate(handleDescriptor, args); } diff --git a/test/unit/enumerable.spec.js b/test/unit/enumerable.spec.js new file mode 100644 index 0000000..aa85415 --- /dev/null +++ b/test/unit/enumerable.spec.js @@ -0,0 +1,14 @@ +import chai from 'chai'; +import enumerable from '../../lib/enumerable'; + +describe('@enumerable', function () { + class Foo { + @enumerable + bar(){} + } + + it('is enumerable', function () { + Object.getOwnPropertyDescriptor(Foo.prototype, 'bar') + .enumerable.should.equal(true); + }); +}); diff --git a/test/unit/nonenumerable.spec.js b/test/unit/nonenumerable.spec.js index 069f258..ac5a660 100644 --- a/test/unit/nonenumerable.spec.js +++ b/test/unit/nonenumerable.spec.js @@ -1,19 +1,13 @@ import chai from 'chai'; import nonenumerable from '../../lib/nonenumerable'; -function enumerable (target, key, descriptor){ - descriptor.enumerable = true; - return descriptor; -} - describe('@nonenumerable', function () { class Foo { @nonenumerable - @enumerable - bar(){} + bar = 'test'; } - it('is nonenumerable', function () { + it('is not enumerable', function () { Object.getOwnPropertyDescriptor(Foo.prototype, 'bar') .enumerable.should.equal(false); });