Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

class-is

Package Overview
Dependencies
Maintainers
20
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-is - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

10

CHANGELOG.md

@@ -5,2 +5,12 @@ # Change Log

<a name="0.5.0"></a>
# [0.5.0](https://github.com/moxystudio/js-class-is/compare/v0.4.0...v0.5.0) (2018-03-28)
### Features
* add support to ES5 classes ([0beb84f](https://github.com/moxystudio/js-class-is/commit/0beb84f))
<a name="0.4.0"></a>

@@ -7,0 +17,0 @@ # [0.4.0](https://github.com/moxystudio/js-class-is/compare/v0.3.1...v0.4.0) (2018-03-27)

@@ -32,2 +32,30 @@ 'use strict';

function withIsProto(Class, { className, symbolName }) {
const symbol = Symbol.for(symbolName);
const ClassIsWrapper = {
/* eslint-disable object-shorthand */
[className]: function (...args) {
const _this = Class.call(this, ...args) || this;
if (_this && !_this[symbol]) {
Object.defineProperty(_this, symbol, { value: true });
}
return _this;
},
}[className];
Object.defineProperty(ClassIsWrapper.prototype, Symbol.toStringTag, {
get() {
return className;
},
});
ClassIsWrapper[`is${className}`] = (obj) => obj && Boolean(obj[symbol]);
return ClassIsWrapper;
}
module.exports = withIs;
module.exports.proto = withIsProto;

2

package.json
{
"name": "class-is",
"version": "0.4.0",
"version": "0.5.0",
"description": "Enhances a JavaScript class by adding an is<Class> property to compare types between realms.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -37,2 +37,4 @@ # class-is

### ES6 classes:
```js

@@ -80,2 +82,35 @@ // Package X

### ES5 and below classes:
In ES5 it's not unusual to see constructors like the one below, so you can call it without using the `new` keyword.
```js
function Circle(radius) {
if (!(this instanceof Circle)) {
return new Circle();
}
}
```
In such cases you can use the `withIs.proto` method:
```js
const withIs = require('class-is');
const Circle = withIs.proto(function () {
if (!(this instanceof Circle)) {
return new Circle();
}
}, { className: 'Circle', symbolName: '@org/package/circle' });
const circle = Circle();
console.log(Circle.isCircle(circle));
```
The example above will print:
```
true
```
## API

@@ -105,2 +140,7 @@

### withIs.proto(Class, { className: name, symbolName: symbol })
Apply the same parameters as above.
## Tests

@@ -107,0 +147,0 @@

@@ -5,31 +5,71 @@ 'use strict';

class Person {
constructor(name, city) {
this.name = name;
this.city = city;
/* ==================================================================
ES6 tests
================================================================== */
describe('ES6', () => {
class Person {
constructor(name, city) {
this.name = name;
this.city = city;
}
}
}
class Animal {
constructor(species) {
this.species = species;
class Animal {
constructor(species) {
this.species = species;
}
}
}
const PersonWithIs = withIs(Person, { className: 'Person', symbolName: '@org/package-x/person' });
const AnimalWithIs = withIs(Animal, { className: 'Animal', symbolName: '@org/package-y/animal' });
const PersonWithIs = withIs(Person, { className: 'Person', symbolName: '@org/package-x/person' });
const AnimalWithIs = withIs(Animal, { className: 'Animal', symbolName: '@org/package-y/animal' });
const diogo = new PersonWithIs('Diogo', 'Porto');
const wolf = new AnimalWithIs('Wolf');
const diogo = new PersonWithIs('Diogo', 'Porto');
const wolf = new AnimalWithIs('Wolf');
test('person is an instance of Person class', () => {
expect(PersonWithIs.isPerson(diogo)).toBe(true);
});
test('person is an instance of Person class', () => {
expect(PersonWithIs.isPerson(diogo)).toBe(true);
});
test('wolf is not an instance of Person class', () => {
expect(PersonWithIs.isPerson(wolf)).toBe(false);
test('wolf is not an instance of Person class', () => {
expect(PersonWithIs.isPerson(wolf)).toBe(false);
});
test('check custom tag of Person class', () => {
expect(Object.prototype.toString.call(diogo)).toBe('[object Person]');
});
});
test('check custom tag of Person class', () => {
expect(Object.prototype.toString.call(diogo)).toBe('[object Person]');
/* ==================================================================
ES5 and below tests
================================================================== */
describe('ES5 and below', () => {
const Circle = withIs.proto(function () {
if (!(this instanceof Circle)) { // eslint-disable-line no-invalid-this
return new Circle();
}
}, { className: 'Circle', symbolName: '@org/package/cirlce' });
const circle = Circle(); // eslint-disable-line new-cap
const Square = withIs.proto(function () {
if (!(this instanceof Square)) { // eslint-disable-line no-invalid-this
return new Square();
}
}, { className: 'Square', symbolName: '@org/package/square' });
const square = new Square();
test('circle is an instance of Circle class', () => {
expect(Circle.isCircle(circle)).toBe(true);
});
test('square is an instance of Square class', () => {
expect(Square.isSquare(square)).toBe(true);
});
test('check custom tag of Square class', () => {
expect(Object.prototype.toString.call(square)).toBe('[object Square]');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc