xcn 0.0.0 → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,6 @@
1
+ Copyright (c) <2025> <earthnut.dev>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
6
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md CHANGED
@@ -1,13 +1,33 @@
1
- # earthnut-vue
1
+ # xcn
2
2
 
3
- ## description
3
+ [![version](<https://img.shields.io/npm/v/xcn.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/xcn) [![Coverage Status](<https://img.shields.io/coverallsCoverage/github/earthnutDev/xcn?logo=coveralls&label=coveralls&labelColor=rgb(12, 244, 39)&color=rgb(0,0,0)>)](https://coveralls.io/github/earthnutDev/xcn?branch=main) [![codecov](<https://img.shields.io/codecov/c/github/earthnutDev/xcn/main?logo=codecov&label=codecov&labelColor=rgb(7, 245, 245)&color=rgb(0,0,0)>)](https://codecov.io/gh/earthnutDev/xcn) [![issues 提交](<https://img.shields.io/badge/issues-提交-rgb(255,0,63)?logo=github>)](https://github.com/earthnutDev/xcn/issues)
4
4
 
5
- earthnut-vue is a vue ui library.
5
+ xcn = mix + class name
6
6
 
7
- aii
7
+ 组装 html 元素的 class 属性值.
8
8
 
9
- axg
9
+ ## 安装
10
10
 
11
- azg
11
+ ```sh
12
+ npm install --save xcn@latest
13
+ ```
12
14
 
13
- bfq
15
+ ## 使用
16
+
17
+ ```ts
18
+ import { xcn } from 'xcn';
19
+
20
+ > xcn('a' , 'b' ,'c');
21
+
22
+ 'a b c'
23
+
24
+ > xcn('a', { c: false }, true , false ,null , {d: true}, 'b');
25
+
26
+ 'a b d'
27
+
28
+
29
+ ```
30
+
31
+ ## 文档地址
32
+
33
+ 参看 [https://earthnut.dev/xcn/](https://earthnut.dev/xcn/)
package/cjs/index.cjs ADDED
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var index = require('./src/index.cjs');
6
+
7
+
8
+
9
+ exports.default = index.xcn;
10
+ exports.xcn = index.xcn;
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+
5
+ /**
6
+ *
7
+ * 合并 class
8
+ *
9
+ * merge class name
10
+ *
11
+ */
12
+ function xcn(...classNameList) {
13
+ const template = [];
14
+ /**
15
+ * 移除空白
16
+ */
17
+ const removeBlank = (str) => str
18
+ .trim()
19
+ .replace(/undefined/g, ' ')
20
+ .replace(/\s+/g, ' ')
21
+ .split(' ')
22
+ .sort()
23
+ .join(' ');
24
+ /**
25
+ * 混合值
26
+ */
27
+ const mergeNewValue = (newValue) => {
28
+ if (newValue === undefined || typeof newValue !== 'string')
29
+ return;
30
+ const newList = removeBlank(newValue).split(' ');
31
+ if (newList.length)
32
+ template.push(...newList);
33
+ };
34
+ classNameList.forEach(classNameItem => {
35
+ // 数据为 undefined 或 null 或 false 或 true 或 '' 或 [] 或 {}
36
+ if (!classNameItem || classNameItem === true) {
37
+ return;
38
+ }
39
+ // 数据为数组类型
40
+ if (Array.isArray(classNameItem) === true) {
41
+ classNameItem.forEach(childItem => mergeNewValue(xcn(childItem)));
42
+ }
43
+ // 数据为 string 类型
44
+ else if ('string' === typeof classNameItem ||
45
+ 'number' === typeof classNameItem) {
46
+ mergeNewValue(classNameItem.toString());
47
+ }
48
+ // 数据为函数类型
49
+ else if (aTypeOfJs.isFunction(classNameItem)) {
50
+ const result = classNameItem();
51
+ if (aTypeOfJs.isString(result)) {
52
+ mergeNewValue(result);
53
+ }
54
+ else {
55
+ result.forEach(item => mergeNewValue(xcn(item)));
56
+ }
57
+ }
58
+ else {
59
+ // 数据为 object 类型
60
+ for (const key in classNameItem) {
61
+ if (Object.prototype.hasOwnProperty.call(classNameItem, key)) {
62
+ const element = classNameItem[key];
63
+ if (true === element)
64
+ mergeNewValue(key);
65
+ }
66
+ }
67
+ }
68
+ });
69
+ const result = removeBlank(new Array(...new Set(template)).join(' '));
70
+ return (result || undefined);
71
+ }
72
+
73
+ exports.xcn = xcn;
package/mjs/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ import { xcn } from './src/index.mjs';
2
+
3
+
4
+
5
+ export { xcn as default, xcn };
@@ -0,0 +1,71 @@
1
+ import { isFunction, isString } from 'a-type-of-js';
2
+
3
+ /**
4
+ *
5
+ * 合并 class
6
+ *
7
+ * merge class name
8
+ *
9
+ */
10
+ function xcn(...classNameList) {
11
+ const template = [];
12
+ /**
13
+ * 移除空白
14
+ */
15
+ const removeBlank = (str) => str
16
+ .trim()
17
+ .replace(/undefined/g, ' ')
18
+ .replace(/\s+/g, ' ')
19
+ .split(' ')
20
+ .sort()
21
+ .join(' ');
22
+ /**
23
+ * 混合值
24
+ */
25
+ const mergeNewValue = (newValue) => {
26
+ if (newValue === undefined || typeof newValue !== 'string')
27
+ return;
28
+ const newList = removeBlank(newValue).split(' ');
29
+ if (newList.length)
30
+ template.push(...newList);
31
+ };
32
+ classNameList.forEach(classNameItem => {
33
+ // 数据为 undefined 或 null 或 false 或 true 或 '' 或 [] 或 {}
34
+ if (!classNameItem || classNameItem === true) {
35
+ return;
36
+ }
37
+ // 数据为数组类型
38
+ if (Array.isArray(classNameItem) === true) {
39
+ classNameItem.forEach(childItem => mergeNewValue(xcn(childItem)));
40
+ }
41
+ // 数据为 string 类型
42
+ else if ('string' === typeof classNameItem ||
43
+ 'number' === typeof classNameItem) {
44
+ mergeNewValue(classNameItem.toString());
45
+ }
46
+ // 数据为函数类型
47
+ else if (isFunction(classNameItem)) {
48
+ const result = classNameItem();
49
+ if (isString(result)) {
50
+ mergeNewValue(result);
51
+ }
52
+ else {
53
+ result.forEach(item => mergeNewValue(xcn(item)));
54
+ }
55
+ }
56
+ else {
57
+ // 数据为 object 类型
58
+ for (const key in classNameItem) {
59
+ if (Object.prototype.hasOwnProperty.call(classNameItem, key)) {
60
+ const element = classNameItem[key];
61
+ if (true === element)
62
+ mergeNewValue(key);
63
+ }
64
+ }
65
+ }
66
+ });
67
+ const result = removeBlank(new Array(...new Set(template)).join(' '));
68
+ return (result || undefined);
69
+ }
70
+
71
+ export { xcn };
package/package.json CHANGED
@@ -1,26 +1,58 @@
1
1
  {
2
+ "main": "cjs/index.cjs",
3
+ "module": "mjs/index.mjs",
4
+ "types": "types/index.d.ts",
2
5
  "name": "xcn",
3
- "version": "0.0.0",
4
- "main": "index.js",
5
- "author": "lmssee <lmssee@outlook.com> (https://lmssee.com)",
6
- "homepage": "https://earthnut.dev/vue",
6
+ "type": "module",
7
+ "version": "0.1.0",
8
+ "description": "一个用于字符串拼接的小工具,常用于 class name 的拼接",
7
9
  "license": "ISC",
8
- "description": "email to earthnut.dev@outlook.com",
10
+ "dependencies": {
11
+ "a-type-of-js": ">=0.2.0 <1.0.0"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
9
17
  "files": [
10
- "index.js"
18
+ "mjs/",
19
+ "cjs/",
20
+ "types/"
11
21
  ],
12
- "bugs": {
13
- "url": "https://github.com/lmssee/npm-earthnut-vue/issues"
22
+ "exports": {
23
+ ".": {
24
+ "import": {
25
+ "default": "./mjs/index.mjs",
26
+ "types": "./types/index.d.ts"
27
+ },
28
+ "require": {
29
+ "default": "./cjs/index.cjs",
30
+ "types": "./types/index.d.ts"
31
+ }
32
+ }
14
33
  },
15
- "keywords": [
16
- "earthnut"
17
- ],
18
34
  "repository": {
19
35
  "type": "git",
20
- "url": "git+https://github.com/lmssee/npm-earthnut-vue.git"
36
+ "url": "git+https://github.com/earthnutDev/xcn.git"
21
37
  },
22
- "publishConfig": {
23
- "access": "public",
24
- "registry": "https://registry.npmjs.org"
38
+ "author": {
39
+ "name": "earthnut",
40
+ "email": "earthnut.dev@outlook.com",
41
+ "url": "https://earthnut.dev"
42
+ },
43
+ "browserslist": [
44
+ "node>=18.0.0"
45
+ ],
46
+ "engines": {
47
+ "node": ">=18.0.0"
48
+ },
49
+ "keywords": [
50
+ "xcn",
51
+ "mix-cn"
52
+ ],
53
+ "homepage": "https://earthnut.dev/xcn",
54
+ "bugs": {
55
+ "url": "https://github.com/earthnutDev/xcn/issues",
56
+ "email": "earthnut.dev@outlook.com"
25
57
  }
26
- }
58
+ }
@@ -0,0 +1,3 @@
1
+ import { xcn } from './src/index';
2
+ export { xcn };
3
+ export default xcn;
File without changes
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 参数的数据类型
3
+ */
4
+ export type ClassNameItem = null | number | string | boolean | undefined | (() => string) | (() => ClassNameItem[]) | ClassNameItem[] | Record<string, boolean | undefined>;
5
+ /**
6
+ * 使用 infer 判断出当前的数据类型
7
+ *
8
+ * 字符串为具体的字符串值而非 string
9
+ */
10
+ type TypeofClassNameItem<T> = T extends null | boolean | undefined ? '' : T extends readonly [unknown, infer U] ? TypeofClassNameItem<U> | '' : T extends readonly [unknown, infer U, infer V] ? TypeofClassNameItem<U> | TypeofClassNameItem<V> : T extends Record<string, boolean | undefined> ? keyof T | '' : T extends () => string ? string : T;
11
+ /**
12
+ * 递归判断当前返回的数据类型
13
+ */
14
+ type XCN<T> = T extends [infer U, ...infer V] ? `${U & string} ${XCN<V>}` : '';
15
+ /**
16
+ *
17
+ * 合并 class
18
+ *
19
+ * merge class name
20
+ *
21
+ */
22
+ export declare function xcn<T extends ClassNameItem[]>(...classNameList: T): XCN<{
23
+ [K in keyof T]: TypeofClassNameItem<T[K]>;
24
+ }>;
25
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/index.js DELETED
@@ -1 +0,0 @@
1
- console.log("hello ,iggg!");