Skip to content

Commit 07cfdc9

Browse files
fsdiogosatazor
authored andcommitted
feat: add name and symbol params (#1)
1 parent 73e58a3 commit 07cfdc9

File tree

5 files changed

+72
-42
lines changed

5 files changed

+72
-42
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"extends": [
44
"eslint-config-moxy/es6",
55
"eslint-config-moxy/addons/es6-modules",
6-
"eslint-config-moxy/addons/jest"
6+
"eslint-config-moxy/addons/jest",
7+
"eslint-config-moxy/addons/babel-parser"
78
]
89
}

README.md

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# is-class-decorator
1+
# class-is
22

33
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-url]
44

5-
[npm-url]:https://npmjs.org/package/is-class-decorator
6-
[downloads-image]:http://img.shields.io/npm/dm/is-class-decorator.svg
7-
[npm-image]:http://img.shields.io/npm/v/is-class-decorator.svg
8-
[travis-url]:https://travis-ci.org/moxystudio/js-is-class-decorator
9-
[travis-image]:http://img.shields.io/travis/moxystudio/js-is-class-decorator/master.svg
10-
[codecov-url]:https://codecov.io/gh/moxystudio/js-is-class-decorator
11-
[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/js-is-class-decorator/master.svg
12-
[david-dm-url]:https://david-dm.org/moxystudio/js-is-class-decorator
13-
[david-dm-image]:https://img.shields.io/david/moxystudio/js-is-class-decorator.svg
14-
[david-dm-dev-url]:https://david-dm.org/moxystudio/js-is-class-decorator?type=dev
15-
[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/js-is-class-decorator.svg
16-
[greenkeeper-image]:https://badges.greenkeeper.io/moxystudio/js-is-class-decorator.svg
5+
[npm-url]:https://npmjs.org/package/class-is
6+
[downloads-image]:http://img.shields.io/npm/dm/class-is.svg
7+
[npm-image]:http://img.shields.io/npm/v/class-is.svg
8+
[travis-url]:https://travis-ci.org/moxystudio/js-class-is
9+
[travis-image]:http://img.shields.io/travis/moxystudio/js-class-is/master.svg
10+
[codecov-url]:https://codecov.io/gh/moxystudio/js-class-is
11+
[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/js-class-is/master.svg
12+
[david-dm-url]:https://david-dm.org/moxystudio/js-class-is
13+
[david-dm-image]:https://img.shields.io/david/moxystudio/js-class-is.svg
14+
[david-dm-dev-url]:https://david-dm.org/moxystudio/js-class-is?type=dev
15+
[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/js-class-is.svg
16+
[greenkeeper-image]:https://badges.greenkeeper.io/moxystudio/js-class-is.svg
1717
[greenkeeper-url]:https://greenkeeper.io/
1818

1919
Enhances a JavaScript class by adding an `is<Class>` property to compare types between realms.
@@ -28,13 +28,13 @@ So the solution is to use symbols.
2828

2929
## Installation
3030

31-
`$ npm install is-class-decorator`
32-
31+
`$ npm install class-is`
3332

3433
## Usage
3534

3635
```js
37-
import addIs from 'is-class-decorator';
36+
// Package X
37+
import withIs from 'is-class-decorator';
3838

3939
class Person {
4040
constructor(name, city) {
@@ -43,20 +43,26 @@ class Person {
4343
}
4444
}
4545

46+
export default withIs(Person, { className: 'Person', symbolName: '@org/package-x/person' });
47+
48+
// Package Y
4649
class Animal {
4750
constructor(species) {
4851
this.species = species;
4952
}
5053
}
5154

52-
const PersonWithType = addIs('Person')(Person);
53-
const AnimalWithType = addIs('Animal')(Animal);
55+
export default withIs(Animal, { className: 'Animal', symbolName: '@org/package-y/animal' });
5456

55-
const diogo = new PersonWithType('Diogo', 'Porto');
56-
const wolf = new AnimalWithType('Wolf');
57+
// Package Z
58+
import Person from 'package-x';
59+
import Animal from 'package-y';
5760

58-
console.log(PersonWithType.isPerson(diogo));
59-
console.log(PersonWithType.isPerson(wolf));
61+
const diogo = new Person('Diogo', 'Porto');
62+
const wolf = new Animal('Gray Wolf');
63+
64+
console.log(Person.isPerson(diogo));
65+
console.log(Person.isPerson(wolf));
6066
```
6167

6268
Running the example above will print:
@@ -66,6 +72,30 @@ true
6672
false
6773
```
6874

75+
## API
76+
77+
### withIs(Class, { className: name, symbolName: symbol })
78+
79+
###### class
80+
81+
Type: `class`
82+
83+
The class to be enhanced.
84+
85+
###### className
86+
87+
Type: `String`
88+
89+
The name of the class your passing.
90+
91+
###### symbolName
92+
93+
Type: `String`
94+
95+
Unique *id* for the class. This should be namespaced so different classes from different modules do not collide and give false positives.
96+
97+
Example: `@organization/package/class`
98+
6999
## Tests
70100

71101
`$ npm test`

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "is-class-decorator",
2+
"name": "class-is",
33
"version": "0.1.1",
44
"description": "Enhances a JavaScript class by adding an is<Class> property to compare types between realms.",
55
"keywords": [
@@ -14,10 +14,10 @@
1414
"instance-of"
1515
],
1616
"author": "Diogo Silva <fsdiogo@gmail.com>",
17-
"homepage": "https://github.com/moxystudio/js-is-class-decorator",
17+
"homepage": "https://github.com/moxystudio/js-class-is",
1818
"repository": {
1919
"type": "git",
20-
"url": "git@github.com:moxystudio/js-is-class-decorator.git"
20+
"url": "git@github.com:moxystudio/js-class-is.git"
2121
},
2222
"license": "MIT",
2323
"main": "lib/index.js",
@@ -64,5 +64,6 @@
6464
"jest": "^22.4.3",
6565
"lint-staged": "^7.0.0",
6666
"standard-version": "^4.3.0"
67-
}
67+
},
68+
"dependencies": {}
6869
}

src/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
function addIs(name) {
2-
return (Class) => {
3-
const symbol = Symbol.for(`add-is/${name}`);
1+
function withIs(Class, { className, symbolName }) {
2+
const symbol = Symbol.for(symbolName);
43

5-
class NewClass extends Class {
6-
constructor(...args) {
7-
super(...args);
8-
this[symbol] = true;
9-
}
4+
class NewClass extends Class {
5+
constructor(...args) {
6+
super(...args);
7+
Object.defineProperty(this, symbol, { value: true, enumerable: false });
108
}
9+
}
1110

12-
NewClass[`is${name}`] = (obj) => obj && Boolean(obj[symbol]);
11+
NewClass[`is${className}`] = (obj) => obj && Boolean(obj[symbol]);
1312

14-
return NewClass;
15-
};
13+
return NewClass;
1614
}
1715

18-
export default addIs;
16+
export default withIs;

test/index.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import addIs from '../src';
1+
import withIs from '../src';
22

33
class Person {
44
constructor(name, city) {
@@ -13,8 +13,8 @@ class Animal {
1313
}
1414
}
1515

16-
const PersonWithType = addIs('Person')(Person);
17-
const AnimalWithType = addIs('Animal')(Animal);
16+
const PersonWithType = withIs(Person, { className: 'Person', symbolName: '@org/package-x/person' });
17+
const AnimalWithType = withIs(Animal, { className: 'Animal', symbolName: '@org/package-y/animal' });
1818

1919
const diogo = new PersonWithType('Diogo', 'Porto');
2020
const wolf = new AnimalWithType('Wolf');

0 commit comments

Comments
 (0)