Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
{
"root": true,
"extends": [
"eslint-config-moxy/es6",
"eslint-config-moxy/addons/node",
"eslint-config-moxy/addons/jest"
]
}
+66
View File
@@ -0,0 +1,66 @@
'use strict';
function withIs(Class, { className, symbolName }) {
const symbol = Symbol.for(symbolName);
const ClassIsWrapper = {
// The code below assigns the class wrapper to an object to trick
// JavaScript engines to show the name of the extended class when
// logging an instances.
// We are assigning an anonymous class (class wrapper) to the object
// with key `className` to keep the correct name.
// If this is not supported it falls back to logging `ClassIsWrapper`.
[className]: class extends Class {
constructor(...args) {
super(...args);
Object.defineProperty(this, symbol, { value: true });
}
get [Symbol.toStringTag]() {
return className;
}
},
}[className];
ClassIsWrapper[`is${className}`] = (obj) => !!(obj && obj[symbol]);
return ClassIsWrapper;
}
function withIsProto(Class, { className, symbolName, withoutNew }) {
const symbol = Symbol.for(symbolName);
/* eslint-disable object-shorthand */
const ClassIsWrapper = {
[className]: function (...args) {
if (withoutNew && !(this instanceof ClassIsWrapper)) {
return new ClassIsWrapper(...args);
}
const _this = Class.call(this, ...args) || this;
if (_this && !_this[symbol]) {
Object.defineProperty(_this, symbol, { value: true });
}
return _this;
},
}[className];
/* eslint-enable object-shorthand */
ClassIsWrapper.prototype = Object.create(Class.prototype);
ClassIsWrapper.prototype.constructor = ClassIsWrapper;
Object.defineProperty(ClassIsWrapper.prototype, Symbol.toStringTag, {
get() {
return className;
},
});
ClassIsWrapper[`is${className}`] = (obj) => !!(obj && obj[symbol]);
return ClassIsWrapper;
}
module.exports = withIs;
module.exports.proto = withIsProto;
+89
View File
@@ -0,0 +1,89 @@
{
"_from": "class-is@^1.1.0",
"_id": "class-is@1.1.0",
"_inBundle": false,
"_integrity": "sha512-rhjH9AG1fvabIDoGRVH587413LPjTZgmDF9fOFCbFJQV4yuocX1mHxxvXI4g3cGwbVY9wAYIoKlg1N79frJKQw==",
"_location": "/class-is",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "class-is@^1.1.0",
"name": "class-is",
"escapedName": "class-is",
"rawSpec": "^1.1.0",
"saveSpec": null,
"fetchSpec": "^1.1.0"
},
"_requiredBy": [
"/cids"
],
"_resolved": "https://registry.npmjs.org/class-is/-/class-is-1.1.0.tgz",
"_shasum": "9d3c0fba0440d211d843cec3dedfa48055005825",
"_spec": "class-is@^1.1.0",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\cids",
"author": {
"name": "Diogo Silva",
"email": "fsdiogo@gmail.com"
},
"bugs": {
"url": "https://github.com/moxystudio/js-class-is/issues"
},
"bundleDependencies": false,
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"dependencies": {},
"deprecated": false,
"description": "Enhances a JavaScript class by adding an is<Class> property to compare types between realms.",
"devDependencies": {
"@commitlint/cli": "^6.1.3",
"@commitlint/config-conventional": "^6.1.3",
"eslint": "^4.19.1",
"eslint-config-moxy": "^5.2.1",
"husky": "^0.14.3",
"jest": "^22.4.3",
"lint-staged": "^7.0.0",
"standard-version": "^4.3.0"
},
"homepage": "https://github.com/moxystudio/js-class-is",
"keywords": [
"withis",
"with-is",
"isclass",
"is-class",
"symbols",
"realms",
"instanceof",
"instance-of"
],
"license": "MIT",
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
"main": "index.js",
"name": "class-is",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/moxystudio/js-class-is.git"
},
"scripts": {
"commitmsg": "commitlint -e $GIT_PARAMS",
"lint": "eslint .",
"precommit": "lint-staged",
"prerelease": "npm t && npm run lint",
"release": "standard-version",
"test": "jest --env node --coverage"
},
"standard-version": {
"scripts": {
"posttag": "git push --follow-tags origin master && npm publish"
}
},
"version": "1.1.0"
}
+127
View File
@@ -0,0 +1,127 @@
'use strict';
const {
Animal,
Plant,
Mammal,
Algae,
ExplicitWithoutNew,
ImplicitWithoutNew,
ImplicitExplicitWithoutNew,
} = require('./fixtures/es5');
it('should setup the prototype chain correctly', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Object.getPrototypeOf(animal)).toBe(Animal.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(animal))).toBe(Animal.WrappedClass.prototype);
expect(Object.getPrototypeOf(animal)).not.toBe(Plant.prototype);
expect(Object.getPrototypeOf(plant)).toBe(Plant.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(plant))).toBe(Plant.WrappedClass.prototype);
expect(Object.getPrototypeOf(plant)).not.toBe(Animal.prototype);
expect(animal instanceof Animal).toBe(true);
expect(animal instanceof Animal.WrappedClass).toBe(true);
expect(animal instanceof Plant).toBe(false);
expect(plant instanceof Plant).toBe(true);
expect(plant instanceof Plant.WrappedClass).toBe(true);
expect(plant instanceof Animal).toBe(false);
expect(animal.getType()).toBe('mammal');
expect(plant.getType()).toBe('algae');
});
it('should have a custom toStringTag', () => {
expect(Object.prototype.toString.call(new Animal())).toBe('[object Animal]');
expect(Object.prototype.toString.call(new Plant())).toBe('[object Plant]');
});
describe('is<className> method', () => {
it('should add a working is<className> static method', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Animal.isAnimal(animal)).toBe(true);
expect(Animal.isAnimal(plant)).toBe(false);
expect(Plant.isPlant(plant)).toBe(true);
expect(Plant.isPlant(animal)).toBe(false);
});
it('should not crash if `null` or `undefined` is passed to is<ClassName>', () => {
expect(Animal.isAnimal(null)).toBe(false);
expect(Animal.isAnimal(undefined)).toBe(false);
});
it('should work correctly for deep inheritance scenarios', () => {
const mammal = new Mammal();
const algae = new Algae();
expect(Mammal.isMammal(mammal)).toBe(true);
expect(Animal.isAnimal(mammal)).toBe(true);
expect(Mammal.isMammal(algae)).toBe(false);
expect(Animal.isAnimal(algae)).toBe(false);
expect(Algae.isAlgae(algae)).toBe(true);
expect(Plant.isPlant(algae)).toBe(true);
expect(Algae.isAlgae(mammal)).toBe(false);
expect(Plant.isPlant(mammal)).toBe(false);
});
});
describe('new operator', () => {
it('should work on explicit without-new handling', () => {
const instance = new ExplicitWithoutNew();
const instance2 = ExplicitWithoutNew(); // eslint-disable-line new-cap
expect(Object.getPrototypeOf(instance)).toBe(ExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ExplicitWithoutNew.WrappedClass.prototype);
expect(Object.getPrototypeOf(instance2)).toBe(ExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance2))).toBe(ExplicitWithoutNew.WrappedClass.prototype);
expect(instance instanceof ExplicitWithoutNew).toBe(true);
expect(instance instanceof ExplicitWithoutNew.WrappedClass).toBe(true);
expect(instance2 instanceof ExplicitWithoutNew).toBe(true);
expect(instance2 instanceof ExplicitWithoutNew.WrappedClass).toBe(true);
expect(instance.getLabel()).toBe('ExplicitWithoutNew');
expect(instance2.getLabel()).toBe('ExplicitWithoutNew');
});
it('should work on implicit without-new handling', () => {
const instance = new ImplicitWithoutNew();
const instanceNoNew = ImplicitWithoutNew(); // eslint-disable-line new-cap
expect(Object.getPrototypeOf(instance)).toBe(ImplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ImplicitWithoutNew.WrappedClass.prototype);
expect(Object.getPrototypeOf(instanceNoNew)).toBe(ImplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instanceNoNew))).toBe(ImplicitWithoutNew.WrappedClass.prototype);
expect(instance instanceof ImplicitWithoutNew).toBe(true);
expect(instance instanceof ImplicitWithoutNew.WrappedClass).toBe(true);
expect(instanceNoNew instanceof ImplicitWithoutNew).toBe(true);
expect(instanceNoNew instanceof ImplicitWithoutNew.WrappedClass).toBe(true);
expect(instance.getLabel()).toBe('ImplicitWithoutNew');
expect(instanceNoNew.getLabel()).toBe('ImplicitWithoutNew');
});
it('should work on explicit & implicit without-new handling', () => {
const instance = new ImplicitExplicitWithoutNew();
const instanceNoNew = ImplicitExplicitWithoutNew(); // eslint-disable-line new-cap
expect(Object.getPrototypeOf(instance)).toBe(ImplicitExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instance))).toBe(ImplicitExplicitWithoutNew.WrappedClass.prototype);
expect(Object.getPrototypeOf(instanceNoNew)).toBe(ImplicitExplicitWithoutNew.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(instanceNoNew))).toBe(ImplicitExplicitWithoutNew.WrappedClass.prototype);
expect(instance instanceof ImplicitExplicitWithoutNew).toBe(true);
expect(instance instanceof ImplicitExplicitWithoutNew.WrappedClass).toBe(true);
expect(instanceNoNew instanceof ImplicitExplicitWithoutNew).toBe(true);
expect(instanceNoNew instanceof ImplicitExplicitWithoutNew.WrappedClass).toBe(true);
expect(instance.getLabel()).toBe('ImplicitExplicitWithoutNew');
expect(instanceNoNew.getLabel()).toBe('ImplicitExplicitWithoutNew');
});
});
+67
View File
@@ -0,0 +1,67 @@
'use strict';
const {
Animal,
Plant,
Mammal,
Algae,
} = require('./fixtures/es6');
it('should setup the prototype chain correctly', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Object.getPrototypeOf(animal)).toBe(Animal.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(animal))).toBe(Animal.WrappedClass.prototype);
expect(Object.getPrototypeOf(animal)).not.toBe(Plant.prototype);
expect(Object.getPrototypeOf(plant)).toBe(Plant.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(plant))).toBe(Plant.WrappedClass.prototype);
expect(Object.getPrototypeOf(plant)).not.toBe(Animal.prototype);
expect(animal instanceof Animal).toBe(true);
expect(animal instanceof Animal.WrappedClass).toBe(true);
expect(animal instanceof Plant).toBe(false);
expect(plant instanceof Plant).toBe(true);
expect(plant instanceof Plant.WrappedClass).toBe(true);
expect(plant instanceof Animal).toBe(false);
expect(animal.getType()).toBe('mammal');
expect(plant.getType()).toBe('algae');
});
it('should have a custom toStringTag', () => {
expect(Object.prototype.toString.call(new Animal())).toBe('[object Animal]');
expect(Object.prototype.toString.call(new Plant())).toBe('[object Plant]');
});
describe('is<className> method', () => {
it('should add a working is<className> static method', () => {
const animal = new Animal('mammal');
const plant = new Plant('algae');
expect(Animal.isAnimal(animal)).toBe(true);
expect(Animal.isAnimal(plant)).toBe(false);
expect(Plant.isPlant(plant)).toBe(true);
expect(Plant.isPlant(animal)).toBe(false);
});
it('should not crash if `null` or `undefined` is passed to is<ClassName>', () => {
expect(Animal.isAnimal(null)).toBe(false);
expect(Animal.isAnimal(undefined)).toBe(false);
});
it('should work correctly for deep inheritance scenarios', () => {
const mammal = new Mammal();
const algae = new Algae();
expect(Mammal.isMammal(mammal)).toBe(true);
expect(Animal.isAnimal(mammal)).toBe(true);
expect(Mammal.isMammal(algae)).toBe(false);
expect(Animal.isAnimal(algae)).toBe(false);
expect(Algae.isAlgae(algae)).toBe(true);
expect(Plant.isPlant(algae)).toBe(true);
expect(Algae.isAlgae(mammal)).toBe(false);
expect(Plant.isPlant(mammal)).toBe(false);
});
});
+17
View File
@@ -0,0 +1,17 @@
'use strict';
const withIs = require('../../..');
const Plant = require('./Plant');
function Algae() {
Plant.call(this, 'algae');
}
Algae.prototype = Object.create(Plant.prototype);
Algae.prototype.constructor = Algae;
module.exports = withIs.proto(Algae, {
className: 'Algae',
symbolName: '@org/package/Algae',
});
module.exports.WrappedClass = Algae;
+17
View File
@@ -0,0 +1,17 @@
'use strict';
const withIs = require('../../..');
function Animal(type) {
this.type = type;
}
Animal.prototype.getType = function () {
return this.type;
};
module.exports = withIs.proto(Animal, {
className: 'Animal',
symbolName: '@org/package/Animal',
});
module.exports.WrappedClass = Animal;
+23
View File
@@ -0,0 +1,23 @@
'use strict';
const withIs = require('../../..');
function ExplicitWithoutNew_() {
if (!(this instanceof ExplicitWithoutNew)) {
return new ExplicitWithoutNew();
}
this.label = 'ExplicitWithoutNew';
}
ExplicitWithoutNew_.prototype.getLabel = function () {
return this.label;
};
const ExplicitWithoutNew = withIs.proto(ExplicitWithoutNew_, {
className: 'ExplicitWithoutNew',
symbolName: '@org/package/ExplicitWithoutNew',
});
module.exports = ExplicitWithoutNew;
module.exports.WrappedClass = ExplicitWithoutNew_;
@@ -0,0 +1,25 @@
'use strict';
const withIs = require('../../..');
function ImplicitExplicitWithoutNew_() {
/* istanbul ignore if */
if (!(this instanceof ImplicitExplicitWithoutNew)) {
return new ImplicitExplicitWithoutNew();
}
this.label = 'ImplicitExplicitWithoutNew';
}
ImplicitExplicitWithoutNew_.prototype.getLabel = function () {
return this.label;
};
const ImplicitExplicitWithoutNew = withIs.proto(ImplicitExplicitWithoutNew_, {
className: 'ImplicitExplicitWithoutNew',
symbolName: '@org/package/ImplicitExplicitWithoutNew',
withoutNew: true,
});
module.exports = ImplicitExplicitWithoutNew;
module.exports.WrappedClass = ImplicitExplicitWithoutNew_;
+18
View File
@@ -0,0 +1,18 @@
'use strict';
const withIs = require('../../..');
function ImplicitWithoutNew() {
this.label = 'ImplicitWithoutNew';
}
ImplicitWithoutNew.prototype.getLabel = function () {
return this.label;
};
module.exports = withIs.proto(ImplicitWithoutNew, {
className: 'ImplicitWithoutNew',
symbolName: '@org/package/ImplicitWithoutNew',
withoutNew: true,
});
module.exports.WrappedClass = ImplicitWithoutNew;
+17
View File
@@ -0,0 +1,17 @@
'use strict';
const withIs = require('../../..');
const Animal = require('./Animal');
function Mammal() {
Animal.call(this, 'mammal');
}
Mammal.prototype = Object.create(Animal.prototype);
Mammal.prototype.constructor = Mammal;
module.exports = withIs.proto(Mammal, {
className: 'Mammal',
symbolName: '@org/package/Mammal',
});
module.exports.WrappedClass = Mammal;
+17
View File
@@ -0,0 +1,17 @@
'use strict';
const withIs = require('../../..');
function Plant(type) {
this.type = type;
}
Plant.prototype.getType = function () {
return this.type;
};
module.exports = withIs.proto(Plant, {
className: 'Plant',
symbolName: '@org/package/Plant',
});
module.exports.WrappedClass = Plant;
+12
View File
@@ -0,0 +1,12 @@
'use strict';
module.exports = {
Animal: require('./Animal'),
Plant: require('./Plant'),
Mammal: require('./Mammal'),
Algae: require('./Algae'),
ExplicitWithoutNew: require('./ExplicitWithoutNew'),
ImplicitWithoutNew: require('./ImplicitWithoutNew'),
ImplicitExplicitWithoutNew: require('./ImplicitExplicitWithoutNew'),
};
+16
View File
@@ -0,0 +1,16 @@
'use strict';
const withIs = require('../../..');
const Plant = require('./Plant');
class Algae extends Plant {
constructor() {
super('algae');
}
}
module.exports = withIs(Algae, {
className: 'Algae',
symbolName: '@org/package/Algae',
});
module.exports.WrappedClass = Algae;
+19
View File
@@ -0,0 +1,19 @@
'use strict';
const withIs = require('../../..');
class Animal {
constructor(type) {
this.type = type;
}
getType() {
return this.type;
}
}
module.exports = withIs(Animal, {
className: 'Animal',
symbolName: '@org/package/Animal',
});
module.exports.WrappedClass = Animal;
+16
View File
@@ -0,0 +1,16 @@
'use strict';
const withIs = require('../../..');
const Animal = require('./Animal');
class Mammal extends Animal {
constructor() {
super('mammal');
}
}
module.exports = withIs(Mammal, {
className: 'Mammal',
symbolName: '@org/package/Mammal',
});
module.exports.WrappedClass = Mammal;
+19
View File
@@ -0,0 +1,19 @@
'use strict';
const withIs = require('../../..');
class Plant {
constructor(type) {
this.type = type;
}
getType() {
return this.type;
}
}
module.exports = withIs(Plant, {
className: 'Plant',
symbolName: '@org/package/Plant',
});
module.exports.WrappedClass = Plant;
+8
View File
@@ -0,0 +1,8 @@
'use strict';
module.exports = {
Animal: require('./Animal'),
Plant: require('./Plant'),
Mammal: require('./Mammal'),
Algae: require('./Algae'),
};