Giter Site home page Giter Site logo

could-be-class's Introduction

couldBeClass

Checks if an object could be an instantiable class.

This module also works with those classes that are written in ES5 style.

Example

"use strict";

const assert = require("assert");
const couldBeClass = require("could-be-class").default;

var str = "This is a string";
assert.ok(!couldBeClass(str));

var num = 123;
assert.ok(!couldBeClass(num));

class A { }
assert.ok(couldBeClass(A));

function B() {
    this.name = this.constructor.name;
}
assert.ok(couldBeClass(B));

function C() {
    this["name"] = this.constructor.name;
}
assert.ok(couldBeClass(C));

function D() { }
D.prototype.show = function () { }
assert.ok(couldBeClass(D));

function E() { }
Object.defineProperty(E.prototype, "size", {
    get() {
        return this.length;
    }
});
assert.ok(couldBeClass(E));

function TsEs5Class() {
    var _this = _super.call(this) || this;
    _this.desc = "This class form might be generated from TypeScript to ES5";
}
assert.ok(couldBeClass(TsEs5Class));

function notClass() {
    console.log("this is not a class");
}
assert.ok(!couldBeClass(notClass));

function aMethod() {
    this.desc = "this is a method instead of a class";
}
assert.ok(!couldBeClass(aMethod));

function default_1() {
    this.dec = "this might be a class generated by typescript";
}
assert.ok(couldBeClass(default_1));

function default_2() {
    console.log("this is not class");
}
assert.ok(!couldBeClass(default_2));

function* GenFunc() {
    this.desc = "Error will be thrown if trying to instantiate a Generator function.";
}
assert.ok(!couldBeClass(GenFunc));

if (parseFloat(process.version.slice(1)) >= 7.6) {
    // this block should only appear when you NodeJS version higher than 7.6, 
    // otherwise a SyntaxError will be thrown.
    async function AsyncFunc() {
        this.desc = "Error will be thrown if trying to instantiate an async function.";
    }
    assert.ok(!couldBeClass(AsyncFunc));
}

console.log("#### OK ####"); // should outout OK

Tip

Commonly, any functions (except async, generator and arrow functions) in JavaScript could be instantiated as a class constructor, however, in most cases, a class written in ES5 style will carry some special fingerprints that the detector can test if it could be a class constructor or just a pure function.

WARN This module only performs about 98% percent of accuracy when testing ES5 class.

could-be-class's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.