diff --git a/benchmarks/package.json b/benchmarks/package.json deleted file mode 100644 index 6990891..0000000 --- a/benchmarks/package.json +++ /dev/null @@ -1 +0,0 @@ -{"type": "module"} diff --git a/bind.d.ts b/bind.d.ts index f89f787..ca4c0d8 100644 --- a/bind.d.ts +++ b/bind.d.ts @@ -1,17 +1,3 @@ import { ArgumentArray } from './index.js'; - -declare namespace classNames { - type Binding = Record; -} - -interface ClassNames { - (this: classNames.Binding | undefined, ...args: ArgumentArray): string; - - default: ClassNames; -} - -declare const classNames: ClassNames; - -export as namespace classNames; - -export = classNames; +export type Binding = Record; +export default function classNames(this: Binding | undefined, ...args: ArgumentArray): string; diff --git a/bind.js b/bind.js index 3add1d6..462ac45 100644 --- a/bind.js +++ b/bind.js @@ -1,77 +1,54 @@ -/*! - Copyright (c) 2018 Jed Watson. - Licensed under the MIT License (MIT), see - http://jedwatson.github.io/classnames -*/ -/* global define */ +const hasOwn = {}.hasOwnProperty; -(function () { - 'use strict'; +export default function classNames () { + let classes = ''; - var hasOwn = {}.hasOwnProperty; + for (let i = 0; i < arguments.length; i++) { + const arg = arguments[i]; + if (arg) { + classes = appendClass(classes, parseValue.call(this, arg)); + } + } - function classNames () { - var classes = ''; + return classes; +} - for (var i = 0; i < arguments.length; i++) { - var arg = arguments[i]; - if (arg) { - classes = appendClass(classes, parseValue.call(this, arg)); - } - } +function parseValue (arg) { + if (typeof arg === 'string' || typeof arg === 'number') { + return this && this[arg] || arg; + } - return classes; + if (typeof arg !== 'object') { + return ''; } - function parseValue (arg) { - if (typeof arg === 'string' || typeof arg === 'number') { - return this && this[arg] || arg; - } + if (Array.isArray(arg)) { + return classNames.apply(this, arg); + } - if (typeof arg !== 'object') { - return ''; - } + if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) { + return arg.toString(); + } - if (Array.isArray(arg)) { - return classNames.apply(this, arg); - } + let classes = ''; - if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) { - return arg.toString(); + for (const key in arg) { + if (hasOwn.call(arg, key) && arg[key]) { + classes = appendClass(classes, this && this[key] || key); } + } - var classes = ''; - - for (var key in arg) { - if (hasOwn.call(arg, key) && arg[key]) { - classes = appendClass(classes, this && this[key] || key); - } - } + return classes; +} - return classes; +function appendClass (value, newClass) { + if (!newClass) { + return value; } - function appendClass (value, newClass) { - if (!newClass) { - return value; - } - - if (value) { - return value + ' ' + newClass; - } - - return value + newClass; + if (value) { + return value + ' ' + newClass; } - if (typeof module !== 'undefined' && module.exports) { - classNames.default = classNames; - module.exports = classNames; - } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) { - // register as 'classnames', consistent with npm package name - define('classnames', [], function () { - return classNames; - }); - } else { - window.classNames = classNames; - } -}()); + return value + newClass; +} diff --git a/dedupe.d.ts b/dedupe.d.ts index 392248a..b27b8eb 100644 --- a/dedupe.d.ts +++ b/dedupe.d.ts @@ -1,5 +1,2 @@ -import classNames = require('./index.js'); - -export as namespace classNames; - -export = classNames; +import classNames from './index.js'; +export default classNames; diff --git a/dedupe.js b/dedupe.js index 3a7264b..7d20bdf 100644 --- a/dedupe.js +++ b/dedupe.js @@ -1,115 +1,94 @@ -/*! - Copyright (c) 2018 Jed Watson. - Licensed under the MIT License (MIT), see - http://jedwatson.github.io/classnames -*/ -/* global define */ - -(function () { - 'use strict'; - - var classNames = (function () { - // don't inherit from Object so we can skip hasOwnProperty check later - // http://stackoverflow.com/questions/15518328/creating-js-object-with-object-createnull#answer-21079232 - function StorageObject() {} - StorageObject.prototype = Object.create(null); - - function _parseArray (resultSet, array) { - var length = array.length; - - for (var i = 0; i < length; ++i) { - _parse(resultSet, array[i]); - } - } +const classNames = (function () { + // don't inherit from Object so we can skip hasOwnProperty check later + // http://stackoverflow.com/questions/15518328/creating-js-object-with-object-createnull#answer-21079232 + function StorageObject() {} + StorageObject.prototype = Object.create(null); - var hasOwn = {}.hasOwnProperty; + function _parseArray (resultSet, array) { + const length = array.length; - function _parseNumber (resultSet, num) { - resultSet[num] = true; + for (let i = 0; i < length; ++i) { + _parse(resultSet, array[i]); } + } - function _parseObject (resultSet, object) { - if (object.toString !== Object.prototype.toString && !object.toString.toString().includes('[native code]')) { - resultSet[object.toString()] = true; - return; - } + const hasOwn = {}.hasOwnProperty; - for (var k in object) { - if (hasOwn.call(object, k)) { - // set value to false instead of deleting it to avoid changing object structure - // https://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions - resultSet[k] = !!object[k]; - } + function _parseNumber (resultSet, num) { + resultSet[num] = true; + } + + function _parseObject (resultSet, object) { + if (object.toString !== Object.prototype.toString && !object.toString.toString().includes('[native code]')) { + resultSet[object.toString()] = true; + return; + } + + for (const k in object) { + if (hasOwn.call(object, k)) { + // set value to false instead of deleting it to avoid changing object structure + // https://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions + resultSet[k] = !!object[k]; } } + } - var SPACE = /\s+/; - function _parseString (resultSet, str) { - var array = str.split(SPACE); - var length = array.length; + const SPACE = /\s+/; + function _parseString (resultSet, str) { + const array = str.split(SPACE); + const length = array.length; - for (var i = 0; i < length; ++i) { - resultSet[array[i]] = true; - } + for (let i = 0; i < length; ++i) { + resultSet[array[i]] = true; } + } - function _parse (resultSet, arg) { - if (!arg) return; - var argType = typeof arg; + function _parse (resultSet, arg) { + if (!arg) return; + const argType = typeof arg; - // 'foo bar' - if (argType === 'string') { - _parseString(resultSet, arg); + // 'foo bar' + if (argType === 'string') { + _parseString(resultSet, arg); - // ['foo', 'bar', ...] - } else if (Array.isArray(arg)) { - _parseArray(resultSet, arg); + // ['foo', 'bar', ...] + } else if (Array.isArray(arg)) { + _parseArray(resultSet, arg); - // { 'foo': true, ... } - } else if (argType === 'object') { - _parseObject(resultSet, arg); + // { 'foo': true, ... } + } else if (argType === 'object') { + _parseObject(resultSet, arg); - // '130' - } else if (argType === 'number') { - _parseNumber(resultSet, arg); - } + // '130' + } else if (argType === 'number') { + _parseNumber(resultSet, arg); } + } - function _classNames () { - // don't leak arguments - // https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments - var len = arguments.length; - var args = Array(len); - for (var i = 0; i < len; i++) { - args[i] = arguments[i]; - } + function _classNames () { + // don't leak arguments + // https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments + const length = arguments.length; + const args = Array(length); + for (let i = 0; i < length; i++) { + args[i] = arguments[i]; + } - var classSet = new StorageObject(); - _parseArray(classSet, args); + const classSet = new StorageObject(); + _parseArray(classSet, args); - var list = []; + const list = []; - for (var k in classSet) { - if (classSet[k]) { - list.push(k) - } + for (const k in classSet) { + if (classSet[k]) { + list.push(k) } - - return list.join(' '); } - return _classNames; - })(); - - if (typeof module !== 'undefined' && module.exports) { - classNames.default = classNames; - module.exports = classNames; - } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) { - // register as 'classnames', consistent with npm package name - define('classnames', [], function () { - return classNames; - }); - } else { - window.classNames = classNames; + return list.join(' '); } -}()); + + return _classNames; +})(); + +export default classNames; diff --git a/index.d.ts b/index.d.ts index 65efa7d..97f52b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,26 +7,12 @@ // Sean Kelley // Michal Adamczyk // Marvin Hagemeister - -declare namespace classNames { - type Value = string | number | boolean | undefined | null; - type Mapping = Record; - interface ArgumentArray extends Array {} - interface ReadonlyArgumentArray extends ReadonlyArray {} - type Argument = Value | Mapping | ArgumentArray | ReadonlyArgumentArray; -} - -interface ClassNames { - (...args: classNames.ArgumentArray): string; - - default: ClassNames; -} - +export type Value = string | number | boolean | undefined | null; +export type Mapping = Record; +export interface ArgumentArray extends Array {} +export interface ReadonlyArgumentArray extends ReadonlyArray {} +export type Argument = Value | Mapping | ArgumentArray | ReadonlyArgumentArray; /** * A simple JavaScript utility for conditionally joining classNames together. */ -declare const classNames: ClassNames; - -export as namespace classNames; - -export = classNames; +export default function classNames(...args: ArgumentArray): string; diff --git a/index.js b/index.js index a42ea92..462992c 100644 --- a/index.js +++ b/index.js @@ -1,77 +1,54 @@ -/*! - Copyright (c) 2018 Jed Watson. - Licensed under the MIT License (MIT), see - http://jedwatson.github.io/classnames -*/ -/* global define */ +const hasOwn = {}.hasOwnProperty; -(function () { - 'use strict'; +export default function classNames () { + let classes = ''; - var hasOwn = {}.hasOwnProperty; + for (let i = 0; i < arguments.length; i++) { + const arg = arguments[i]; + if (arg) { + classes = appendClass(classes, parseValue(arg)); + } + } - function classNames () { - var classes = ''; + return classes; +} - for (var i = 0; i < arguments.length; i++) { - var arg = arguments[i]; - if (arg) { - classes = appendClass(classes, parseValue(arg)); - } - } +function parseValue (arg) { + if (typeof arg === 'string' || typeof arg === 'number') { + return arg; + } - return classes; + if (typeof arg !== 'object') { + return ''; } - function parseValue (arg) { - if (typeof arg === 'string' || typeof arg === 'number') { - return arg; - } + if (Array.isArray(arg)) { + return classNames.apply(null, arg); + } - if (typeof arg !== 'object') { - return ''; - } + if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) { + return arg.toString(); + } - if (Array.isArray(arg)) { - return classNames.apply(null, arg); - } + let classes = ''; - if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) { - return arg.toString(); + for (const key in arg) { + if (hasOwn.call(arg, key) && arg[key]) { + classes = appendClass(classes, key); } + } - var classes = ''; - - for (var key in arg) { - if (hasOwn.call(arg, key) && arg[key]) { - classes = appendClass(classes, key); - } - } + return classes; +} - return classes; +function appendClass (value, newClass) { + if (!newClass) { + return value; } - function appendClass (value, newClass) { - if (!newClass) { - return value; - } - - if (value) { - return value + ' ' + newClass; - } - - return value + newClass; + if (value) { + return value + ' ' + newClass; } - if (typeof module !== 'undefined' && module.exports) { - classNames.default = classNames; - module.exports = classNames; - } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) { - // register as 'classnames', consistent with npm package name - define('classnames', [], function () { - return classNames; - }); - } else { - window.classNames = classNames; - } -}()); + return value + newClass; +} diff --git a/package.json b/package.json index 3f63fc5..d913bb4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "type": "git", "url": "git+https://github.com/JedWatson/classnames.git" }, - "type": "commonjs", + "type": "module", "main": "./index.js", "types": "./index.d.ts", "exports": { @@ -39,7 +39,7 @@ } }, "scripts": { - "test": "node --test ./tests/*.mjs", + "test": "node --test ./tests/*.js", "bench": "node ./benchmarks/run.js", "bench-browser": "rollup --plugin commonjs,json,node-resolve ./benchmarks/runInBrowser.js --file ./benchmarks/runInBrowser.bundle.js && http-server -c-1 ./benchmarks", "check-types": "tsd" diff --git a/tests/bind.mjs b/tests/bind.js similarity index 100% rename from tests/bind.mjs rename to tests/bind.js diff --git a/tests/bind.test-d.ts b/tests/bind.test-d.ts index 7c0b07a..71878a4 100644 --- a/tests/bind.test-d.ts +++ b/tests/bind.test-d.ts @@ -14,7 +14,6 @@ interface IFoo { const ifoo: IFoo = { bar: true }; // bind -bind.default.bind({foo: 'bar'}); const bound = bind.bind({foo: 'bar'}); bind.bind(undefined); expectError(bind.bind(Symbol())); diff --git a/tests/dedupe.mjs b/tests/dedupe.js similarity index 100% rename from tests/dedupe.mjs rename to tests/dedupe.js diff --git a/tests/dedupe.test-d.ts b/tests/dedupe.test-d.ts index c2ef547..693562a 100644 --- a/tests/dedupe.test-d.ts +++ b/tests/dedupe.test-d.ts @@ -14,7 +14,6 @@ interface IFoo { const ifoo: IFoo = { bar: true }; // dedupe -dedupe.default('foo'); dedupe('foo'); dedupe(null); dedupe(undefined); diff --git a/tests/index.mjs b/tests/index.js similarity index 100% rename from tests/index.mjs rename to tests/index.js diff --git a/tests/index.test-d.ts b/tests/index.test-d.ts index c7d3138..90b092d 100644 --- a/tests/index.test-d.ts +++ b/tests/index.test-d.ts @@ -14,7 +14,6 @@ interface IFoo { const ifoo: IFoo = { bar: true }; // default -classNames.default('foo'); classNames('foo'); classNames(null); classNames(undefined);