@hzab/utils 1.1.1 → 1.1.3

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/src/file/file.ts CHANGED
@@ -1,37 +1,37 @@
1
- export interface IFile extends File {
2
- file?: IFile;
3
- url?: string;
4
- }
5
-
6
- /**
7
- * 建立一个可以存取该 file 的 url
8
- * @param {Object} file 文件
9
- * @returns {string} url
10
- * blob:http://localhost:8000/c9950644-5118-4231-9be7-8183bde1fdc7
11
- */
12
- export function getFileURL(file: IFile) {
13
- const _file: IFile = file?.file || file;
14
- if (!(_file instanceof File)) {
15
- return;
16
- }
17
-
18
- let url = _file.url || null;
19
-
20
- try {
21
- // 下面函数执行的效果是一样的,只是需要针对不同的浏览器执行不同的 js 函数而已
22
- if (window.createObjectURL != undefined) {
23
- // basic
24
- return window.createObjectURL(_file);
25
- } else if (window.URL != undefined) {
26
- // mozilla(firefox)
27
- return window.URL.createObjectURL(_file);
28
- } else if (window.webkitURL != undefined) {
29
- // webkit or chrome
30
- return window.webkitURL.createObjectURL(_file);
31
- }
32
- } catch (error) {
33
- console.warn("getFileURL Error: ", error);
34
- }
35
-
36
- return url;
37
- }
1
+ export interface IFile extends File {
2
+ file?: IFile;
3
+ url?: string;
4
+ }
5
+
6
+ /**
7
+ * 建立一个可以存取该 file 的 url
8
+ * @param {Object} file 文件
9
+ * @returns {string} url
10
+ * blob:http://localhost:8000/c9950644-5118-4231-9be7-8183bde1fdc7
11
+ */
12
+ export function getFileURL(file: IFile) {
13
+ const _file: IFile = file?.file || file;
14
+ if (!(_file instanceof File)) {
15
+ return;
16
+ }
17
+
18
+ let url = _file.url || null;
19
+
20
+ try {
21
+ // 下面函数执行的效果是一样的,只是需要针对不同的浏览器执行不同的 js 函数而已
22
+ if (window.createObjectURL != undefined) {
23
+ // basic
24
+ return window.createObjectURL(_file);
25
+ } else if (window.URL != undefined) {
26
+ // mozilla(firefox)
27
+ return window.URL.createObjectURL(_file);
28
+ } else if (window.webkitURL != undefined) {
29
+ // webkit or chrome
30
+ return window.webkitURL.createObjectURL(_file);
31
+ }
32
+ } catch (error) {
33
+ console.warn("getFileURL Error: ", error);
34
+ }
35
+
36
+ return url;
37
+ }
@@ -1,98 +1,98 @@
1
- import dayjs from "dayjs";
2
- import { nanoidNumALetters } from "../nanoid";
3
-
4
- /**
5
- * 获取文件后缀
6
- * @param fileUrl
7
- * @returns
8
- */
9
- export function getFileExt(fileUrl) {
10
- if (typeof fileUrl !== "string") {
11
- return;
12
- }
13
- const res = fileUrl?.match(/([^\/]+?)\.([^\/\.]+)$/);
14
- if (res && res.length > 2) {
15
- return res[2];
16
- }
17
- return fileUrl;
18
- }
19
-
20
- /**
21
- * 从 url 获取文件名称,不包含后缀
22
- * @param {string} fileName
23
- * @param {string} str
24
- * @returns
25
- */
26
- export function getFileName(fileUrl) {
27
- if (typeof fileUrl !== "string") {
28
- return;
29
- }
30
- const res = fileUrl?.match(/([^\/]+?)\.[^\/\.]+$/);
31
- if (res && res.length > 0) {
32
- return res?.[1] || res?.[0];
33
- }
34
- return fileUrl;
35
- }
36
-
37
- /**
38
- * 从 url 获取文件名称,包含后缀
39
- * @param {string} fileName
40
- * @param {string} str
41
- * @returns
42
- */
43
- export function getFullFileName(fileUrl) {
44
- if (typeof fileUrl !== "string") {
45
- return;
46
- }
47
- const res = fileUrl?.match(/[^\/]+?\.[^\/\.]+$/);
48
- if (res && res.length > 0) {
49
- return res[0];
50
- }
51
- return fileUrl;
52
- }
53
-
54
- /**
55
- * 合并文件名称,自动处理后缀
56
- * @param {string} fileName
57
- * @param {string} str
58
- * @returns
59
- */
60
- export const mergeFileName = function (fileName, str = "") {
61
- if (typeof fileName !== "string") {
62
- return "";
63
- }
64
- const name = fileName?.match(/([^\/]+?)\.[^\/]+$/)?.[1] || "";
65
- const suffix = fileName?.match(/[^\/]+?\.([^\/]+)$/)?.[1] || "";
66
-
67
- return `${name}${str ?? ""}.${suffix}`;
68
- };
69
-
70
- /**
71
- * 获取唯一的文件名
72
- * @param {string} fileName
73
- * @returns
74
- */
75
- export const getUFileName = function (fileName) {
76
- return mergeFileName(getFileName(fileName) || "up", "-" + `~kid-${nanoidNumALetters()}~`);
77
- };
78
-
79
- /**
80
- * 获取文件名的后缀
81
- * @param fileName
82
- * @returns
83
- */
84
- export const getFileNameExt = function (fileName) {
85
- const regex = /^(.*)\.([^.]+)$/;
86
- const result = fileName.match(regex);
87
- if (result) {
88
- const [, mainPart, ext] = result;
89
- return {
90
- mainPart,
91
- ext,
92
- };
93
- }
94
- return {
95
- mainPart: fileName,
96
- ext: "",
97
- };
98
- };
1
+ import dayjs from "dayjs";
2
+ import { nanoidNumALetters } from "../nanoid";
3
+
4
+ /**
5
+ * 获取文件后缀
6
+ * @param fileUrl
7
+ * @returns
8
+ */
9
+ export function getFileExt(fileUrl) {
10
+ if (typeof fileUrl !== "string") {
11
+ return;
12
+ }
13
+ const res = fileUrl?.match(/([^\/]+?)\.([^\/\.]+)$/);
14
+ if (res && res.length > 2) {
15
+ return res[2];
16
+ }
17
+ return fileUrl;
18
+ }
19
+
20
+ /**
21
+ * 从 url 获取文件名称,不包含后缀
22
+ * @param {string} fileName
23
+ * @param {string} str
24
+ * @returns
25
+ */
26
+ export function getFileName(fileUrl) {
27
+ if (typeof fileUrl !== "string") {
28
+ return;
29
+ }
30
+ const res = fileUrl?.match(/([^\/]+?)\.[^\/\.]+$/);
31
+ if (res && res.length > 0) {
32
+ return res?.[1] || res?.[0];
33
+ }
34
+ return fileUrl;
35
+ }
36
+
37
+ /**
38
+ * 从 url 获取文件名称,包含后缀
39
+ * @param {string} fileName
40
+ * @param {string} str
41
+ * @returns
42
+ */
43
+ export function getFullFileName(fileUrl) {
44
+ if (typeof fileUrl !== "string") {
45
+ return;
46
+ }
47
+ const res = fileUrl?.match(/[^\/]+?\.[^\/\.]+$/);
48
+ if (res && res.length > 0) {
49
+ return res[0];
50
+ }
51
+ return fileUrl;
52
+ }
53
+
54
+ /**
55
+ * 合并文件名称,自动处理后缀
56
+ * @param {string} fileName
57
+ * @param {string} str
58
+ * @returns
59
+ */
60
+ export const mergeFileName = function (fileName, str = "") {
61
+ if (typeof fileName !== "string") {
62
+ return "";
63
+ }
64
+ const name = fileName?.match(/([^\/]+?)\.[^\/]+$/)?.[1] || "";
65
+ const suffix = fileName?.match(/[^\/]+?\.([^\/]+)$/)?.[1] || "";
66
+
67
+ return `${name}${str ?? ""}.${suffix}`;
68
+ };
69
+
70
+ /**
71
+ * 获取唯一的文件名
72
+ * @param {string} fileName
73
+ * @returns
74
+ */
75
+ export const getUFileName = function (fileName) {
76
+ return mergeFileName(getFileName(fileName) || "up", "-" + `~kid-${nanoidNumALetters()}~`);
77
+ };
78
+
79
+ /**
80
+ * 获取文件名的后缀
81
+ * @param fileName
82
+ * @returns
83
+ */
84
+ export const getFileNameExt = function (fileName) {
85
+ const regex = /^(.*)\.([^.]+)$/;
86
+ const result = fileName.match(regex);
87
+ if (result) {
88
+ const [, mainPart, ext] = result;
89
+ return {
90
+ mainPart,
91
+ ext,
92
+ };
93
+ }
94
+ return {
95
+ mainPart: fileName,
96
+ ext: "",
97
+ };
98
+ };
@@ -1,63 +1,63 @@
1
- import React from "react";
2
-
3
- /**
4
- * 检测是否为原生 HTML DOM 节点
5
- * @param {any} node 待检测对象
6
- * @returns {boolean} 是否为原生 DOM 节点
7
- */
8
- function isHtmlDomNode(node) {
9
- // 先判断浏览器环境,避免 SSR 报错;兼容 Node/HTMLElement 类型
10
- return !!(typeof window !== "undefined" && node && (node instanceof HTMLElement || node instanceof Node));
11
- }
12
-
13
- /**
14
- * 增强版 Formily Schema 克隆函数
15
- * 解决 lodash cloneDeep 丢失 React DOM/原生 DOM 的问题,兼容多种特殊类型
16
- * @param {object} schema 待克隆的 Formily Schema
17
- * @returns {object} 克隆后的 Schema
18
- */
19
- export function cloneSchema(schema) {
20
- // 1. 基础类型(null/undefined/字符串/数字/布尔)直接返回
21
- if (schema === null || typeof schema !== "object") {
22
- return schema;
23
- }
24
-
25
- // 2. React 虚拟DOM:直接保留原引用,不拷贝
26
- if (React.isValidElement(schema)) {
27
- return schema;
28
- }
29
-
30
- // 3. 原生 HTML DOM 节点:直接保留原引用
31
- if (isHtmlDomNode(schema)) {
32
- return schema;
33
- }
34
-
35
- // 4. 函数:Formily Schema 中的校验/格式化函数等,保留原引用
36
- if (typeof schema === "function") {
37
- return schema;
38
- }
39
-
40
- // 5. 日期对象:创建新的 Date 实例,保证值相同但引用不同
41
- if (schema instanceof Date) {
42
- return new Date(schema.getTime());
43
- }
44
-
45
- // 6. 正则对象:创建新的 RegExp 实例,保留源和标志
46
- if (schema instanceof RegExp) {
47
- return new RegExp(schema.source, schema.flags);
48
- }
49
-
50
- // 7. 数组:递归克隆每一项
51
- if (Array.isArray(schema)) {
52
- return schema.map((item) => cloneSchema(item));
53
- }
54
-
55
- // 8. 普通对象:递归克隆自有属性
56
- const clonedObj = {};
57
- for (const key in schema) {
58
- if (Object.prototype.hasOwnProperty.call(schema, key)) {
59
- clonedObj[key] = cloneSchema(schema[key]);
60
- }
61
- }
62
- return clonedObj;
63
- }
1
+ import React from "react";
2
+
3
+ /**
4
+ * 检测是否为原生 HTML DOM 节点
5
+ * @param {any} node 待检测对象
6
+ * @returns {boolean} 是否为原生 DOM 节点
7
+ */
8
+ function isHtmlDomNode(node) {
9
+ // 先判断浏览器环境,避免 SSR 报错;兼容 Node/HTMLElement 类型
10
+ return !!(typeof window !== "undefined" && node && (node instanceof HTMLElement || node instanceof Node));
11
+ }
12
+
13
+ /**
14
+ * 增强版 Formily Schema 克隆函数
15
+ * 解决 lodash cloneDeep 丢失 React DOM/原生 DOM 的问题,兼容多种特殊类型
16
+ * @param {object} schema 待克隆的 Formily Schema
17
+ * @returns {object} 克隆后的 Schema
18
+ */
19
+ export function cloneSchema(schema) {
20
+ // 1. 基础类型(null/undefined/字符串/数字/布尔)直接返回
21
+ if (schema === null || typeof schema !== "object") {
22
+ return schema;
23
+ }
24
+
25
+ // 2. React 虚拟DOM:直接保留原引用,不拷贝
26
+ if (React.isValidElement(schema)) {
27
+ return schema;
28
+ }
29
+
30
+ // 3. 原生 HTML DOM 节点:直接保留原引用
31
+ if (isHtmlDomNode(schema)) {
32
+ return schema;
33
+ }
34
+
35
+ // 4. 函数:Formily Schema 中的校验/格式化函数等,保留原引用
36
+ if (typeof schema === "function") {
37
+ return schema;
38
+ }
39
+
40
+ // 5. 日期对象:创建新的 Date 实例,保证值相同但引用不同
41
+ if (schema instanceof Date) {
42
+ return new Date(schema.getTime());
43
+ }
44
+
45
+ // 6. 正则对象:创建新的 RegExp 实例,保留源和标志
46
+ if (schema instanceof RegExp) {
47
+ return new RegExp(schema.source, schema.flags);
48
+ }
49
+
50
+ // 7. 数组:递归克隆每一项
51
+ if (Array.isArray(schema)) {
52
+ return schema.map((item) => cloneSchema(item));
53
+ }
54
+
55
+ // 8. 普通对象:递归克隆自有属性
56
+ const clonedObj = {};
57
+ for (const key in schema) {
58
+ if (Object.prototype.hasOwnProperty.call(schema, key)) {
59
+ clonedObj[key] = cloneSchema(schema[key]);
60
+ }
61
+ }
62
+ return clonedObj;
63
+ }
@@ -1,48 +1,48 @@
1
- /**
2
- * formily 全局组件
3
- * 项目初始化时进行 formily 组件的挂载,解决 form render 组件日益庞大的问题。
4
- */
5
- window.formilyGlobalComponents = {};
6
-
7
- /**
8
- * 获取全局 formily 组件对象
9
- * @returns {Object} formilyGlobalComponents
10
- */
11
- export const getFormilyGlobalComponents = function () {
12
- return window.formilyGlobalComponents || {};
13
- };
14
-
15
- /**
16
- * 批量设置全局 formily 组件
17
- * @param {Object} components
18
- * @returns {Object} formilyGlobalComponents
19
- */
20
- export const setFormilyGlobalComponents = function (components) {
21
- Object.assign(window.formilyGlobalComponents, components);
22
- return window.formilyGlobalComponents;
23
- };
24
-
25
- /**
26
- * 单个设置全局 formily 组件
27
- * @returns {Object} formilyGlobalComponents
28
- */
29
- /**
30
- * 单个设置全局 formily 组件
31
- * @param {string} key
32
- * @param {ReactComponent} component
33
- * @returns {Object} formilyGlobalComponents
34
- */
35
- export const setFormilyGlobalComponent = function (key, component) {
36
- window.formilyGlobalComponents[key] = component;
37
- return window.formilyGlobalComponents;
38
- };
39
-
40
- /**
41
- * 单个删除全局 formily 组件
42
- * @param {string} key
43
- * @returns {Object} formilyGlobalComponents
44
- */
45
- export const rmFormilyGlobalComponents = function (key) {
46
- delete window.formilyGlobalComponents[key];
47
- return window.formilyGlobalComponents;
48
- };
1
+ /**
2
+ * formily 全局组件
3
+ * 项目初始化时进行 formily 组件的挂载,解决 form render 组件日益庞大的问题。
4
+ */
5
+ window.formilyGlobalComponents = {};
6
+
7
+ /**
8
+ * 获取全局 formily 组件对象
9
+ * @returns {Object} formilyGlobalComponents
10
+ */
11
+ export const getFormilyGlobalComponents = function () {
12
+ return window.formilyGlobalComponents || {};
13
+ };
14
+
15
+ /**
16
+ * 批量设置全局 formily 组件
17
+ * @param {Object} components
18
+ * @returns {Object} formilyGlobalComponents
19
+ */
20
+ export const setFormilyGlobalComponents = function (components) {
21
+ Object.assign(window.formilyGlobalComponents, components);
22
+ return window.formilyGlobalComponents;
23
+ };
24
+
25
+ /**
26
+ * 单个设置全局 formily 组件
27
+ * @returns {Object} formilyGlobalComponents
28
+ */
29
+ /**
30
+ * 单个设置全局 formily 组件
31
+ * @param {string} key
32
+ * @param {ReactComponent} component
33
+ * @returns {Object} formilyGlobalComponents
34
+ */
35
+ export const setFormilyGlobalComponent = function (key, component) {
36
+ window.formilyGlobalComponents[key] = component;
37
+ return window.formilyGlobalComponents;
38
+ };
39
+
40
+ /**
41
+ * 单个删除全局 formily 组件
42
+ * @param {string} key
43
+ * @returns {Object} formilyGlobalComponents
44
+ */
45
+ export const rmFormilyGlobalComponents = function (key) {
46
+ delete window.formilyGlobalComponents[key];
47
+ return window.formilyGlobalComponents;
48
+ };
@@ -1,68 +1,68 @@
1
- import { cloneDeep, uniqueId } from "lodash-es";
2
-
3
- export function mergeSchema(BaseSchema, InsideSchema, title = "") {
4
- const _schema = {
5
- form: BaseSchema.form,
6
- schema: cloneDeep(BaseSchema.schema),
7
- };
8
- let idx = Object.keys(_schema.schema.properties).length;
9
-
10
- if (title) {
11
- _schema.schema.properties.insideTitle = {
12
- type: "string",
13
- "x-component": "Text",
14
- "x-component-props": {
15
- content: title || "内部情况",
16
- },
17
- "x-designable-id": "insideTitle" + idx,
18
- "x-index": idx,
19
- };
20
- }
21
-
22
- Object.keys(InsideSchema.schema.properties).forEach((key) => {
23
- const obj = InsideSchema.schema.properties[key];
24
- idx += 1;
25
- obj["x-index"] = idx;
26
- _schema.schema.properties[key] = obj;
27
- });
28
- return _schema;
29
- }
30
-
31
- export function mergeSchemas(schemas) {
32
- if (!(Array.isArray(schemas) && schemas.length > 0)) {
33
- console.error("请传入 schema");
34
- return;
35
- }
36
- const _schemaList = cloneDeep(schemas);
37
- const _schema = {
38
- form: _schemaList[0].json.form,
39
- schema: {
40
- type: "object",
41
- properties: {},
42
- "x-designable-id": uniqueId("schema-merge-"),
43
- },
44
- };
45
-
46
- let idx = 0;
47
- _schemaList.forEach((schema, i) => {
48
- const titleName = `insideTitle-${i}`;
49
- _schema.schema.properties[titleName] = {
50
- type: "string",
51
- "x-component": "Text",
52
- "x-component-props": {
53
- content: schema.title,
54
- },
55
- "x-designable-id": titleName,
56
- "x-index": idx,
57
- };
58
- idx += 1;
59
- Object.keys(schema.json.schema.properties).forEach((key) => {
60
- const obj = schema.json.schema.properties[key];
61
- obj["x-index"] = idx;
62
- _schema.schema.properties[key] = obj;
63
- idx += 1;
64
- });
65
- });
66
-
67
- return _schema;
68
- }
1
+ import { cloneDeep, uniqueId } from "lodash-es";
2
+
3
+ export function mergeSchema(BaseSchema, InsideSchema, title = "") {
4
+ const _schema = {
5
+ form: BaseSchema.form,
6
+ schema: cloneDeep(BaseSchema.schema),
7
+ };
8
+ let idx = Object.keys(_schema.schema.properties).length;
9
+
10
+ if (title) {
11
+ _schema.schema.properties.insideTitle = {
12
+ type: "string",
13
+ "x-component": "Text",
14
+ "x-component-props": {
15
+ content: title || "内部情况",
16
+ },
17
+ "x-designable-id": "insideTitle" + idx,
18
+ "x-index": idx,
19
+ };
20
+ }
21
+
22
+ Object.keys(InsideSchema.schema.properties).forEach((key) => {
23
+ const obj = InsideSchema.schema.properties[key];
24
+ idx += 1;
25
+ obj["x-index"] = idx;
26
+ _schema.schema.properties[key] = obj;
27
+ });
28
+ return _schema;
29
+ }
30
+
31
+ export function mergeSchemas(schemas) {
32
+ if (!(Array.isArray(schemas) && schemas.length > 0)) {
33
+ console.error("请传入 schema");
34
+ return;
35
+ }
36
+ const _schemaList = cloneDeep(schemas);
37
+ const _schema = {
38
+ form: _schemaList[0].json.form,
39
+ schema: {
40
+ type: "object",
41
+ properties: {},
42
+ "x-designable-id": uniqueId("schema-merge-"),
43
+ },
44
+ };
45
+
46
+ let idx = 0;
47
+ _schemaList.forEach((schema, i) => {
48
+ const titleName = `insideTitle-${i}`;
49
+ _schema.schema.properties[titleName] = {
50
+ type: "string",
51
+ "x-component": "Text",
52
+ "x-component-props": {
53
+ content: schema.title,
54
+ },
55
+ "x-designable-id": titleName,
56
+ "x-index": idx,
57
+ };
58
+ idx += 1;
59
+ Object.keys(schema.json.schema.properties).forEach((key) => {
60
+ const obj = schema.json.schema.properties[key];
61
+ obj["x-index"] = idx;
62
+ _schema.schema.properties[key] = obj;
63
+ idx += 1;
64
+ });
65
+ });
66
+
67
+ return _schema;
68
+ }