generated from unplugin/unplugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.ts
87 lines (79 loc) · 2.19 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import process from 'node:process'
import type { Options } from '../types'
export function debounce(fn: (...args: any) => void, delay: number) {
let timer: NodeJS.Timeout
return function (...args: any[]) {
if (timer)
clearTimeout(timer)
timer = setTimeout(() => {
// @ts-expect-error invalid this
fn.apply(this, args)
clearTimeout(timer)
}, delay)
}
}
export function replace(dts: string, symbolIds: Set<string>, componentName: string) {
return dts.replace(
/\$svg_symbolIds/g,
Array.from(symbolIds).join('" | "'),
).replace(
/\$component_name/g,
componentName,
).replace(
/\$svg_names/g,
Array.from(symbolIds).join('", "'),
)
}
export function resolveOptions(options: Options): Options {
const defaultOptions: Partial<Options> = {
componentName: 'SvgIcon',
dtsDir: process.cwd(),
projectType: 'auto',
vueVersion: 'auto',
svgSpriteDomId: '__svg_sprite__dom__',
componentStyle: 'width: 1em; height: 1em; fill:currentColor;',
symbolIdFormatter(svgName: string, prefix: string) {
const nameArr = svgName.split('\\')
if (prefix)
nameArr.unshift(prefix)
return nameArr.join('-').replace(/\.svg$/, '')
},
treeShaking: true,
scanGlob: [
'**/*.html',
'**/*.pug',
'**/*.vue',
'**/*.js',
'**/*.ts',
'**/*.tsx',
'**/*.jsx',
],
scanStrategy: 'component',
domInsertionStrategy: 'replaceHtml',
}
return {
...defaultOptions,
...options,
}
}
export function transformStyleStrToObject(styleStr: string): Record<string, string> {
return styleStr.replace(/;$/, '').split(';').reduce((ruleMap: Record<string, string>, ruleString) => {
const rulePair = ruleString.split(':')
if (!rulePair[0] || !rulePair[1])
return ruleMap
ruleMap[rulePair[0].trim()] = rulePair[1].trim()
return ruleMap
}, {})
}
export function tranfromToKebabCase(str: string) {
const arr = str.trim().split('')
const result = arr.map((item, index) => {
if (index === 0)
return `${item.toLowerCase()}`
else if (item.toUpperCase() === item)
return `-${item.toLowerCase()}`
else
return item
}).join('')
return result
}