Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #28 from jayphelps/en
Browse files Browse the repository at this point in the history
Add enumerable decorator, correct nonenumerable docs/test. Fixes #27
  • Loading branch information
jayphelps committed Nov 16, 2015
2 parents a7e8c74 + f835719 commit c16bda3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ 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)
* [@deprecate](#deprecate-alias-deprecated)
* [@debounce](#debounce)
* [@throttle](#throttle) :new:
* [@suppressWarnings](#suppresswarnings)
* [@enumerable](#enumerable) :new:
* [@nonenumerable](#nonenumerable)
* [@nonconfigurable](#nonconfigurable)
* [@decorate](#decorate) :new:
Expand Down Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/core-decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 10 additions & 0 deletions src/enumerable.js
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion src/nonenumerable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
14 changes: 14 additions & 0 deletions test/unit/enumerable.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 2 additions & 8 deletions test/unit/nonenumerable.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down

0 comments on commit c16bda3

Please sign in to comment.