This commit is contained in:
zhujingjing 2023-11-11 15:30:11 +08:00
parent 1f43f2a99b
commit dee9007807
Signed by: karlcw
GPG Key ID: B11805D3A0F5C671
17 changed files with 1323 additions and 57 deletions

17
dprint.json Normal file
View File

@ -0,0 +1,17 @@
{
"typescript": {
},
"json": {
},
"markdown": {
},
"excludes": [
"**/node_modules",
"**/*-lock.json"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.88.3.wasm",
"https://plugins.dprint.dev/json-0.19.0.wasm",
"https://plugins.dprint.dev/markdown-0.16.2.wasm"
]
}

View File

@ -1,2 +0,0 @@
import { stringTokenizer } from "./str-utils";
export { stringTokenizer };

11
lib/commons/record-util.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
import { UnwrapNestedRefs } from 'vue';
declare enum ClearMode {
clear = 3,
delete = 2,
reset = 1
}
export declare const withRecord: (obj: UnwrapNestedRefs<Record<string, any>>) => {
clear: (mode?: ClearMode) => void;
replace: (newVal: UnwrapNestedRefs<Record<string, any>>) => void;
};
export {};

5
lib/index.d.ts vendored
View File

@ -1,2 +1,3 @@
import { stringTokenizer } from "./commons";
export { stringTokenizer };
import { withRecord } from "./commons/record-util";
import { stringTokenizer } from "./commons/str-utils";
export { stringTokenizer, withRecord };

View File

