├── .babelrc ├── .eslintrc ├── .gitignore ├── .npmignore ├── .npmrc ├── CHANGELOG.md ├── DEV_ONLY └── index.js ├── LICENSE ├── README.md ├── dist ├── get-object-class.js ├── get-object-class.js.map └── get-object-class.min.js ├── package.json ├── src ├── constants.js ├── getClassName.js └── index.js ├── test ├── getClassName.js ├── helpers │ ├── fakeData.js │ └── setup-browser-env.js └── index.js ├── webpack.config.dev.js ├── webpack.config.js ├── webpack.config.minified.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["add-module-exports"], 3 | "presets": [ 4 | [ 5 | "env", 6 | { 7 | "loose": true 8 | } 9 | ], 10 | "react", 11 | "stage-2" 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env":{ 3 | "amd": true, 4 | "browser": true, 5 | "es6": true, 6 | "node": true 7 | }, 8 | "globals": { 9 | "process": true 10 | }, 11 | "parser": "babel-eslint", 12 | "parserOptions": { 13 | "ecmaFeatures":{ 14 | "arrowFunctions": true, 15 | "blockBindings": true, 16 | "classes": true, 17 | "defaultParams": true, 18 | "experimentalObjectRestSpread": true, 19 | "forOf": true, 20 | "generators": true, 21 | "globalReturn": true, 22 | "jsx": true, 23 | "modules": true, 24 | "regexUFlag": true, 25 | "regexYFlag": true, 26 | "restParams": true, 27 | "spread": true, 28 | "superInFunctions": true, 29 | "templateStrings": true, 30 | "unicodeCodePointEscapes": true 31 | }, 32 | "ecmaVersion": 6, 33 | "sourceType": "module" 34 | }, 35 | "rules":{ 36 | "arrow-parens": 2, 37 | "arrow-spacing": [2, { 38 | "after": true, 39 | "before": true 40 | }], 41 | "brace-style": [2, "1tbs", { 42 | "allowSingleLine": true 43 | }], 44 | "comma-dangle": 2, 45 | "comma-spacing": [2, { 46 | "after": true, 47 | "before": false 48 | }], 49 | "computed-property-spacing": [2, "never"], 50 | "constructor-super": 2, 51 | "curly": 2, 52 | "eol-last": 2, 53 | "eqeqeq": 2, 54 | "keyword-spacing": [2, { 55 | "after": true 56 | }], 57 | "newline-after-var": 2, 58 | "generator-star-spacing":0, 59 | "jsx-quotes": [2, "prefer-double"], 60 | "no-array-constructor": 2, 61 | "no-caller": 1, 62 | "no-class-assign": 2, 63 | "no-console": 1, 64 | "no-const-assign": 2, 65 | "no-dupe-args": 2, 66 | "no-dupe-class-members": 2, 67 | "no-dupe-keys": 2, 68 | "no-empty": 2, 69 | "no-eval": 2, 70 | "no-implied-eval": 2, 71 | "no-native-reassign": 2, 72 | "no-new-func": 2, 73 | "no-new-object": 2, 74 | "no-new-symbol": 2, 75 | "no-obj-calls": 2, 76 | "no-proto": 2, 77 | "no-this-before-super": 2, 78 | "no-undef": 2, 79 | "no-unexpected-multiline": 1, 80 | "no-unreachable": 2, 81 | "no-useless-call": 2, 82 | "no-useless-concat": 2, 83 | "no-unused-expressions": 2, 84 | "no-unused-vars": [1, { 85 | "vars":"all", 86 | "varsIgnorePattern": "React|[iI]gnored" 87 | }], 88 | "no-whitespace-before-property": 2, 89 | "quotes": [2, "single", { 90 | "avoidEscape": true, 91 | "allowTemplateLiterals": true 92 | }], 93 | "radix": 2, 94 | "semi": [2, "always"], 95 | "space-before-blocks": [2, { 96 | "classes": "always", 97 | "functions": "always", 98 | "keywords": "always" 99 | }], 100 | "space-before-function-paren": [2, "never"], 101 | "space-in-parens": [2, "never"], 102 | "space-unary-ops": [2, { 103 | "nonwords": false, 104 | "words": true 105 | }], 106 | "template-curly-spacing": [2, "never"], 107 | "valid-jsdoc": [1, { 108 | "requireReturn": false, 109 | "requireParamDescription": false, 110 | "requireReturnDescription": false 111 | }], 112 | "valid-typeof": 2, 113 | "yield-star-spacing": [2, "after"] 114 | } 115 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | node_modules/ 3 | lib/ -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | DEV_ONLY/ 3 | node_modules/ 4 | src/ 5 | test/ 6 | .babelrc 7 | .eslintrc 8 | .gitignore 9 | .npmignore 10 | webpack.config.js 11 | webpack.config.minified.js 12 | webpack.config.dev.js -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | scripts-prepend-node-path=true -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # get-object-class CHANGELOG 2 | 3 | ## 1.0.2 4 | 5 | - Simplify and unify object class construction 6 | - Fix security vulnerability with `webpack-dev-server` 7 | 8 | ## 1.0.1 9 | 10 | - Leverage `typeof` when possible for performance 11 | 12 | ## 1.0.0 13 | 14 | - Initial release 15 | -------------------------------------------------------------------------------- /DEV_ONLY/index.js: -------------------------------------------------------------------------------- 1 | import 'babel-polyfill'; 2 | 3 | import React from 'react'; 4 | import { 5 | render 6 | } from 'react-dom'; 7 | 8 | import goc from '../src/index'; 9 | 10 | console.log('{}', goc({})); 11 | console.log('is {} object', goc.isObject({})); 12 | console.log('is {} boolean', goc.isBoolean({})); 13 | console.log('is true boolean', goc.isBoolean(true)); 14 | console.log('true', goc(true)); 15 | console.log('is true promise', goc.isPromise(true)); 16 | console.log('is Promise.resolve() promise', goc.isPromise(Promise.resolve())); 17 | console.log('Promise.resolve()', goc(Promise.resolve())); 18 | console.log('is document.createElement("div") element', goc.isElement(document.createElement('div'))); 19 | console.log('document.createElement("div")', goc(document.createElement('div'))); 20 | console.log('is "div" element', goc.isElement('dev')); 21 | console.log('json', goc(JSON)); 22 | console.log('is json', goc.isJSON(JSON)); 23 | 24 | const App = () => { 25 | return ( 26 |
27 | App 28 |
29 | ); 30 | }; 31 | 32 | const div = document.createElement('div'); 33 | 34 | div.id = 'app-container'; 35 | 36 | render(( 37 | 38 | ), div); 39 | 40 | document.body.appendChild(div); 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Tony Quetano 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do 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 | # get-object-class 2 | 3 | A more explicit improvement on typeof 4 | 5 | #### Installation 6 | 7 | ``` 8 | $ npm i get-object-class --save 9 | ``` 10 | 11 | #### Usage 12 | 13 | ```javascript 14 | // ES2015 15 | import goc from 'get-object-class'; 16 | 17 | // CommonJS 18 | const goc = require('get-object-class'); 19 | 20 | // script 21 | const goc = window.getObjectClass; 22 | ``` 23 | 24 | #### Implementation 25 | 26 | ```javascript 27 | const array = []; 28 | const promise = Promise.resolve(); 29 | 30 | console.log(goc(array)); // array 31 | console.log(goc(promise)); // promise 32 | ``` 33 | 34 | #### Why do we need this? 35 | 36 | Generally speaking, you can use the [`typeof` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) to determine a number of object classes: 37 | 38 | * boolean 39 | * function 40 | * number 41 | * object 42 | * string 43 | * symbol 44 | * undefined 45 | 46 | However, this list is quite limited, and things can get confusing for other classes of objects: 47 | 48 | ```javascript 49 | console.log(typeof new Date()); // object 50 | console.log(typeof null); // object 51 | ``` 52 | 53 | This library rectifies that by giving you the specific object class for any object (if I missed one tell me, I'll add it): 54 | 55 | * Arguments => `arguments` 56 | * Array => `array` 57 | * ArrayBuffer => `arraybuffer` 58 | * Boolean => `boolean` 59 | * DataView => `dataview` 60 | * Date => `date` 61 | * Error => `error` 62 | * Float32Array => `float32array` 63 | * Float64Array => `float64array` 64 | * Function => `function` 65 | * GeneratorFunction => `generatorfunction` 66 | * global => `global` (specific to node) 67 | * Int8Array => `int8array` 68 | * Int16Array => `int16array` 69 | * Int32Array => `int32array` 70 | * JSON => `json` (tests the JSON object itself, not if the value is a valid JSON string) 71 | * Map => `map` 72 | * Math => `math` 73 | * Null => `null` 74 | * Number => `number` 75 | * Object => `object` 76 | * Promise => `promise` 77 | * RegExp => `regexp` 78 | * Set => `set` 79 | * String => `string` 80 | * Symbol => `symbol` 81 | * Uint8Array => `uint8array` 82 | * Uint8ClampedArray => `uint8Clampedarray` 83 | * Uint16Array => `uint16array` 84 | * Uint32Array => `uint32array` 85 | * WeakMap => `weakmap` 86 | * WeakSet => `weakset` 87 | * Window => `window` (specific to browser) 88 | 89 | #### Checker functions 90 | 91 | `get-object-class` also provides a checker function for each object class, example: 92 | 93 | ```javascript 94 | const array = []; 95 | const boolean = true; 96 | 97 | console.log(goc.isArray(array)); // true 98 | console.log(goc.isBoolean(array)); // false 99 | ``` 100 | 101 | Keep in mind that the name of the function is driven by the PascalCase names in the list above: 102 | 103 | ```javascript 104 | const regexp = /foo/; 105 | 106 | console.log(goc.isRegExp(regexp)); // true 107 | console.log(goc.isFloat32Array(regexp)); // false 108 | console.log(goc.isJSON(regexp)); // false 109 | ``` 110 | 111 | ### Development 112 | 113 | Standard stuff, clone the repo and `npm i` to get the dependencies. npm scripts available: 114 | * `build` => builds the distributed JS with `NODE_ENV=development` and with sourcemaps 115 | * `build-minified` => builds the distributed JS with `NODE_ENV=production` and minified 116 | * `compile-for-publish` => runs the `lint`, `test`, `transpile`, `build`, and `build-minified` scripts 117 | * `dev` => runs the webpack dev server for the playground 118 | * `lint` => runs ESLint against files in the `src` folder 119 | * `prepublish` => if in publish, runs `compile-for-publish` 120 | * `test` => run ava with NODE_ENV=test 121 | * `test:watch` => runs `test` but with persistent watcher 122 | * `transpile` => runs Babel against files in `src` to files in `lib` 123 | -------------------------------------------------------------------------------- /dist/get-object-class.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(); 4 | else if(typeof define === 'function' && define.amd) 5 | define("getObjectClass", [], factory); 6 | else if(typeof exports === 'object') 7 | exports["getObjectClass"] = factory(); 8 | else 9 | root["getObjectClass"] = factory(); 10 | })(window, function() { 11 | return /******/ (function(modules) { // webpackBootstrap 12 | /******/ // The module cache 13 | /******/ var installedModules = {}; 14 | /******/ 15 | /******/ // The require function 16 | /******/ function __webpack_require__(moduleId) { 17 | /******/ 18 | /******/ // Check if module is in cache 19 | /******/ if(installedModules[moduleId]) { 20 | /******/ return installedModules[moduleId].exports; 21 | /******/ } 22 | /******/ // Create a new module (and put it into the cache) 23 | /******/ var module = installedModules[moduleId] = { 24 | /******/ i: moduleId, 25 | /******/ l: false, 26 | /******/ exports: {} 27 | /******/ }; 28 | /******/ 29 | /******/ // Execute the module function 30 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 31 | /******/ 32 | /******/ // Flag the module as loaded 33 | /******/ module.l = true; 34 | /******/ 35 | /******/ // Return the exports of the module 36 | /******/ return module.exports; 37 | /******/ } 38 | /******/ 39 | /******/ 40 | /******/ // expose the modules object (__webpack_modules__) 41 | /******/ __webpack_require__.m = modules; 42 | /******/ 43 | /******/ // expose the module cache 44 | /******/ __webpack_require__.c = installedModules; 45 | /******/ 46 | /******/ // define getter function for harmony exports 47 | /******/ __webpack_require__.d = function(exports, name, getter) { 48 | /******/ if(!__webpack_require__.o(exports, name)) { 49 | /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); 50 | /******/ } 51 | /******/ }; 52 | /******/ 53 | /******/ // define __esModule on exports 54 | /******/ __webpack_require__.r = function(exports) { 55 | /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { 56 | /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); 57 | /******/ } 58 | /******/ Object.defineProperty(exports, '__esModule', { value: true }); 59 | /******/ }; 60 | /******/ 61 | /******/ // create a fake namespace object 62 | /******/ // mode & 1: value is a module id, require it 63 | /******/ // mode & 2: merge all properties of value into the ns 64 | /******/ // mode & 4: return value when already ns object 65 | /******/ // mode & 8|1: behave like require 66 | /******/ __webpack_require__.t = function(value, mode) { 67 | /******/ if(mode & 1) value = __webpack_require__(value); 68 | /******/ if(mode & 8) return value; 69 | /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; 70 | /******/ var ns = Object.create(null); 71 | /******/ __webpack_require__.r(ns); 72 | /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); 73 | /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); 74 | /******/ return ns; 75 | /******/ }; 76 | /******/ 77 | /******/ // getDefaultExport function for compatibility with non-harmony modules 78 | /******/ __webpack_require__.n = function(module) { 79 | /******/ var getter = module && module.__esModule ? 80 | /******/ function getDefault() { return module['default']; } : 81 | /******/ function getModuleExports() { return module; }; 82 | /******/ __webpack_require__.d(getter, 'a', getter); 83 | /******/ return getter; 84 | /******/ }; 85 | /******/ 86 | /******/ // Object.prototype.hasOwnProperty.call 87 | /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; 88 | /******/ 89 | /******/ // __webpack_public_path__ 90 | /******/ __webpack_require__.p = ""; 91 | /******/ 92 | /******/ 93 | /******/ // Load entry module and return exports 94 | /******/ return __webpack_require__(__webpack_require__.s = 0); 95 | /******/ }) 96 | /************************************************************************/ 97 | /******/ ({ 98 | 99 | /***/ "./src/constants.js": 100 | /*!**************************!*\ 101 | !*** ./src/constants.js ***! 102 | \**************************/ 103 | /*! no static exports found */ 104 | /***/ (function(module, exports, __webpack_require__) { 105 | 106 | "use strict"; 107 | 108 | 109 | exports.__esModule = true; 110 | var CLASS_NAMES = exports.CLASS_NAMES = ['Arguments', 'Array', 'ArrayBuffer', 'Boolean', 'DataView', 'Date', 'Error', 'Float32Array', 'Float64Array', 'Function', 'GeneratorFunction', 'global', 'Int8Array', 'Int16Array', 'Int32Array', 'JSON', 'Map', 'Math', 'Null', 'Number', 'Object', 'Promise', 'RegExp', 'Set', 'String', 'Symbol', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'Undefined', 'WeakMap', 'WeakSet', 'Window']; 111 | 112 | var HTML_ELEMENT_REGEXP = exports.HTML_ELEMENT_REGEXP = /HTML(.*)Element/; 113 | 114 | /***/ }), 115 | 116 | /***/ "./src/getClassName.js": 117 | /*!*****************************!*\ 118 | !*** ./src/getClassName.js ***! 119 | \*****************************/ 120 | /*! no static exports found */ 121 | /***/ (function(module, exports, __webpack_require__) { 122 | 123 | "use strict"; 124 | 125 | 126 | exports.__esModule = true; 127 | exports.getIsElement = exports.getClassName = undefined; 128 | 129 | var _constants = __webpack_require__(/*! ./constants */ "./src/constants.js"); 130 | 131 | var toString = Object.prototype.toString; 132 | 133 | /** 134 | * get the object prototype toString of the object passed, and return 135 | * the classname from that string 136 | * 137 | * @param {*} object 138 | * @returns {string} 139 | */ 140 | // constants 141 | var getClassName = exports.getClassName = function getClassName(object) { 142 | var objectToString = toString.call(object); 143 | 144 | return objectToString.substring(8, objectToString.length - 1); 145 | }; 146 | 147 | /** 148 | * determine if the object passed is an element based on its object class 149 | * 150 | * @param {*} object 151 | * @returns {boolean} 152 | */ 153 | var getIsElement = exports.getIsElement = function getIsElement(object) { 154 | return _constants.HTML_ELEMENT_REGEXP.test(getClassName(object)) || !!object && object.nodeType === 1; 155 | }; 156 | 157 | /***/ }), 158 | 159 | /***/ "./src/index.js": 160 | /*!**********************!*\ 161 | !*** ./src/index.js ***! 162 | \**********************/ 163 | /*! no static exports found */ 164 | /***/ (function(module, exports, __webpack_require__) { 165 | 166 | "use strict"; 167 | 168 | 169 | exports.__esModule = true; 170 | 171 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; // constants 172 | 173 | 174 | // utils 175 | 176 | 177 | var _constants = __webpack_require__(/*! ./constants */ "./src/constants.js"); 178 | 179 | var _getClassName = __webpack_require__(/*! ./getClassName */ "./src/getClassName.js"); 180 | 181 | /** 182 | * return the lowercase object class name for the 183 | * object provided 184 | * 185 | * @param {*} object 186 | * @returns {string} 187 | */ 188 | var getObjectClass = function getObjectClass(object) { 189 | if (object === null) { 190 | return 'null'; 191 | } 192 | 193 | if ((0, _getClassName.getIsElement)(object)) { 194 | return 'element'; 195 | } 196 | 197 | var type = typeof object === 'undefined' ? 'undefined' : _typeof(object); 198 | 199 | return type === 'object' || type === 'function' ? (0, _getClassName.getClassName)(object).toLowerCase() : type; 200 | }; 201 | 202 | /** 203 | * create the is checkers for all object classes saved in CLASS_NAMES, 204 | * which all accept {any} object and return {string} 205 | */ 206 | _constants.CLASS_NAMES.forEach(function (className) { 207 | var lowerCaseClassName = className.toLowerCase(); 208 | 209 | getObjectClass['is' + className] = function (object) { 210 | return getObjectClass(object) === lowerCaseClassName; 211 | }; 212 | }); 213 | 214 | /** 215 | * isElement is unique because there can be a wealth of object class 216 | * names associated with it, so it gets created separately 217 | * 218 | * @param {*} object 219 | * @returns {boolean} 220 | */ 221 | getObjectClass.isElement = _getClassName.getIsElement; 222 | 223 | exports.default = getObjectClass; 224 | module.exports = exports['default']; 225 | 226 | /***/ }), 227 | 228 | /***/ 0: 229 | /*!****************************!*\ 230 | !*** multi ./src/index.js ***! 231 | \****************************/ 232 | /*! no static exports found */ 233 | /***/ (function(module, exports, __webpack_require__) { 234 | 235 | module.exports = __webpack_require__(/*! /home/tquetano/git/get-object-class/src/index.js */"./src/index.js"); 236 | 237 | 238 | /***/ }) 239 | 240 | /******/ }); 241 | }); 242 | //# sourceMappingURL=get-object-class.js.map -------------------------------------------------------------------------------- /dist/get-object-class.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"sources":["webpack://getObjectClass/webpack/universalModuleDefinition","webpack://getObjectClass/webpack/bootstrap","webpack://getObjectClass/./src/constants.js","webpack://getObjectClass/./src/getClassName.js","webpack://getObjectClass/./src/index.js"],"names":["CLASS_NAMES","HTML_ELEMENT_REGEXP","toString","Object","prototype","getClassName","object","objectToString","call","substring","length","getIsElement","test","nodeType","getObjectClass","type","toLowerCase","forEach","className","lowerCaseClassName","isElement"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;ACVA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA,kDAA0C,gCAAgC;AAC1E;AACA;;AAEA;AACA;AACA;AACA,gEAAwD,kBAAkB;AAC1E;AACA,yDAAiD,cAAc;AAC/D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iDAAyC,iCAAiC;AAC1E,wHAAgH,mBAAmB,EAAE;AACrI;AACA;;AAEA;AACA;AACA;AACA,mCAA2B,0BAA0B,EAAE;AACvD,yCAAiC,eAAe;AAChD;AACA;AACA;;AAEA;AACA,8DAAsD,+DAA+D;;AAErH;AACA;;;AAGA;AACA;;;;;;;;;;;;;;;;AClFO,IAAMA,oCAAc,CACzB,WADyB,EAEzB,OAFyB,EAGzB,aAHyB,EAIzB,SAJyB,EAKzB,UALyB,EAMzB,MANyB,EAOzB,OAPyB,EAQzB,cARyB,EASzB,cATyB,EAUzB,UAVyB,EAWzB,mBAXyB,EAYzB,QAZyB,EAazB,WAbyB,EAczB,YAdyB,EAezB,YAfyB,EAgBzB,MAhByB,EAiBzB,KAjByB,EAkBzB,MAlByB,EAmBzB,MAnByB,EAoBzB,QApByB,EAqBzB,QArByB,EAsBzB,SAtByB,EAuBzB,QAvByB,EAwBzB,KAxByB,EAyBzB,QAzByB,EA0BzB,QA1ByB,EA2BzB,YA3ByB,EA4BzB,mBA5ByB,EA6BzB,aA7ByB,EA8BzB,aA9ByB,EA+BzB,WA/ByB,EAgCzB,SAhCyB,EAiCzB,SAjCyB,EAkCzB,QAlCyB,CAApB;;AAqCA,IAAMC,oDAAsB,iBAA5B,C;;;;;;;;;;;;;;;;;ACpCP;;AAEA,IAAMC,WAAWC,OAAOC,SAAP,CAAiBF,QAAlC;;AAEA;;;;;;;AALA;AAYO,IAAMG,sCAAe,SAAfA,YAAe,CAACC,MAAD,EAAY;AACtC,MAAMC,iBAAiBL,SAASM,IAAT,CAAcF,MAAd,CAAvB;;AAEA,SAAOC,eAAeE,SAAf,CAAyB,CAAzB,EAA4BF,eAAeG,MAAf,GAAwB,CAApD,CAAP;AACD,CAJM;;AAMP;;;;;;AAMO,IAAMC,sCAAe,SAAfA,YAAe,CAACL,MAAD,EAAY;AACtC,SACEL,+BAAoBW,IAApB,CAAyBP,aAAaC,MAAb,CAAzB,KACC,CAAC,CAACA,MAAF,IAAYA,OAAOO,QAAP,KAAoB,CAFnC;AAID,CALM,C;;;;;;;;;;;;;;;;8QCxBP;;;AAGA;;;AAFA;;AAGA;;AAEA;;;;;;;AAOA,IAAMC,iBAAiB,SAAjBA,cAAiB,CAACR,MAAD,EAAY;AACjC,MAAIA,WAAW,IAAf,EAAqB;AACnB,WAAO,MAAP;AACD;;AAED,MAAI,gCAAaA,MAAb,CAAJ,EAA0B;AACxB,WAAO,SAAP;AACD;;AAED,MAAMS,cAAcT,MAAd,yCAAcA,MAAd,CAAN;;AAEA,SAAOS,SAAS,QAAT,IAAqBA,SAAS,UAA9B,GACH,gCAAaT,MAAb,EAAqBU,WAArB,EADG,GAEHD,IAFJ;AAGD,CAdD;;AAgBA;;;;AAIAf,uBAAYiB,OAAZ,CAAoB,UAACC,SAAD,EAAe;AACjC,MAAMC,qBAAqBD,UAAUF,WAAV,EAA3B;;AAEAF,wBAAoBI,SAApB,IAAmC,UAACZ,MAAD;AAAA,WACjCQ,eAAeR,MAAf,MAA2Ba,kBADM;AAAA,GAAnC;AAED,CALD;;AAOA;;;;;;;AAOAL,eAAeM,SAAf,GAA2BT,0BAA3B;;kBAEeG,c","file":"get-object-class.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine(\"getObjectClass\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"getObjectClass\"] = factory();\n\telse\n\t\troot[\"getObjectClass\"] = factory();\n})(window, function() {\nreturn "," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n","export const CLASS_NAMES = [\n 'Arguments',\n 'Array',\n 'ArrayBuffer',\n 'Boolean',\n 'DataView',\n 'Date',\n 'Error',\n 'Float32Array',\n 'Float64Array',\n 'Function',\n 'GeneratorFunction',\n 'global',\n 'Int8Array',\n 'Int16Array',\n 'Int32Array',\n 'JSON',\n 'Map',\n 'Math',\n 'Null',\n 'Number',\n 'Object',\n 'Promise',\n 'RegExp',\n 'Set',\n 'String',\n 'Symbol',\n 'Uint8Array',\n 'Uint8ClampedArray',\n 'Uint16Array',\n 'Uint32Array',\n 'Undefined',\n 'WeakMap',\n 'WeakSet',\n 'Window'\n];\n\nexport const HTML_ELEMENT_REGEXP = /HTML(.*)Element/;\n","// constants\nimport { HTML_ELEMENT_REGEXP } from './constants';\n\nconst toString = Object.prototype.toString;\n\n/**\n * get the object prototype toString of the object passed, and return\n * the classname from that string\n *\n * @param {*} object\n * @returns {string}\n */\nexport const getClassName = (object) => {\n const objectToString = toString.call(object);\n\n return objectToString.substring(8, objectToString.length - 1);\n};\n\n/**\n * determine if the object passed is an element based on its object class\n *\n * @param {*} object\n * @returns {boolean}\n */\nexport const getIsElement = (object) => {\n return (\n HTML_ELEMENT_REGEXP.test(getClassName(object)) ||\n (!!object && object.nodeType === 1)\n );\n};\n","// constants\nimport { CLASS_NAMES } from './constants';\n\n// utils\nimport { getClassName, getIsElement } from './getClassName';\n\n/**\n * return the lowercase object class name for the\n * object provided\n *\n * @param {*} object\n * @returns {string}\n */\nconst getObjectClass = (object) => {\n if (object === null) {\n return 'null';\n }\n\n if (getIsElement(object)) {\n return 'element';\n }\n\n const type = typeof object;\n\n return type === 'object' || type === 'function'\n ? getClassName(object).toLowerCase()\n : type;\n};\n\n/**\n * create the is checkers for all object classes saved in CLASS_NAMES,\n * which all accept {any} object and return {string}\n */\nCLASS_NAMES.forEach((className) => {\n const lowerCaseClassName = className.toLowerCase();\n\n getObjectClass[`is${className}`] = (object) =>\n getObjectClass(object) === lowerCaseClassName;\n});\n\n/**\n * isElement is unique because there can be a wealth of object class\n * names associated with it, so it gets created separately\n *\n * @param {*} object\n * @returns {boolean}\n */\ngetObjectClass.isElement = getIsElement;\n\nexport default getObjectClass;\n"],"sourceRoot":""} -------------------------------------------------------------------------------- /dist/get-object-class.min.js: -------------------------------------------------------------------------------- 1 | !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("getObjectClass",[],t):"object"==typeof exports?exports.getObjectClass=t():e.getObjectClass=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){"use strict";t.__esModule=!0;t.CLASS_NAMES=["Arguments","Array","ArrayBuffer","Boolean","DataView","Date","Error","Float32Array","Float64Array","Function","GeneratorFunction","global","Int8Array","Int16Array","Int32Array","JSON","Map","Math","Null","Number","Object","Promise","RegExp","Set","String","Symbol","Uint8Array","Uint8ClampedArray","Uint16Array","Uint32Array","Undefined","WeakMap","WeakSet","Window"],t.HTML_ELEMENT_REGEXP=/HTML(.*)Element/},function(e,t,n){e.exports=n(2)},function(e,t,n){"use strict";t.__esModule=!0;var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o=n(0),u=n(3),i=function(e){if(null===e)return"null";if((0,u.getIsElement)(e))return"element";var t=void 0===e?"undefined":r(e);return"object"===t||"function"===t?(0,u.getClassName)(e).toLowerCase():t};o.CLASS_NAMES.forEach(function(e){var t=e.toLowerCase();i["is"+e]=function(e){return i(e)===t}}),i.isElement=u.getIsElement,t.default=i,e.exports=t.default},function(e,t,n){"use strict";t.__esModule=!0,t.getIsElement=t.getClassName=void 0;var r=n(0),o=Object.prototype.toString,u=t.getClassName=function(e){var t=o.call(e);return t.substring(8,t.length-1)};t.getIsElement=function(e){return r.HTML_ELEMENT_REGEXP.test(u(e))||!!e&&1===e.nodeType}}])}); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "planttheidea", 3 | "ava": { 4 | "babel": "inherit", 5 | "concurrency": 5, 6 | "failFast": true, 7 | "files": [ 8 | "test/**/*.js" 9 | ], 10 | "require": [ 11 | "babel-register", 12 | "babel-polyfill", 13 | "./test/helpers/setup-browser-env.js" 14 | ], 15 | "verbose": true 16 | }, 17 | "bugs": { 18 | "url": "https://github.com/planttheidea/get-object-class/issues" 19 | }, 20 | "description": "Get the class type of any object", 21 | "homepage": "https://github.com/planttheidea/get-object-class#readme", 22 | "keywords": [ 23 | "typeof", 24 | "type-of", 25 | "object", 26 | "class", 27 | "get-object-class" 28 | ], 29 | "license": "MIT", 30 | "main": "lib/index.js", 31 | "name": "get-object-class", 32 | "repository": { 33 | "type": "git", 34 | "url": "git+https://github.com/planttheidea/get-object-class.git" 35 | }, 36 | "scripts": { 37 | "build": "NODE_ENV=development webpack --progress --colors", 38 | "build-minified": "NODE_ENV=production webpack --progress --colors --config=webpack.config.minified.js", 39 | "compile-for-publish": "npm run lint && npm run test && npm run transpile && npm run build && npm run build-minified", 40 | "dev": "NODE_ENV=development webpack-dev-server --progress --colors --config=webpack.config.dev.js", 41 | "lint": "NODE_ENV=test eslint src", 42 | "prepublish": "in-publish && npm run compile-for-publish || in-install", 43 | "test": "NODE_ENV=test ava --no-cache", 44 | "test:watch": "NODE_ENV=test ava --no-cache --watch", 45 | "transpile": "babel src --out-dir lib" 46 | }, 47 | "version": "1.0.2", 48 | "devDependencies": { 49 | "ava": "^0.25.0", 50 | "babel-cli": "^6.26.0", 51 | "babel-eslint": "^8.2.6", 52 | "babel-loader": "^7.1.5", 53 | "babel-plugin-add-module-exports": "^0.2.1", 54 | "babel-polyfill": "^6.26.0", 55 | "babel-preset-env": "^1.7.0", 56 | "babel-preset-react": "^6.24.1", 57 | "babel-preset-stage-2": "^6.24.1", 58 | "babel-register": "^6.26.0", 59 | "eslint": "^5.12.0", 60 | "eslint-friendly-formatter": "^4.0.1", 61 | "eslint-loader": "^2.1.1", 62 | "html-webpack-plugin": "^3.2.0", 63 | "in-publish": "^2.0.0", 64 | "jsdom": "^9.8.0", 65 | "lodash": "^4.16.4", 66 | "react": "^16.7.0", 67 | "react-dom": "^16.7.0", 68 | "webpack": "^4.28.3", 69 | "webpack-cli": "^3.2.0", 70 | "webpack-dev-server": "^3.1.14" 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/constants.js: -------------------------------------------------------------------------------- 1 | export const CLASS_NAMES = [ 2 | 'Arguments', 3 | 'Array', 4 | 'ArrayBuffer', 5 | 'Boolean', 6 | 'DataView', 7 | 'Date', 8 | 'Error', 9 | 'Float32Array', 10 | 'Float64Array', 11 | 'Function', 12 | 'GeneratorFunction', 13 | 'global', 14 | 'Int8Array', 15 | 'Int16Array', 16 | 'Int32Array', 17 | 'JSON', 18 | 'Map', 19 | 'Math', 20 | 'Null', 21 | 'Number', 22 | 'Object', 23 | 'Promise', 24 | 'RegExp', 25 | 'Set', 26 | 'String', 27 | 'Symbol', 28 | 'Uint8Array', 29 | 'Uint8ClampedArray', 30 | 'Uint16Array', 31 | 'Uint32Array', 32 | 'Undefined', 33 | 'WeakMap', 34 | 'WeakSet', 35 | 'Window' 36 | ]; 37 | 38 | export const HTML_ELEMENT_REGEXP = /HTML(.*)Element/; 39 | -------------------------------------------------------------------------------- /src/getClassName.js: -------------------------------------------------------------------------------- 1 | // constants 2 | import { HTML_ELEMENT_REGEXP } from './constants'; 3 | 4 | const toString = Object.prototype.toString; 5 | 6 | /** 7 | * get the object prototype toString of the object passed, and return 8 | * the classname from that string 9 | * 10 | * @param {*} object 11 | * @returns {string} 12 | */ 13 | export const getClassName = (object) => { 14 | const objectToString = toString.call(object); 15 | 16 | return objectToString.substring(8, objectToString.length - 1); 17 | }; 18 | 19 | /** 20 | * determine if the object passed is an element based on its object class 21 | * 22 | * @param {*} object 23 | * @returns {boolean} 24 | */ 25 | export const getIsElement = (object) => { 26 | return ( 27 | HTML_ELEMENT_REGEXP.test(getClassName(object)) || 28 | (!!object && object.nodeType === 1) 29 | ); 30 | }; 31 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // constants 2 | import { CLASS_NAMES } from './constants'; 3 | 4 | // utils 5 | import { getClassName, getIsElement } from './getClassName'; 6 | 7 | /** 8 | * return the lowercase object class name for the 9 | * object provided 10 | * 11 | * @param {*} object 12 | * @returns {string} 13 | */ 14 | const getObjectClass = (object) => { 15 | if (object === null) { 16 | return 'null'; 17 | } 18 | 19 | if (getIsElement(object)) { 20 | return 'element'; 21 | } 22 | 23 | const type = typeof object; 24 | 25 | return type === 'object' || type === 'function' 26 | ? getClassName(object).toLowerCase() 27 | : type; 28 | }; 29 | 30 | /** 31 | * create the is checkers for all object classes saved in CLASS_NAMES, 32 | * which all accept {any} object and return {string} 33 | */ 34 | CLASS_NAMES.forEach((className) => { 35 | const lowerCaseClassName = className.toLowerCase(); 36 | 37 | getObjectClass[`is${className}`] = (object) => 38 | getObjectClass(object) === lowerCaseClassName; 39 | }); 40 | 41 | /** 42 | * isElement is unique because there can be a wealth of object class 43 | * names associated with it, so it gets created separately 44 | * 45 | * @param {*} object 46 | * @returns {boolean} 47 | */ 48 | getObjectClass.isElement = getIsElement; 49 | 50 | export default getObjectClass; 51 | -------------------------------------------------------------------------------- /test/getClassName.js: -------------------------------------------------------------------------------- 1 | import snakeCase from 'lodash/snakeCase'; 2 | import test from 'ava'; 3 | 4 | import { getClassName, getIsElement } from '../src/getClassName'; 5 | import { CLASS_NAMES } from '../src/constants'; 6 | 7 | import * as data from './helpers/fakeData'; 8 | 9 | data.JSON = JSON; 10 | 11 | // window can't be tested here because it is in node 12 | const classNamesWithoutWindowSpecificProperties = CLASS_NAMES.filter( 13 | (className) => { 14 | return className !== 'Window'; 15 | } 16 | ); 17 | 18 | test("if getClassName returns the Startcase version of the object's class type", (t) => { 19 | let screamingSnakeClassName; 20 | 21 | classNamesWithoutWindowSpecificProperties.forEach((className) => { 22 | screamingSnakeClassName = snakeCase(className).toUpperCase(); 23 | 24 | t.is(getClassName(data[screamingSnakeClassName], true), className); 25 | }); 26 | }); 27 | 28 | test('if getIsElement returns true for elements but false for all else', (t) => { 29 | for (let key in data) { 30 | t.false(getIsElement(data[key])); 31 | } 32 | 33 | const div = document.createElement('div'); 34 | 35 | t.true(getIsElement(div)); 36 | }); 37 | -------------------------------------------------------------------------------- /test/helpers/fakeData.js: -------------------------------------------------------------------------------- 1 | let ARGUMENTS; 2 | 3 | const argumentsFunc = function() { 4 | ARGUMENTS = arguments; 5 | }; 6 | 7 | argumentsFunc(); 8 | 9 | export { ARGUMENTS }; 10 | 11 | export const ARRAY = []; 12 | export const ARRAY_BUFFER = new ArrayBuffer(8); 13 | export const BOOLEAN = true; 14 | export const DATA_VIEW = new DataView(new ArrayBuffer(2)); 15 | export const DATE = new Date(); 16 | export const ERROR = new Error('foo'); 17 | export const FLOAT_32_ARRAY = new Float32Array(8); 18 | export const FLOAT_64_ARRAY = new Float64Array(8); 19 | export const FUNCTION = function() {}; 20 | export const GENERATOR_FUNCTION = function*() {}; 21 | export const GLOBAL = global; 22 | export const INT_8_ARRAY = new Int8Array(8); 23 | export const INT_16_ARRAY = new Int16Array(8); 24 | export const INT_32_ARRAY = new Int32Array(8); 25 | export const MAP = new Map(); 26 | export const MATH = Math; 27 | export const NULL = null; 28 | export const NUMBER = 2; 29 | export const OBJECT = {}; 30 | export const PROMISE = Promise.resolve(); 31 | export const REG_EXP = new RegExp(); 32 | export const SET = new Set(); 33 | export const STRING = 'foo'; 34 | export const SYMBOL = Symbol('foo'); 35 | export const UINT_8_ARRAY = new Uint8Array(8); 36 | export const UINT_8_CLAMPED_ARRAY = new Uint8ClampedArray(8); 37 | export const UINT_16_ARRAY = new Uint16Array(8); 38 | export const UINT_32_ARRAY = new Uint32Array(8); 39 | export const UNDEFINED = undefined; 40 | export const WEAK_MAP = new WeakMap(); 41 | export const WEAK_SET = new WeakSet(); 42 | export const WINDOW = window; 43 | -------------------------------------------------------------------------------- /test/helpers/setup-browser-env.js: -------------------------------------------------------------------------------- 1 | global.document = require('jsdom').jsdom(''); 2 | global.window = document.defaultView; 3 | global.navigator = window.navigator; -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | import camelCase from 'lodash/camelCase'; 2 | import test from 'ava'; 3 | 4 | import goc from '../src/index'; 5 | 6 | import { CLASS_NAMES } from '../src/constants'; 7 | 8 | import * as data from './helpers/fakeData'; 9 | 10 | data.JSON = JSON; 11 | 12 | const classNamesWithoutWindow = CLASS_NAMES.filter((className) => { 13 | return className !== 'Window'; 14 | }); 15 | 16 | const dataKeys = Object.keys(data).filter((dataKey) => { 17 | return dataKey.toUpperCase() === dataKey && dataKey !== 'WINDOW'; 18 | }); 19 | 20 | test('if main function returns the correct value', (t) => { 21 | dataKeys.forEach((dataKey) => { 22 | const className = camelCase(dataKey).toLowerCase(); 23 | 24 | t.is(goc(data[dataKey]), className, className); 25 | }); 26 | }); 27 | 28 | test('if each checker function returns the correct boolean value for the object type', (t) => { 29 | classNamesWithoutWindow.forEach((className) => { 30 | const lowerCaseClassName = className.toLowerCase(); 31 | 32 | dataKeys.forEach((dataKey) => { 33 | const dataClassName = camelCase(dataKey).toLowerCase(); 34 | const expectedResult = dataClassName === lowerCaseClassName; 35 | 36 | t[expectedResult](goc[`is${className}`](data[dataKey]), dataKey); 37 | }); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /webpack.config.dev.js: -------------------------------------------------------------------------------- 1 | const HtmlWebpackPlugin = require('html-webpack-plugin'); 2 | const path = require('path'); 3 | 4 | const PORT = 3000; 5 | 6 | const defaultConfig = require('./webpack.config'); 7 | 8 | module.exports = Object.assign({}, defaultConfig, { 9 | devServer: { 10 | contentBase: './dist', 11 | host: 'localhost', 12 | inline: true, 13 | lazy: false, 14 | noInfo: false, 15 | quiet: false, 16 | port: PORT, 17 | stats: { 18 | colors: true, 19 | progress: true 20 | } 21 | }, 22 | 23 | entry: [path.resolve(__dirname, 'DEV_ONLY', 'index.js')], 24 | 25 | plugins: defaultConfig.plugins.concat([new HtmlWebpackPlugin()]) 26 | }); 27 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | 4 | module.exports = { 5 | devtool: '#source-map', 6 | 7 | entry: [path.resolve(__dirname, 'src', 'index.js')], 8 | 9 | mode: 'development', 10 | 11 | module: { 12 | rules: [ 13 | { 14 | enforce: 'pre', 15 | include: [path.resolve(__dirname, 'src')], 16 | loader: 'eslint-loader', 17 | options: { 18 | configFile: '.eslintrc', 19 | failOnError: true, 20 | failOnWarning: false, 21 | formatter: require('eslint-friendly-formatter') 22 | }, 23 | test: /\.js$/ 24 | }, 25 | { 26 | include: [ 27 | path.resolve(__dirname, 'src'), 28 | path.resolve(__dirname, 'DEV_ONLY') 29 | ], 30 | loader: 'babel-loader', 31 | test: /\.js$/ 32 | } 33 | ] 34 | }, 35 | 36 | output: { 37 | filename: 'get-object-class.js', 38 | library: 'getObjectClass', 39 | libraryTarget: 'umd', 40 | path: path.resolve(__dirname, 'dist'), 41 | umdNamedDefine: true 42 | }, 43 | 44 | plugins: [new webpack.EnvironmentPlugin(['NODE_ENV'])] 45 | }; 46 | -------------------------------------------------------------------------------- /webpack.config.minified.js: -------------------------------------------------------------------------------- 1 | const defaultConfig = require('./webpack.config'); 2 | 3 | module.exports = Object.assign({}, defaultConfig, { 4 | devtool: undefined, 5 | 6 | mode: 'production', 7 | 8 | output: Object.assign({}, defaultConfig.output, { 9 | filename: 'get-object-class.min.js' 10 | }) 11 | }); 12 | --------------------------------------------------------------------------------