├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── example └── index.html ├── index.d.ts ├── is-class.js ├── package.json ├── scripts └── gen_babel.js └── test ├── babel-class.js └── is-class.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | # use line feed 8 | end_of_line = lf 9 | 10 | # ensure file ends with a newline when saving 11 | insert_final_newline = true 12 | 13 | # soft tabs 14 | indent_style = space 15 | 16 | # number of columns used for each indentation level 17 | indent_size = 2 18 | 19 | # remove any whitespace characters preceding newline characters 20 | trim_trailing_whitespace = true 21 | 22 | # character set 23 | charset = utf-8 24 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Convert line endings to LF (Line Feed) 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | //this will affect all the git repos 2 | git config --global core.excludesfile ~/.gitignore 3 | 4 | 5 | //update files since .ignore won't if already tracked 6 | git rm --cached 7 | 8 | # Compiled source # 9 | ################### 10 | *.com 11 | *.class 12 | *.dll 13 | *.exe 14 | *.o 15 | *.so 16 | 17 | # Packages # 18 | ############ 19 | # it's better to unpack these files and commit the raw source 20 | # git has its own built in compression methods 21 | *.7z 22 | *.dmg 23 | *.gz 24 | *.iso 25 | *.jar 26 | *.rar 27 | *.tar 28 | *.zip 29 | 30 | # Logs and databases # 31 | ###################### 32 | *.log 33 | *.sql 34 | *.sqlite 35 | 36 | # OS generated files # 37 | ###################### 38 | .DS_Store 39 | .DS_Store? 40 | ._* 41 | .Spotlight-V100 42 | .Trashes 43 | # Icon? 44 | ehthumbs.db 45 | Thumbs.db 46 | .cache 47 | .project 48 | .settings 49 | .tmproj 50 | *.esproj 51 | nbproject 52 | 53 | # Numerous always-ignore extensions # 54 | ##################################### 55 | *.diff 56 | *.err 57 | *.orig 58 | *.rej 59 | *.swn 60 | *.swo 61 | *.swp 62 | *.vi 63 | *~ 64 | *.sass-cache 65 | *.grunt 66 | *.tmp 67 | 68 | # Dreamweaver added files # 69 | ########################### 70 | _notes 71 | dwsync.xml 72 | 73 | # Komodo # 74 | ########################### 75 | *.komodoproject 76 | .komodotools 77 | 78 | # Node # 79 | ##################### 80 | node_modules 81 | 82 | # Bower # 83 | ##################### 84 | bower_components 85 | 86 | # Folders to ignore # 87 | ##################### 88 | .hg 89 | .svn 90 | .CVS 91 | intermediate 92 | publish 93 | .idea 94 | .graphics 95 | _test 96 | _archive 97 | uploads 98 | tmp 99 | 100 | # Vim files to ignore # 101 | ####################### 102 | .VimballRecord 103 | .netrwhist 104 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | 5 | ## [0.0.2] - 2015-04-26 6 | ### Added 7 | - Babel.js classes support 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT license 2 | 3 | Copyright (C) 2014 Miguel Mota 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | of the Software, and to permit persons to whom the Software is furnished to do 10 | so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-class 2 | 3 | > Check if function is an ES6 [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). 4 | 5 | [![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/miguelmota/is-class/master/LICENSE) 6 | [![NPM version](https://badge.fury.io/js/is-class.svg)](http://badge.fury.io/js/is-class) 7 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing) 8 | 9 | # Install 10 | 11 | ```bash 12 | npm install is-class 13 | ``` 14 | 15 | # Usage 16 | 17 | ```javascript 18 | const isClass = require('is-class') 19 | 20 | class F {} 21 | function G() {} 22 | 23 | console.log(isClass(F)) // true 24 | console.log(isClass(G)) // false 25 | ``` 26 | 27 | # Test 28 | 29 | ```bash 30 | npm test 31 | ``` 32 | 33 | # License 34 | 35 | [MIT](LICENSE) 36 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Example 7 | 8 | 9 |
10 | 11 | 12 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export function isClass any = new (...args: any[]) => any>(target: any): target is T; 2 | export default isClass; 3 | -------------------------------------------------------------------------------- /is-class.js: -------------------------------------------------------------------------------- 1 | ;(function (root) { 2 | const toString = Function.prototype.toString 3 | 4 | function fnBody (fn) { 5 | return toString.call(fn).replace(/^[^{]*{\s*/, '').replace(/\s*}[^}]*$/, '') 6 | } 7 | 8 | function isClass (fn) { 9 | if (typeof fn !== 'function') { 10 | return false 11 | } 12 | 13 | if (/^class[\s{]/.test(toString.call(fn))) { 14 | return true 15 | } 16 | 17 | // babel.js classCallCheck() & inlined 18 | const body = fnBody(fn) 19 | return (/classCallCheck\(/.test(body) || /TypeError\("Cannot call a class as a function"\)/.test(body)) 20 | } 21 | 22 | if (typeof exports !== 'undefined') { 23 | if (typeof module !== 'undefined' && module.exports) { 24 | exports = module.exports = isClass 25 | } 26 | exports.isClass = isClass 27 | } else if (typeof define === 'function' && define.amd) { 28 | define([], function () { 29 | return isClass 30 | }) 31 | } else { 32 | root.isClass = isClass 33 | } 34 | })(this); 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-class", 3 | "version": "0.0.9", 4 | "description": "Check if function is an ES6 class.", 5 | "main": "is-class.js", 6 | "types": "index.d.ts", 7 | "typings": "index.d.ts", 8 | "directories": { 9 | "test": "test" 10 | }, 11 | "scripts": { 12 | "test": "tape test/*.js", 13 | "lint": "standard --fix *.js" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/miguelmota/is-class" 18 | }, 19 | "keywords": [ 20 | "predicate", 21 | "function", 22 | "class", 23 | "es6" 24 | ], 25 | "author": { 26 | "name": "Miguel Mota", 27 | "email": "hello@miguelmota.com", 28 | "url": "https://miguelmota.com/" 29 | }, 30 | "license": { 31 | "type": "MIT", 32 | "url": "https://github.com/miguelmota/is-class/blob/master/LICENSE" 33 | }, 34 | "bugs": { 35 | "url": "https://github.com/miguelmota/is-class/issues" 36 | }, 37 | "homepage": "https://github.com/miguelmota/is-class", 38 | "devDependencies": { 39 | "@babel/core": "^7.1.5", 40 | "@babel/plugin-transform-classes": "^7.1.0", 41 | "standard": "^12.0.1", 42 | "tape": "^4.9.1" 43 | }, 44 | "standard": { 45 | "globals": [ 46 | "module", 47 | "exports", 48 | "define", 49 | "describe", 50 | "before", 51 | "after", 52 | "beforeEach", 53 | "afterEach", 54 | "it", 55 | "assert" 56 | ] 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /scripts/gen_babel.js: -------------------------------------------------------------------------------- 1 | var code = `class F {}` 2 | var output = require('@babel/core').transform(code, { 3 | plugins: ['@babel/plugin-transform-classes'] 4 | }) 5 | 6 | console.log(output.code) 7 | -------------------------------------------------------------------------------- /test/babel-class.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; 4 | 5 | var test = require('tape'); 6 | var isClass = require('../is-class'); 7 | 8 | test('isClass', function (t) { 9 | t.plan(9); 10 | 11 | var F = function F() { 12 | _classCallCheck(this, F); 13 | }; 14 | 15 | function G() {} 16 | 17 | t.true(isClass(F)); 18 | t.false(isClass(G)); 19 | t.false(isClass('')); 20 | t.false(isClass(0)); 21 | t.false(isClass(null)); 22 | t.false(isClass(undefined)); 23 | t.false(isClass(1)); 24 | t.false(isClass({})); 25 | t.false(isClass([])); 26 | }); 27 | 28 | -------------------------------------------------------------------------------- /test/is-class.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | var isClass = require('../is-class'); 5 | 6 | test('isClass', function (t) { 7 | t.plan(19); 8 | 9 | class F {} 10 | function G() {} 11 | 12 | t.true(isClass(F)); 13 | t.true(isClass(class{})); 14 | t.true(isClass(class{ })); 15 | t.true(isClass(class{constructor(){}})); 16 | t.true(isClass(class _{})); 17 | t.true(isClass(class _FF {})); 18 | t.true(isClass(class B extends(F){})); 19 | t.true(isClass(class extends(F){})); 20 | t.true(isClass(class extends F{})); 21 | t.true(isClass(class extends F {})); 22 | t.true(isClass(class extends F {})); 23 | t.false(isClass(G)); 24 | t.false(isClass('')); 25 | t.false(isClass(0)); 26 | t.false(isClass(null)); 27 | t.false(isClass(undefined)); 28 | t.false(isClass(1)); 29 | t.false(isClass({})); 30 | t.false(isClass([])); 31 | }); 32 | --------------------------------------------------------------------------------