@ -1,16 +1,32 @@
function r(u, i, o) {
const h = i, s = o || i;
return function(e) {
const n = [], l = e.matchAll(u);
let t = 0;
for (const c of l) {
const f = e.slice(t, c.index);
f && n.push(s(f, !1)), n.push(h(c[0], !0)), t = c.index + c[0].length;
const u = (e) => ({
clear: function(t = 2) {
t === 1 ? Object.keys(e).forEach((c) => {
e[c] = void 0;
}) : t === 2 ? Object.keys(e).forEach((c) => {
delete e[c];
}) : Object.assign(e, {});
},
replace: function(t) {
this.clear(
3
/* clear */
), Object.assign(e, t);
}
});
function a(e, t, c) {
const r = t, l = c || t;
return function(n) {
const s = [], h = n.matchAll(e);
let i = 0;
for (const f of h) {
const o = n.slice(i, f.index);
o && s.push(l(o, !1)), s.push(r(f[0], !0)), i = f.index + f[0].length;
}
return t < e.length && n.push(s(e.slice(t), !1)), n;
return i < n.length && s.push(l(n.slice(i), !1)), s;
};
}
export {
r as stringTokenizer
a as stringTokenizer,
u as withRecord
};
//# sourceMappingURL=index.es.js.map

View File

@ -1 +1 @@
{"version":3,"file":"index.es.js","sources":["../src/commons/str-utils.ts"],"sourcesContent":["/**\r\n * 生成给定函数体的函数注释。\r\n *\r\n * @param {RegExp} regExp - 用于匹配的正则表达式。\r\n * @param {(text: string, match: boolean) => T} matchHandler - 处理匹配到的文本的函数。\r\n * @param {(text: string, match: boolean) => T} [textHandler] - 处理未匹配到的文本的函数。\r\n * @return {(str: string) => T[]} - 分词器函数。\r\n */\r\n export function stringTokenizer<T>(\r\n regExp: RegExp,\r\n matchHandler: (text: string, match: boolean) => T,\r\n textHandler?: (text: string, match: boolean) => T\r\n ): (str: string) => T[] {\r\n\r\n const ifMatch = matchHandler;\r\n const ifText = textHandler?textHandler: matchHandler;\r\n\r\n return function (str: string) {\r\n const result: T[] = [];\r\n const matches = str.matchAll(regExp);\r\n let index = 0;\r\n for (const match of matches) {\r\n const before = str.slice(index, match.index);\r\n if (before) {\r\n result.push(ifText(before, false));\r\n }\r\n result.push(ifMatch(match[0], true));\r\n index = match.index! + match[0].length;\r\n }\r\n if (index < str.length) {\r\n result.push(ifText(str.slice(index), false));\r\n }\r\n return result;\r\n };\r\n }"],"names":["stringTokenizer","regExp","matchHandler","textHandler","ifMatch","ifText","str","result","matches","index","match","before"],"mappings":"AAQkB,SAAAA,EACdC,GACAC,GACAC,GACsB;AAEtB,QAAMC,IAAUF,GACVG,IAASF,KAAyBD;AAExC,SAAO,SAAUI,GAAa;AAC5B,UAAMC,IAAc,CAAA,GACdC,IAAUF,EAAI,SAASL,CAAM;AACnC,QAAIQ,IAAQ;AACZ,eAAWC,KAASF,GAAS;AAC3B,YAAMG,IAASL,EAAI,MAAMG,GAAOC,EAAM,KAAK;AAC3C,MAAIC,KACFJ,EAAO,KAAKF,EAAOM,GAAQ,EAAK,CAAC,GAEnCJ,EAAO,KAAKH,EAAQM,EAAM,CAAC,GAAG,EAAI,CAAC,GACnCD,IAAQC,EAAM,QAASA,EAAM,CAAC,EAAE;AAAA,IAClC;AACI,WAAAD,IAAQH,EAAI,UACdC,EAAO,KAAKF,EAAOC,EAAI,MAAMG,CAAK,GAAG,EAAK,CAAC,GAEtCF;AAAA,EAAA;AAEX;"}
{"version":3,"file":"index.es.js","sources":["../src/commons/record-util.ts","../src/commons/str-utils.ts"],"sourcesContent":["import { UnwrapNestedRefs } from 'vue';\r\nenum ClearMode {\r\n clear = 3,\r\n delete = 2,\r\n reset = 1\r\n}\r\nexport const withRecord = (\r\n obj: UnwrapNestedRefs<Record<string, any>>,\r\n) => {\r\n\r\n return {\r\n clear: function (mode: ClearMode = ClearMode.delete) {\r\n if (mode === ClearMode.reset) {\r\n Object.keys(obj).forEach((key) => {\r\n obj[key] = undefined;\r\n });\r\n } else if (mode === ClearMode.delete) {\r\n Object.keys(obj).forEach((key) => {\r\n delete obj[key];\r\n });\r\n } else {\r\n Object.assign(obj, {});\r\n }\r\n },\r\n replace: function (newVal: UnwrapNestedRefs<Record<string, any>>) {\r\n this.clear(ClearMode.clear);\r\n Object.assign(obj, newVal);\r\n }\r\n };\r\n};","/**\r\n * 生成给定函数体的函数注释。\r\n *\r\n * @param {RegExp} regExp - 用于匹配的正则表达式。\r\n * @param {(text: string, match: boolean) => T} matchHandler - 处理匹配到的文本的函数。\r\n * @param {(text: string, match: boolean) => T} [textHandler] - 处理未匹配到的文本的函数。\r\n * @return {(str: string) => T[]} - 分词器函数。\r\n */\r\n export function stringTokenizer<T>(\r\n regExp: RegExp,\r\n matchHandler: (text: string, match: boolean) => T,\r\n textHandler?: (text: string, match: boolean) => T\r\n ): (str: string) => T[] {\r\n\r\n const ifMatch = matchHandler;\r\n const ifText = textHandler?textHandler: matchHandler;\r\n\r\n return function (str: string) {\r\n const result: T[] = [];\r\n const matches = str.matchAll(regExp);\r\n let index = 0;\r\n for (const match of matches) {\r\n const before = str.slice(index, match.index);\r\n if (before) {\r\n result.push(ifText(before, false));\r\n }\r\n result.push(ifMatch(match[0], true));\r\n index = match.index! + match[0].length;\r\n }\r\n if (index < str.length) {\r\n result.push(ifText(str.slice(index), false));\r\n }\r\n return result;\r\n };\r\n }"],"names":["withRecord","obj","mode","key","newVal","stringTokenizer","regExp","matchHandler","textHandler","ifMatch","ifText","str","result","matches","index","match","before"],"mappings":"AAMa,MAAAA,IAAa,CACtBC,OAGO;AAAA,EACH,OAAO,SAAUC,IAAkB,GAAkB;AACjD,IAAIA,MAAS,IACT,OAAO,KAAKD,CAAG,EAAE,QAAQ,CAACE,MAAQ;AAC9B,MAAAF,EAAIE,CAAG,IAAI;AAAA,IAAA,CACd,IACMD,MAAS,IAChB,OAAO,KAAKD,CAAG,EAAE,QAAQ,CAACE,MAAQ;AAC9B,aAAOF,EAAIE,CAAG;AAAA,IAAA,CACjB,IAEM,OAAA,OAAOF,GAAK,CAAA,CAAE;AAAA,EAE7B;AAAA,EACA,SAAS,SAAUG,GAA+C;AAC9D,SAAK;AAAA,MAAM;AAAA;AAAA,OACJ,OAAA,OAAOH,GAAKG,CAAM;AAAA,EAC7B;AAAA;ACnBU,SAAAC,EACdC,GACAC,GACAC,GACsB;AAEtB,QAAMC,IAAUF,GACVG,IAASF,KAAyBD;AAExC,SAAO,SAAUI,GAAa;AAC5B,UAAMC,IAAc,CAAA,GACdC,IAAUF,EAAI,SAASL,CAAM;AACnC,QAAIQ,IAAQ;AACZ,eAAWC,KAASF,GAAS;AAC3B,YAAMG,IAASL,EAAI,MAAMG,GAAOC,EAAM,KAAK;AAC3C,MAAIC,KACFJ,EAAO,KAAKF,EAAOM,GAAQ,EAAK,CAAC,GAEnCJ,EAAO,KAAKH,EAAQM,EAAM,CAAC,GAAG,EAAI,CAAC,GACnCD,IAAQC,EAAM,QAASA,EAAM,CAAC,EAAE;AAAA,IAClC;AACI,WAAAD,IAAQH,EAAI,UACdC,EAAO,KAAKF,EAAOC,EAAI,MAAMG,CAAK,GAAG,EAAK,CAAC,GAEtCF;AAAA,EAAA;AAEX;"}

2
lib/index.umd.js Normal file
View File

@ -0,0 +1,2 @@
(function(n,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(n=typeof globalThis<"u"?globalThis:n||self,c(n["ez-common-ts"]={}))})(this,function(n){"use strict";const c=e=>({clear:function(t=2){t===1?Object.keys(e).forEach(i=>{e[i]=void 0}):t===2?Object.keys(e).forEach(i=>{delete e[i]}):Object.assign(e,{})},replace:function(t){this.clear(3),Object.assign(e,t)}});function h(e,t,i){const d=t,l=i||t;return function(s){const f=[],a=s.matchAll(e);let o=0;for(const u of a){const r=s.slice(o,u.index);r&&f.push(l(r,!1)),f.push(d(u[0],!0)),o=u.index+u[0].length}return o<s.length&&f.push(l(s.slice(o),!1)),f}}n.stringTokenizer=h,n.withRecord=c,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=index.umd.js.map

1
lib/index.umd.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.umd.js","sources":["../src/commons/record-util.ts","../src/commons/str-utils.ts"],"sourcesContent":["import { UnwrapNestedRefs } from 'vue';\r\nenum ClearMode {\r\n clear = 3,\r\n delete = 2,\r\n reset = 1\r\n}\r\nexport const withRecord = (\r\n obj: UnwrapNestedRefs<Record<string, any>>,\r\n) => {\r\n\r\n return {\r\n clear: function (mode: ClearMode = ClearMode.delete) {\r\n if (mode === ClearMode.reset) {\r\n Object.keys(obj).forEach((key) => {\r\n obj[key] = undefined;\r\n });\r\n } else if (mode === ClearMode.delete) {\r\n Object.keys(obj).forEach((key) => {\r\n delete obj[key];\r\n });\r\n } else {\r\n Object.assign(obj, {});\r\n }\r\n },\r\n replace: function (newVal: UnwrapNestedRefs<Record<string, any>>) {\r\n this.clear(ClearMode.clear);\r\n Object.assign(obj, newVal);\r\n }\r\n };\r\n};","/**\r\n * 生成给定函数体的函数注释。\r\n *\r\n * @param {RegExp} regExp - 用于匹配的正则表达式。\r\n * @param {(text: string, match: boolean) => T} matchHandler - 处理匹配到的文本的函数。\r\n * @param {(text: string, match: boolean) => T} [textHandler] - 处理未匹配到的文本的函数。\r\n * @return {(str: string) => T[]} - 分词器函数。\r\n */\r\n export function stringTokenizer<T>(\r\n regExp: RegExp,\r\n matchHandler: (text: string, match: boolean) => T,\r\n textHandler?: (text: string, match: boolean) => T\r\n ): (str: string) => T[] {\r\n\r\n const ifMatch = matchHandler;\r\n const ifText = textHandler?textHandler: matchHandler;\r\n\r\n return function (str: string) {\r\n const result: T[] = [];\r\n const matches = str.matchAll(regExp);\r\n let index = 0;\r\n for (const match of matches) {\r\n const before = str.slice(index, match.index);\r\n if (before) {\r\n result.push(ifText(before, false));\r\n }\r\n result.push(ifMatch(match[0], true));\r\n index = match.index! + match[0].length;\r\n }\r\n if (index < str.length) {\r\n result.push(ifText(str.slice(index), false));\r\n }\r\n return result;\r\n };\r\n }"],"names":["withRecord","obj","mode","key","newVal","stringTokenizer","regExp","matchHandler","textHandler","ifMatch","ifText","str","result","matches","index","match","before"],"mappings":"uOAMa,MAAAA,EACTC,IAGO,CACH,MAAO,SAAUC,EAAkB,EAAkB,CAC7CA,IAAS,EACT,OAAO,KAAKD,CAAG,EAAE,QAASE,GAAQ,CAC9BF,EAAIE,CAAG,EAAI,MAAA,CACd,EACMD,IAAS,EAChB,OAAO,KAAKD,CAAG,EAAE,QAASE,GAAQ,CAC9B,OAAOF,EAAIE,CAAG,CAAA,CACjB,EAEM,OAAA,OAAOF,EAAK,CAAA,CAAE,CAE7B,EACA,QAAS,SAAUG,EAA+C,CAC9D,KAAK,MAAM,GACJ,OAAA,OAAOH,EAAKG,CAAM,CAC7B,CAAA,GCnBU,SAAAC,EACdC,EACAC,EACAC,EACsB,CAEtB,MAAMC,EAAUF,EACVG,EAASF,GAAyBD,EAExC,OAAO,SAAUI,EAAa,CAC5B,MAAMC,EAAc,CAAA,EACdC,EAAUF,EAAI,SAASL,CAAM,EACnC,IAAIQ,EAAQ,EACZ,UAAWC,KAASF,EAAS,CAC3B,MAAMG,EAASL,EAAI,MAAMG,EAAOC,EAAM,KAAK,EACvCC,GACFJ,EAAO,KAAKF,EAAOM,EAAQ,EAAK,CAAC,EAEnCJ,EAAO,KAAKH,EAAQM,EAAM,CAAC,EAAG,EAAI,CAAC,EACnCD,EAAQC,EAAM,MAASA,EAAM,CAAC,EAAE,MAClC,CACI,OAAAD,EAAQH,EAAI,QACdC,EAAO,KAAKF,EAAOC,EAAI,MAAMG,CAAK,EAAG,EAAK,CAAC,EAEtCF,CAAA,CAEX"}

View File

@ -1,15 +1,30 @@
{
"name": "ez-common-ts",
"private": true,
"version": "0.0.0",
"version": "0.0.1",
"author": "zjj",
"type": "module",
"main": "./lib/index.umd.js",
"module": "./lib/index.es.js",
"types": "./lib/index.d.ts",
"scripts": {
"build": "vite build && tsc -p tsconfig.build.json"
"build1": "tsc && vite build",
"build": "vite build && tsc -p tsconfig.build.json",
"test": "vitest"
},
"devDependencies": {
"typescript": "^5.0.2",
"vite": "^4.4.5"
"vite": "^4.4.5",
"vite-plugin-dts": "^3.6.3",
"vue": "^3.2.47"
},
"module": "./lib/index.es.js",
"types": "./lib/index.d.ts"
"exports": {
".": {
"import": "./lib/index.es.js",
"require": "./lib/index.umd.js"
}
},
"dependencies": {
"vitest": "^0.34.6"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +0,0 @@
import { stringTokenizer } from "./str-utils";
export { stringTokenizer };

View File

@ -0,0 +1,30 @@
import { UnwrapNestedRefs } from 'vue';
enum ClearMode {
clear = 3,
delete = 2,
reset = 1
}
export const withRecord = (
obj: UnwrapNestedRefs<Record<string, any>>,
) => {
return {
clear: function (mode: ClearMode = ClearMode.delete) {
if (mode === ClearMode.reset) {
Object.keys(obj).forEach((key) => {
obj[key] = undefined;
});
} else if (mode === ClearMode.delete) {
Object.keys(obj).forEach((key) => {
delete obj[key];
});
} else {
Object.assign(obj, {});
}
},
replace: function (newVal: UnwrapNestedRefs<Record<string, any>>) {
this.clear(ClearMode.clear);
Object.assign(obj, newVal);
}
};
};

View File

@ -1,3 +1,4 @@
import { stringTokenizer } from "./commons";
import { withRecord } from "./commons/record-util";
import { stringTokenizer } from "./commons/str-utils";
export { stringTokenizer }
export { stringTokenizer, withRecord };

15
test/withRecord.test.ts Normal file
View File

@ -0,0 +1,15 @@
import { describe, expect, test } from "vitest";
import { withRecord } from "../src/commons/record-util";
describe("withRecord", () => {
test("clear", () => {
const oirgianlRec = { a: 1 };
withRecord(oirgianlRec).clear();
expect(oirgianlRec).toEqual({});
});
test("replace", () => {
const oirgianlRec = { a: 1 };
withRecord(oirgianlRec).replace({ a: 2 });
expect(oirgianlRec).toEqual({ a: 2 });
});
});

View File

@ -6,7 +6,5 @@
"emitDeclarationOnly": true, //
"declarationDir": "lib" //
},
"include": [
"src"
]
}

View File

@ -12,7 +12,7 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"declaration": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,

View File

@ -1,4 +1,5 @@
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
// https://vitejs.dev/config/
@ -7,15 +8,16 @@ export default defineConfig({
// 打包配置
build: {
lib: {
entry: 'src/index.ts', // 设置入口文件
entry: 'src/index.ts', // 设置入口文件
name: 'ez-common-ts', // 起个名字,安装、引入用
formats: ['es'],
// formats: ['es'],
fileName: (format) => `index.${format}.js` // 打包后的文件名
},
sourcemap: true, // 输出.map文件
rollupOptions: {
// 确保外部化处理那些你不想打包进库的依赖
//external: ['vue', 'ant-design-vue'],
external: ['vue', "vitest"]
/*output: {
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
globals: {
@ -26,4 +28,6 @@ export default defineConfig({
},
outDir: 'lib' // 默认为 dist
},
plugins: [dts()]
})