simple-merge-class-names 1.0.10 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -29
- package/mergeClassNames.js +10 -6
- package/package.json +38 -38
package/README.md
CHANGED
|
@@ -7,12 +7,16 @@ A straightforward utility for merging CSS class names in `React + Tailwind` and
|
|
|
7
7
|
- [Installation](#installation)
|
|
8
8
|
- [Install Prettier With VSCode (Most Recommended)](#install-prettier-with-vscode-most-recommended)
|
|
9
9
|
- [Usage](#usage)
|
|
10
|
+
|
|
10
11
|
- [Workflow To Minimize Typing Strain](#workflow-to-minimize-typing-strain)
|
|
12
|
+
|
|
13
|
+
- [Argument Handling](#argument-handling)
|
|
14
|
+
- [Breaking Changes From Version 1.X.X](#breaking-changes-from-version-1xx)
|
|
15
|
+
- [Testing](#testing)
|
|
16
|
+
- [Source Code](#source-code)
|
|
11
17
|
- [Why the Mismatch Between Exported Function and Package Name?](#why-the-mismatch-between-exported-function-and-package-name)
|
|
12
18
|
- [Where This Package Excels](#where-this-package-excels)
|
|
13
|
-
|
|
14
|
-
- [Argument Handling](#argument-handling)
|
|
15
|
-
- [Testing](#testing)
|
|
19
|
+
|
|
16
20
|
- [Production Considerations](#production-considerations)
|
|
17
21
|
- [License](#license)
|
|
18
22
|
|
|
@@ -89,15 +93,29 @@ function MyComponent() {
|
|
|
89
93
|
- Replaces single quotes with double quotes.
|
|
90
94
|
- Neatly arranges each class name on a new line.
|
|
91
95
|
|
|
92
|
-
##
|
|
96
|
+
## Argument Handling
|
|
93
97
|
|
|
94
|
-
|
|
98
|
+
`mergeClassNames` only accepts **_non-empty string values_**, everything else like empty strings (`""`), `null`, `undefined`, numbers, objects and arrays is _ignored_. This ensures stricter and predictable output.
|
|
95
99
|
|
|
96
|
-
|
|
100
|
+
### Breaking Changes From Version 1.X.X
|
|
97
101
|
|
|
98
|
-
|
|
102
|
+
In pervious versions, arguments that were not strings were implicitly converted to strings by the JavaScript engine.
|
|
99
103
|
|
|
100
|
-
|
|
104
|
+
## Testing
|
|
105
|
+
|
|
106
|
+
This project uses `Vitest` as the test runner for fast, modern testing.
|
|
107
|
+
|
|
108
|
+
#### Run All Testing Once
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
pnpm test
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Run Tests In Watch Mode
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
pnpm test:watch
|
|
118
|
+
```
|
|
101
119
|
|
|
102
120
|
## Source Code
|
|
103
121
|
|
|
@@ -106,39 +124,34 @@ While similar packages exist (`clsx`) with better features and potentially impro
|
|
|
106
124
|
* mergeClassNames - A straightforward utility for merging CSS class names in React + Tailwind, and other JavaScript projects.
|
|
107
125
|
...
|
|
108
126
|
*/
|
|
109
|
-
|
|
127
|
+
|
|
128
|
+
const isTypeString = (val) => typeof val === "string";
|
|
110
129
|
|
|
111
130
|
const isNotEmptyString = (val) => val !== "";
|
|
112
131
|
|
|
113
132
|
export const mergeClassNames = (...args) => {
|
|
114
|
-
const space = " ";
|
|
115
|
-
const values = args.filter(
|
|
116
|
-
(val) => isDefined(val) && isNotEmptyString(val)
|
|
117
|
-
);
|
|
118
|
-
const className = values.join(space);
|
|
119
|
-
return className;
|
|
120
|
-
};
|
|
121
|
-
```
|
|
133
|
+
const space = "\x20"; // " "; ASCII code for single space character;
|
|
122
134
|
|
|
123
|
-
|
|
135
|
+
const stringsOnly = args.filter((val) => isTypeString(val));
|
|
124
136
|
|
|
125
|
-
|
|
137
|
+
const trimmed = stringsOnly.map((val) => val.trim());
|
|
126
138
|
|
|
127
|
-
|
|
139
|
+
const nonEmpty = trimmed.filter((val) => isNotEmptyString(val));
|
|
128
140
|
|
|
129
|
-
|
|
141
|
+
const className = nonEmpty.join(space);
|
|
142
|
+
return className;
|
|
143
|
+
};
|
|
144
|
+
```
|
|
130
145
|
|
|
131
|
-
|
|
146
|
+
## Why the Mismatch Between Exported Function and Package Name?
|
|
132
147
|
|
|
133
|
-
|
|
134
|
-
pnpm test
|
|
135
|
-
```
|
|
148
|
+
I wanted to name the package as `mergeClassNames` to reflect the single exported function, but the NPM Package Registry does not allow capital letters, only lower case and dash characters.
|
|
136
149
|
|
|
137
|
-
|
|
150
|
+
In addition there was already a package named `merge-class-names` but it is no longer maintained (and the developer recommends `clsx` instead).
|
|
138
151
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
152
|
+
## Where This Package Excels
|
|
153
|
+
|
|
154
|
+
While similar packages exist (`clsx`) with better features and potentially improved performance, `simple-merge-class-names` focuses on being very straightforward and easy to reason about, as defined in its source code.
|
|
142
155
|
|
|
143
156
|
## Production Considerations
|
|
144
157
|
|
|
@@ -148,4 +161,6 @@ If you are considering this package for production, you might also want to look
|
|
|
148
161
|
|
|
149
162
|
This project is licensed under the AGPL-3.0 License. See `LICENSE.txt` for full details.
|
|
150
163
|
|
|
164
|
+
---
|
|
165
|
+
|
|
151
166
|
Enjoy 😉
|
package/mergeClassNames.js
CHANGED
|
@@ -37,15 +37,19 @@
|
|
|
37
37
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
38
38
|
*/
|
|
39
39
|
|
|
40
|
-
const
|
|
40
|
+
const isTypeString = (val) => typeof val === "string";
|
|
41
41
|
|
|
42
42
|
const isNotEmptyString = (val) => val !== "";
|
|
43
43
|
|
|
44
44
|
export const mergeClassNames = (...args) => {
|
|
45
|
-
const space = " ";
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
45
|
+
const space = "\x20"; // " "; ASCII code for single space character;
|
|
46
|
+
|
|
47
|
+
const stringsOnly = args.filter((val) => isTypeString(val));
|
|
48
|
+
|
|
49
|
+
const trimmed = stringsOnly.map((val) => val.trim());
|
|
50
|
+
|
|
51
|
+
const nonEmpty = trimmed.filter((val) => isNotEmptyString(val));
|
|
52
|
+
|
|
53
|
+
const className = nonEmpty.join(space);
|
|
50
54
|
return className;
|
|
51
55
|
};
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "simple-merge-class-names",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Example usage: <div className = {mergeClassNames('flex', 'flex-col')}/>",
|
|
5
|
-
"main": "mergeClassNames.js",
|
|
6
|
-
"exports": "./mergeClassNames.js",
|
|
7
|
-
"files": [
|
|
8
|
-
"package.json",
|
|
9
|
-
"mergeClassNames.js",
|
|
10
|
-
"LICENSE.txt",
|
|
11
|
-
"README.md"
|
|
12
|
-
],
|
|
13
|
-
"scripts": {
|
|
14
|
-
"test": "vitest",
|
|
15
|
-
"test:watch": "vitest --watch",
|
|
16
|
-
"test:ui": "vitest --ui"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"merge",
|
|
20
|
-
"class-names-merger",
|
|
21
|
-
"utility",
|
|
22
|
-
"react",
|
|
23
|
-
"tailwindcss"
|
|
24
|
-
],
|
|
25
|
-
"author": "Abdullah Fatota",
|
|
26
|
-
"license": "AGPL-3.0",
|
|
27
|
-
"repository": {
|
|
28
|
-
"type": "git",
|
|
29
|
-
"url": "git+https://github.com/new-AF/simple-merge-class-names.git"
|
|
30
|
-
},
|
|
31
|
-
"bugs": {
|
|
32
|
-
"url": "https://github.com/new-AF/simple-merge-class-names/issues"
|
|
33
|
-
},
|
|
34
|
-
"homepage": "https://github.com/new-AF/simple-merge-class-names",
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"vitest": "^3.2.3"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "simple-merge-class-names",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Example usage: <div className = {mergeClassNames('flex', 'flex-col')}/>",
|
|
5
|
+
"main": "mergeClassNames.js",
|
|
6
|
+
"exports": "./mergeClassNames.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"package.json",
|
|
9
|
+
"mergeClassNames.js",
|
|
10
|
+
"LICENSE.txt",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "vitest",
|
|
15
|
+
"test:watch": "vitest --watch",
|
|
16
|
+
"test:ui": "vitest --ui"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"merge",
|
|
20
|
+
"class-names-merger",
|
|
21
|
+
"utility",
|
|
22
|
+
"react",
|
|
23
|
+
"tailwindcss"
|
|
24
|
+
],
|
|
25
|
+
"author": "Abdullah Fatota",
|
|
26
|
+
"license": "AGPL-3.0",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/new-AF/simple-merge-class-names.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/new-AF/simple-merge-class-names/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/new-AF/simple-merge-class-names",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"vitest": "^3.2.3"
|
|
37
|
+
}
|
|
38
|
+
}
|