This commit is contained in:
zhujingjing 2023-11-10 16:35:59 +08:00
parent d552dec366
commit 1f43f2a99b
Signed by: karlcw
GPG Key ID: B11805D3A0F5C671
15 changed files with 81 additions and 57 deletions

View File

@ -1,13 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

View File

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

2
lib/commons/index.d.ts vendored Normal file
View File

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

9
lib/commons/str-utils.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
/**
*
*
* @param {RegExp} regExp -
* @param {(text: string, match: boolean) => T} matchHandler -
* @param {(text: string, match: boolean) => T} [textHandler] -
* @return {(str: string) => T[]} -
*/
export declare function stringTokenizer<T>(regExp: RegExp, matchHandler: (text: string, match: boolean) => T, textHandler?: (text: string, match: boolean) => T): (str: string) => T[];

2
lib/index.d.ts vendored Normal file
View File

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

16
lib/index.es.js Normal file
View File

@ -0,0 +1,16 @@
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;
}
return t < e.length && n.push(s(e.slice(t), !1)), n;
};
}
export {
r as stringTokenizer
};
//# sourceMappingURL=index.es.js.map

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

@ -0,0 +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;"}

View File

@ -4,12 +4,12 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
"build": "vite build && tsc -p tsconfig.build.json"
},
"devDependencies": {
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
},
"module": "./lib/index.es.js",
"types": "./lib/index.d.ts"
}

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,9 +0,0 @@
export function setupCounter(element: HTMLButtonElement) {
let counter = 0
const setCounter = (count: number) => {
counter = count
element.innerHTML = `count is ${counter}`
}
element.addEventListener('click', () => setCounter(counter + 1))
setCounter(0)
}

3
src/index.ts Normal file
View File

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

View File

@ -1,24 +0,0 @@
import './style.css'
import typescriptLogo from './typescript.svg'
import viteLogo from '/vite.svg'
import { setupCounter } from './counter.ts'
document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="${viteLogo}" class="logo" alt="Vite logo" />
</a>
<a href="https://www.typescriptlang.org/" target="_blank">
<img src="${typescriptLogo}" class="logo vanilla" alt="TypeScript logo" />
</a>
<h1>Vite + TypeScript</h1>
<div class="card">
<button id="counter" type="button"></button>
</div>
<p class="read-the-docs">
Click on the Vite and TypeScript logos to learn more
</p>
</div>
`
setupCounter(document.querySelector<HTMLButtonElement>('#counter')!)

12
tsconfig.build.json Normal file
View File

@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json", // tsconfig.json
"compilerOptions": {
"noEmit": false, //
"declaration": true, // true
"emitDeclarationOnly": true, //
"declarationDir": "lib" //
},
"include": [
"src"
]
}

View File

@ -12,12 +12,12 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src", "index.ts", "index.ts"]
"include": ["src"]
}

29
vite.config.js Normal file
View File

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