Skip to content

Commit c132c27

Browse files
committed
feat(resolveConfig)!: add option options.camelizePropNames, move props to options.props
1 parent c2c9cfe commit c132c27

File tree

7 files changed

+67
-47
lines changed

7 files changed

+67
-47
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,16 @@ Get global props, attrs, listeners & hooks by analysing global parameters passed
190190

191191
```ts
192192
/**
193-
* @param {object} globalConfig - global parameters
194-
* @param {string[] | object} [localProps] - local props, for the purpose of differentiating between props and attrs
193+
* @param {Record<keyof any, any>} globalConfig - global parameters
194+
* @param {Record<keyof any, any>} [options] - Options
195+
* @param {string[] | Record<keyof any, any>} [options.props = []] - local props, for the purpose of differentiating between props and attrs
196+
* @param {boolean} [options.camelizePropNames = false] - whether to camelize prop names
195197
* @returns {{
196-
* props: object,
197-
* attrs: object,
198-
* listeners: object,
199-
* hooks: object
198+
* props: Record<keyof any, any>,
199+
* attrs: Record<keyof any, any>,
200+
* listeners: Record<string, (...args: any) => unknown>,
201+
* hooks: Record<string, (...args: any) => unknown>
202+
* slots: Record<string, (...args: any) => unknown>
200203
* }} global props, attrs, listeners & hooks
201204
*/
202205
```
@@ -215,6 +218,7 @@ resolveConfig({
215218
'placeholder': 'some attr',
216219
'@blur': function () {},
217220
'@hook:mounted': function () {},
221+
'#left-footer': () => h('Fragment', undefined, 'Global Slot'),
218222
})
219223
```
220224

@@ -247,17 +251,17 @@ The role of `conclude` is to help you figure out the final configuration.
247251
```ts
248252
/**
249253
* @param {any[]} configSequence - Config sequence (priority from highest to lowest, last is the default value)
250-
* @param {object} [config] - Configuration
251-
* @param {PropType<any>} [config.type] - Data type checking
252-
* @param {any} [config.default] - Default value (explicit)
253-
* @param {boolean} [config.defaultIsDynamic = false] - Dynamic generation of default values
254-
* @param {boolean} [config.required = false] - Requirement checking
255-
* @param {Function} [config.validator] - Custom validator
256-
* @param {string} [config.camelizeObjectKeys = false] - Whether or not to camelize object keys
257-
* @param {false|string} [config.mergeObject = 'deep'] - The way to merge objects
258-
* @param {boolean} [config.mergeObjectApplyOnlyToDefault = false] - `mergeObject` only works on `default`
259-
* @param {false|((accumulator, currentValue, index?, array?) => )} [config.mergeFunction = false] - The way to fuse functions
260-
* @param {boolean} [config.mergeFunctionApplyOnlyToDefault = true] - `mergeFunction` only works on `default`
254+
* @param {Record<keyof any, any>} [options] - Options
255+
* @param {PropType<any>} [options.type] - Data type checking
256+
* @param {any} [options.default] - Default value (explicit)
257+
* @param {boolean} [options.defaultIsDynamic = false] - Dynamic generation of default values
258+
* @param {boolean} [options.required = false] - Requirement checking
259+
* @param {(prop: any) => boolean} [options.validator] - Custom validator
260+
* @param {string} [options.camelizeObjectKeys = false] - whether to camelize object keys
261+
* @param {false|string} [options.mergeObject = 'deep'] - The way to merge objects
262+
* @param {boolean} [options.mergeObjectApplyOnlyToDefault = false] - `mergeObject` only works on `default`
263+
* @param {false|((accumulator, currentValue, index?, array?) => )} [options.mergeFunction = false] - The way to fuse functions
264+
* @param {boolean} [options.mergeFunctionApplyOnlyToDefault = true] - `mergeFunction` only works on `default`
261265
* @returns {any} Final prop
262266
*/
263267
```

demo/vue2.7/YourComponent/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Component from './Component.vue'
44

55
type SFCWithInstall<T> = T & Plugin & { install: typeof install }
66

7-
function withInstall<T, E extends Record<string, any>>(main: T, extra?: E) {
7+
function withInstall<T, E extends Record<keyof any, any>>(main: T, extra?: E) {
88
(main as SFCWithInstall<T>).install = (app): void => {
99
for (const comp of [main, ...Object.values(extra ?? {})]) {
1010
app?.component(comp.name, comp)
@@ -19,11 +19,11 @@ function withInstall<T, E extends Record<string, any>>(main: T, extra?: E) {
1919
return main as SFCWithInstall<T> & E
2020
}
2121

22-
const globalProps: Record<string, any> = {}
23-
const globalAttrs: Record<string, any> = {}
24-
const globalListeners: Record<string, any> = {}
25-
const globalHooks: Record<string, any> = {}
26-
const globalSlots: Record<string, any> = {}
22+
const globalProps: Record<keyof any, any> = {}
23+
const globalAttrs: Record<keyof any, any> = {}
24+
const globalListeners: Record<keyof any, any> = {}
25+
const globalHooks: Record<keyof any, any> = {}
26+
const globalSlots: Record<keyof any, any> = {}
2727

2828
const ComponentWithInstall = withInstall(Component)
2929

demo/vue3/YourComponent/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Component from './Component.vue'
44

55
type SFCWithInstall<T> = T & Plugin & { install: typeof install }
66

7-
function withInstall<T, E extends Record<string, any>>(main: T, extra?: E) {
7+
function withInstall<T, E extends Record<keyof any, any>>(main: T, extra?: E) {
88
(main as SFCWithInstall<T>).install = (app): void => {
99
for (const comp of [main, ...Object.values(extra ?? {})]) {
1010
app?.component(comp.name, comp)
@@ -19,11 +19,11 @@ function withInstall<T, E extends Record<string, any>>(main: T, extra?: E) {
1919
return main as SFCWithInstall<T> & E
2020
}
2121

22-
const globalProps: Record<string, any> = {}
23-
const globalAttrs: Record<string, any> = {}
24-
const globalListeners: Record<string, any> = {}
25-
const globalHooks: Record<string, any> = {}
26-
const globalSlots: Record<string, any> = {}
22+
const globalProps: Record<keyof any, any> = {}
23+
const globalAttrs: Record<keyof any, any> = {}
24+
const globalListeners: Record<keyof any, any> = {}
25+
const globalHooks: Record<keyof any, any> = {}
26+
const globalSlots: Record<keyof any, any> = {}
2727

2828
const ComponentWithInstall = withInstall(Component)
2929

src/conclude.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function MergeFunction(
148148

149149
export default function conclude(
150150
configSequence: any[],
151-
config: {
151+
options: {
152152
type?: PropType<any>
153153
default?: any
154154
defaultIsDynamic?: boolean
@@ -172,9 +172,9 @@ export default function conclude(
172172
camelizeObjectKeys = false,
173173
mergeObjectApplyOnlyToDefault = false,
174174
mergeFunctionApplyOnlyToDefault = true,
175-
} = config
175+
} = options
176176

177-
let { mergeObject = MergeObjectOptions.deep, mergeFunction = false } = config
177+
let { mergeObject = MergeObjectOptions.deep, mergeFunction = false } = options
178178

179179
const configSequenceCopy: any[] = []
180180
let result: any
@@ -214,7 +214,7 @@ export default function conclude(
214214
configSequenceCopy.push(handleProp(defaultValue))
215215
} else if (!(typeof defaultValue === 'function')) {
216216
throw new TypeError(
217-
`Invalid option: config.default should be Function when config.defaultIsDynamic enabled, receiving: ${defaultValue}`,
217+
`Invalid option: options.default should be Function when options.defaultIsDynamic enabled, receiving: ${defaultValue}`,
218218
)
219219
}
220220

@@ -270,7 +270,7 @@ export default function conclude(
270270

271271
if (defaultIsDynamic) {
272272
return conclude(configSequence, {
273-
...config,
273+
...options,
274274
default: defaultValue(result),
275275
defaultIsDynamic: false,
276276
})

src/getLocalListeners.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
function objectFilter(
2-
obj: Record<string, any>,
2+
obj: Record<keyof any, any>,
33
predicate: (key: string) => boolean,
4-
): Record<string, any> {
4+
): Record<keyof any, any> {
55
return Object.keys(obj)
66
.filter(key => predicate(key))
7-
.reduce((prev: Record<string, any>, curr) => {
7+
.reduce((prev: Record<keyof any, any>, curr) => {
88
prev[curr] = obj[curr]
99
return prev
1010
}, {})
1111
}
1212

13-
export default function getLocalListeners(listeners: Record<string, any>) {
13+
export default function getLocalListeners(listeners: Record<keyof any, any>) {
1414
return objectFilter(listeners, key => !key.startsWith('hook:'))
1515
}

src/listenGlobalHooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ComponentPublicInstance } from 'vue-demi'
66

77
export default function listenGlobalHooks(
88
this: ComponentPublicInstance,
9-
globalHooks: Record<string, any>,
9+
globalHooks: Record<keyof any, any>,
1010
) {
1111
if (Object.getOwnPropertyNames(globalHooks || {}).length) {
1212
const originalEmit = this.$emit

src/resolveConfig.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ function atToOn(eventName: string) {
99
}
1010

1111
interface ResolvedResult {
12-
props: Record<string, any>
13-
attrs: Record<string, any>
12+
props: Record<keyof any, any>
13+
attrs: Record<keyof any, any>
1414
listeners: Record<string, (...args: any) => unknown>
1515
hooks: Record<string, (...args: any) => unknown>
1616
slots: Record<string, (...args: any) => unknown>
1717
}
1818

1919
export default function resolveConfig(
20-
config: Record<string, any>,
21-
props: string[] | Record<string, any> = [],
20+
config: Record<keyof any, any>,
21+
{
22+
props = [],
23+
camelizePropNames = false,
24+
}: {
25+
props?: string[] | Record<keyof any, any>
26+
camelizePropNames?: boolean
27+
} = {},
2228
): ResolvedResult {
2329
const res: ResolvedResult = {
2430
props: {},
@@ -28,7 +34,17 @@ export default function resolveConfig(
2834
slots: {},
2935
}
3036

31-
const propsList = (Array.isArray(props) ? props : Object.keys(props)).map(v => kebabToCamel(v))
37+
let propsList
38+
if (Array.isArray(props)) {
39+
propsList = camelizePropNames ? props.map(v => kebabToCamel(v)) : props
40+
} else if (camelizePropNames) {
41+
propsList = []
42+
for (const k in props) {
43+
propsList.push(kebabToCamel(k))
44+
}
45+
} else {
46+
propsList = Object.keys(props)
47+
}
3248

3349
for (const k in config) {
3450
if (k.startsWith('@')) {
@@ -75,9 +91,9 @@ export default function resolveConfig(
7591
res.slots[slotName] = config[k]
7692
}
7793
} else {
78-
const camelizedKey = kebabToCamel(k)
79-
if (propsList.includes(camelizedKey)) {
80-
res.props[camelizedKey] = config[k]
94+
const propName = camelizePropNames ? kebabToCamel(k) : k
95+
if (propsList.includes(propName)) {
96+
res.props[propName] = config[k]
8197
} else {
8298
res.attrs[k] = config[k]
8399
}

0 commit comments

Comments
 (0)