Skip to content

Commit 2df99c7

Browse files
brattonrossantfu
authored andcommitted
feat: support kebab and capital cased components
1 parent 1344669 commit 2df99c7

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/context.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ import { ComponentsInfo, ComponentsImportMap, Options } from './types'
22

33
export class Context {
44
importMap: ComponentsImportMap = {}
5-
components: ComponentsInfo[] = []
65
_searchingPromise?: Promise<any>
76

7+
private _components: ComponentsInfo[] = []
88
private importMapPromises: Record<string, [(null | Promise<string[]>), (null | ((result: string[]) => void))]> = {}
99

1010
constructor(
1111
public readonly options: Options,
1212
) {}
1313

14+
get components() {
15+
return this._components
16+
}
17+
18+
set components(components: ComponentsInfo[]) {
19+
this._components = components.map(([name, path]) => [capitalize(camelize(name)), path])
20+
}
21+
1422
async getImportMap(key: string) {
1523
if (this.importMap[key])
1624
return this.importMap[key]
@@ -27,8 +35,17 @@ export class Context {
2735
}
2836

2937
setImportMap(key: string, names: string[]) {
30-
this.importMap[key] = names
38+
const casedNames = names.map(name => capitalize(camelize(name)))
39+
this.importMap[key] = casedNames
3140
if (this.importMapPromises[key])
32-
this.importMapPromises[key][1]?.(names)
41+
this.importMapPromises[key][1]?.(casedNames)
3342
}
3443
}
44+
45+
function camelize(str: string) {
46+
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
47+
}
48+
49+
function capitalize(str: string) {
50+
return str.charAt(0).toUpperCase() + str.slice(1)
51+
}

0 commit comments

Comments
 (0)