Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ESM #348

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion benchmarks/package.json

This file was deleted.

18 changes: 2 additions & 16 deletions bind.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import { ArgumentArray } from './index.js';

declare namespace classNames {
type Binding = Record<string, string>;
}

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<string, string>;
export default function classNames(this: Binding | undefined, ...args: ArgumentArray): string;
97 changes: 37 additions & 60 deletions bind.js
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 2 additions & 5 deletions dedupe.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import classNames = require('./index.js');

export as namespace classNames;

export = classNames;
import classNames from './index.js';
export default classNames;
161 changes: 70 additions & 91 deletions dedupe.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 6 additions & 20 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,12 @@
// Sean Kelley <https://github.com/seansfkelley>
// Michal Adamczyk <https://github.com/mradamczyk>
// Marvin Hagemeister <https://github.com/marvinhagemeister>

declare namespace classNames {
type Value = string | number | boolean | undefined | null;
type Mapping = Record<string, any>;
interface ArgumentArray extends Array<Argument> {}
interface ReadonlyArgumentArray extends ReadonlyArray<Argument> {}
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<string, any>;
export interface ArgumentArray extends Array<Argument> {}
export interface ReadonlyArgumentArray extends ReadonlyArray<Argument> {}
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;
Loading