instanceof-unifier

Allow `instanceof` to match even if comparing an instance created by a different copy of the class

Usage no npm install needed!

<script type="module">
  import instanceofUnifier from 'https://cdn.skypack.dev/instanceof-unifier';
</script>

README

instanceof-unifier

Travis CI NPM Version NPM Downloads MIT

Allow instanceof to match even if comparing an instance created by a different copy of the class. This module requires node.js 6.5.0 or above to function, an exception will be thrown if you attempt to load this module in older versions.

instanceofUnifier(Class, matcher)

Calling this function makes alterations to the way instanceof works on Class. matcher must be a string.

The user is responsible for picking a unique matcher name. It is recommended to use <module-name>@<version>:package-local-id. Version does not have to match the package version but it is important to increment it any time a version update is not compatible with previous versions.

Example Usage

Create an ESM module:

// index.mjs
import instanceofUnifier from 'instanceof-unifier';

export class MyClass {
}

instanceofUnifier(MyClass, 'my-package-name@1.0.0:MyClass');

Transpile to Commonjs (babel output shown):

// index.cjs
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MyClass = void 0;

var _instanceofUnifier = _interopRequireDefault(require("instanceof-unifier"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class MyClass {}

exports.MyClass = MyClass;
(0, _instanceofUnifier.default)(MyClass, 'my-package-name@1.0.0:MyClass');

This avoids the divergent specifier hazard for instanceof. You can test:

import indexCJS from './index.cjs';
import {MyClass as ESM} from './index.mjs';

const CJS = indexCJS.MyClass;

const cjs = new CJS();
const esm = new ESM();

if (cjs instanceof CJS && cjs instanceof ESM) {
    console.log('cjs is instanceof both classes');
}

if (esm instanceof CJS && esm instanceof ESM) {
    console.log('esm is instanceof both classes');
}

In addition to the divergent specifier hazard this also ensures instanceof matches between multiple copies of your module.