Skip to content

Commit

Permalink
rename to classcat, meow.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed Oct 26, 2017
1 parent b875435 commit de0c7fe
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 102 deletions.
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Classwrap
[![Travis CI](https://img.shields.io/travis/JorgeBucaran/classwrap/master.svg)](https://travis-ci.org/JorgeBucaran/classwrap)
[![Codecov](https://img.shields.io/codecov/c/github/JorgeBucaran/classwrap/master.svg)](https://codecov.io/gh/JorgeBucaran/classwrap)
[![npm](https://img.shields.io/npm/v/classwrap.svg)](https://www.npmjs.org/package/classwrap)
# Catclass
[![Travis CI](https://img.shields.io/travis/JorgeBucaran/classcat/master.svg)](https://travis-ci.org/JorgeBucaran/classcat)
[![Codecov](https://img.shields.io/codecov/c/github/JorgeBucaran/classcat/master.svg)](https://codecov.io/gh/JorgeBucaran/classcat)
[![npm](https://img.shields.io/npm/v/classcat.svg)](https://www.npmjs.org/package/classcat)

Classwrap is a 0.3 KB JavaScript utility for conditionally concatenating [class names](https://developer.mozilla.org/en-US/docs/Web/API/Element/className).
Classcat is a 0.3 KB JavaScript utility for conditionally concatenating [class names](https://developer.mozilla.org/en-US/docs/Web/API/Element/className).

[Try it Online](https://codepen.io/JorgeBucaran/pen/GMRjRB)

```js
import cw from "classwrap"
import cc from "classcat"

export function ToggleButton({ toggle, isOn }) {
return (
<div class="btn" onclick={toggle}>
<div
class={cw({
class={cc({
circle: true,
off: !isOn,
on: isOn
})}
/>
<span
class={cw({
class={cc({
textOff: !isOn
})}
>
Expand All @@ -32,38 +32,38 @@ export function ToggleButton({ toggle, isOn }) {
}
```

Classwrap works in all browsers >=IE9. Use it with your favorite JavaScript view library.
classcat works in all browsers >=IE9. Use it with your favorite JavaScript view library.

[![Classwrap](https://user-images.githubusercontent.com/56996/30416101-cda83bd4-9965-11e7-9db5-230ba3fc83fd.gif)](https://codepen.io/JorgeBucaran/full/GMRjRB/)
[![classcat](https://user-images.githubusercontent.com/56996/30416101-cda83bd4-9965-11e7-9db5-230ba3fc83fd.gif)](https://codepen.io/JorgeBucaran/full/GMRjRB/)

## Installation

Install with npm / Yarn.

<pre>
npm i <a href="https://www.npmjs.com/package/classwrap">classwrap</a>
npm i <a href="https://www.npmjs.com/package/classcat">classcat</a>
</pre>

Then with a module bundler like [Rollup](https://github.com/rollup/rollup) or [Webpack](https://github.com/webpack/webpack), use as you would anything else.

```js
import cw from "classwrap"
import cc from "classcat"
```

Or download the minified library from a [unpkg](https://unpkg.com/classwrap@latest/dist/classwrap.js) or [jsDelivr](https://cdn.jsdelivr.net/npm/classwrap@latest/dist/classwrap.js).
Or download the minified library from a [unpkg](https://unpkg.com/classcat@latest/dist/classcat.js) or [jsDelivr](https://cdn.jsdelivr.net/npm/classcat@latest/dist/classcat.js).

```html
<script src="https://unpkg.com/classwrap"></script>
<script src="https://unpkg.com/classcat"></script>
```

Then find it on `window.classwrap`.
Then find it on `window.classcat`.

## Usage

Classwrap joins all elements of an array or keys of an object into a string. If the value associated with a given key is falsy, the key will be ignored.
classcat joins all elements of an array or keys of an object into a string. If the value associated with a given key is falsy, the key will be ignored.

```js
cw([
cc([
"btn",
{
"btn-active": true,
Expand All @@ -75,7 +75,7 @@ cw([
Nested arrays or objects are supported too. Use this feature to assemble classes with a common prefix.

```js
cw([
cc([
"tab",
{
tab: {
Expand All @@ -89,18 +89,18 @@ cw([

## Credits

Classwrap was inspired by [JedWatson/classnames](https://github.com/JedWatson/classnames) with support for nested objects and [improved](/bench/README.md) performance. It differs from classnames in that it does not accept [variable arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments).
classcat was inspired by [JedWatson/classnames](https://github.com/JedWatson/classnames) with support for nested objects and [improved](/bench/README.md) performance. It differs from classnames in that it does not accept [variable arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments).

```js
cw("foo", "bar", "baz") // => foo
cc("foo", "bar", "baz") // => foo
```

To solve this, wrap the arguments inside an array.

```js
cw(["foo", "bar", "baz"]) // => foo bar baz
cc(["foo", "bar", "baz"]) // => foo bar baz
```

## License

Classwrap is MIT licensed. See [LICENSE](LICENSE.md).
classcat is MIT licensed. See [LICENSE](LICENSE.md).
39 changes: 17 additions & 22 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Benchmarks

[Classwrap](../README.md) vs. [JedWatson/classnames](https://github.com/JedWatson/classnames) benchmarks.
[Classcat](../README.md) vs. [classNames](https://github.com/JedWatson/classnames) benchmarks.

## Run

Expand All @@ -10,31 +10,26 @@ npm i && node .

## Results

Please be aware that results may vary across browsers and Node.js runtimes. All results run on a 2.4GHz Intel Core i7 CPU with 16 GB memory.
Please be aware that results may vary across browsers and Node.js runtimes. All tests run on a 2.4GHz Intel Core i7 CPU with 16 GB memory.

<pre>
classwrap – Strings x <b>31,989,110</b> ops/sec ±0.88% (95 runs sampled)
classnames – Strings x 4,023,323 ops/sec ±0.84% (93 runs sampled)
<b>Classcat – Strings × 31,836,467 ops/sec</b>
classNames – Strings × 3,751,299 ops/sec
Fastest is Classcat – Strings

<b>Fastest is classwrap – Strings</b>
<b>Classcat – Objects × 6,385,707 ops/sec</b>
classNames – Objects × 3,260,164 ops/sec
Fastest is Classcat – Objects

classwrap – Objects x <b>5,153,511</b> ops/sec ±0.85% (98 runs sampled)
classnames – Objects x 3,840,956 ops/sec ±0.86% (97 runs sampled)
<b>Classcat – Strings & Objects × 31,213,754 ops/sec</b>
classNames – Strings & Objects × 3,000,682 ops/sec
Fastest is Classcat – Strings & Objects

<b>Fastest is classwrap – Objects</b>
<b>Classcat – Mixed × 29,953,843 ops/sec</b>
classNames – Mixed × 2,126,420 ops/sec
Fastest is Classcat – Mixed

classwrap – Strings & Objects x <b>30,230,884</b> ops/sec ±0.89% (94 runs sampled)
classnames – Strings & Objects x 2,697,613 ops/sec ±0.99% (97 runs sampled)

<b>Fastest is classwrap – Strings & Objects</b>

classwrap – Mixed x <b>29,473,452</b> ops/sec ±0.96% (91 runs sampled)</b>
classnames – Mixed x 1,577,708 ops/sec ±0.93% (94 runs sampled)

<b>Fastest is classwrap – Mixed</b>

classwrap – Arrays x <b>2,488,897</b> ops/sec ±0.83% (95 runs sampled)
classnames – Arrays x 754,558 ops/sec ±0.82% (95 runs sampled)

<b>Fastest is classwrap – Arrays</b>
<b>Classcat – Arrays × 3,628,801 ops/sec</b>
classNames – Arrays × 915,020 ops/sec
Fastest is Classcat – Arrays
</pre>
45 changes: 19 additions & 26 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
const benchmark = require("benchmark")
const classnames = require("classnames")
const classwrap = require("../")
const { Suite } = require("benchmark")
const cx = require("classnames")
const cc = require("../")

require("./fixtures").map(fixed => {
const suite = new benchmark.Suite()

suite.add(`classwrap – ${fixed.description}`, () => {
classwrap.apply({}, fixed.args)
})

suite.add(`classnames – ${fixed.description}`, () => {
classnames.apply({}, fixed.args)
})

suite.on("cycle", event => console.log(event.target + ""))

suite.on("complete", function() {
console.log(`\nFastest is ${this.filter("fastest").pluck("name")} \n`)
})

suite.on("error", ({ target }) => {
console.log(target.error.message)
throw target.error
})

suite.run()
require("./fixtures").map((fixed, index, { length }) => {
const suite = new Suite()
suite
.add(`Classcat – ${fixed.description}`, () => cc.apply({}, fixed.args))
.add(`classNames – ${fixed.description}`, () => cx.apply({}, fixed.args))
.on("cycle", ({ target: { name, hz, stats } }) =>
console.log(`${name} × ${Math.floor(hz).toLocaleString()} ops/sec`)
)
.on("complete", function() {
console.log(
`Fastest is ${this.filter("fastest").map("name")}${index + 1 < length
? "\n"
: ""}`
)
})
.run()
})
6 changes: 3 additions & 3 deletions bench/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "classwrap-benchmarks",
"name": "classcat-benchmarks",
"version": "1.0.0",
"private": true,
"description": "Classwrap vs JedWatson/classnames benchmarks.",
"description": "Classcat vs JedWatson/classnames benchmarks.",
"main": "index.js",
"scripts": {},
"author": "Jorge Bucaran",
"license": "MIT",
"devDependencies": {
"benchmark": "^2.1.4",
"classnames": "2.2.5"
"classnames": "*"
}
}
2 changes: 1 addition & 1 deletion classwrap.d.ts → classcat.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export as namespace classwrap
export as namespace classcat

export interface ClassSet {
[key: string]: boolean | any | ClassSet
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "classwrap",
"name": "classcat",
"description": "0.3 KB JavaScript utility for conditionally concatenating class names.",
"version": "1.2.3",
"main": "dist/classwrap.js",
"version": "1.0.0",
"main": "dist/classcat.js",
"jsnext:main": "src/index.js",
"module": "src/index.js",
"typings": "classwrap.d.ts",
"typings": "classcat.d.ts",
"license": "MIT",
"repository": "JorgeBucaran/classwrap",
"repository": "JorgeBucaran/classcat",
"files": [
"src",
"dist",
"classwrap.d.ts"
"classcat.d.ts"
],
"author": "Jorge Bucaran",
"keywords": [
Expand All @@ -23,8 +23,8 @@
"scripts": {
"test": "jest --coverage --no-cache && tsc -p tests/ts",
"build": "npm run bundle && npm run minify",
"bundle": "rollup -i src/index.js -o dist/classwrap.js -m -f umd -n classwrap",
"minify": "uglifyjs dist/classwrap.js -o dist/classwrap.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --source-map dist/classwrap.js.map",
"bundle": "rollup -i src/index.js -o dist/classcat.js -m -f umd -n classcat",
"minify": "uglifyjs dist/classcat.js -o dist/classcat.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --source-map dist/classcat.js.map",
"bench": "node bench",
"prepare": "npm run build",
"format": "prettier --semi false --write 'src/**/*.js' '{,tests/ts/}*.ts'",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function wrap(classes, prefix) {
export default function cat(classes, prefix) {
var value
var className = ""
var type = typeof classes
Expand All @@ -11,7 +11,7 @@ export default function wrap(classes, prefix) {

if (Array.isArray(classes) && classes.length) {
for (var i = 0, len = classes.length; i < len; i++) {
if ((value = wrap(classes[i], prefix))) {
if ((value = cat(classes[i], prefix))) {
className += (className && prefix) + value
}
}
Expand All @@ -21,7 +21,7 @@ export default function wrap(classes, prefix) {
className +=
(className && prefix) +
i +
(typeof value === "object" ? wrap(value, prefix + i) : "")
(typeof value === "object" ? cat(value, prefix + i) : "")
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import cw from "../src"
import cc from "../src"

test("falsy", () => {
expect(cw({})).toBe("")
expect(cw([])).toBe("")
expect(cw(["", null, false, undefined, 0, NaN])).toBe("")
expect(cc({})).toBe("")
expect(cc([])).toBe("")
expect(cc(["", null, false, undefined, 0, NaN])).toBe("")
})

test("arrays", () => {
expect(cw(["foo", "bar", false, "baz"])).toBe("foo bar baz")
expect(cc(["foo", "bar", false, "baz"])).toBe("foo bar baz")
})

test("objects", () => {
expect(
cw({
cc({
foo: true,
bar: true,
quux: false,
Expand All @@ -24,7 +24,7 @@ test("objects", () => {
test("mix", () => {
const baz = "baz"
expect(
cw([
cc([
"foo",
"foo-bar",
{
Expand All @@ -36,7 +36,7 @@ test("mix", () => {

test("prefix", () => {
expect(
cw(
cc(
{
foo: true,
bar: true,
Expand All @@ -50,7 +50,7 @@ test("prefix", () => {

test("deep", () => {
expect(
cw([
cc([
"foo",
{
foo: {
Expand All @@ -68,7 +68,7 @@ test("deep", () => {
test("not owned props", () => {
Object.prototype.myFunction = () => {}

expect(cw({})).toBe("")
expect(cc({})).toBe("")

delete Object.prototype.myFunction
})
12 changes: 6 additions & 6 deletions tests/ts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import classwrap from "classwrap"
import cc from "classcat"

let className: string

className = classwrap("btn")
className = cc("btn")

className = classwrap(12)
className = cc(12)

className = classwrap({
className = cc({
btn: true
})

className = classwrap(["btn", "btn-active"])
className = cc(["btn", "btn-active"])

className = classwrap([
className = cc([
"btn",
{
btn: {
Expand Down
2 changes: 1 addition & 1 deletion tests/ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"noEmit": true,
"baseUrl": "../..",
"paths": {
"classwrap": ["src", "classwrap.d.ts"]
"classcat": ["src", "classcat.d.ts"]
}
},
"include": ["*.ts", "**/*.ts"]
Expand Down

0 comments on commit de0c7fe

Please sign in to comment.