Skip to content

Commit 0f79572

Browse files
committed
utils
1 parent a7994be commit 0f79572

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

src/option/computed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { VueCons } from '../class'
2-
import { obtainSlot, toComponentReverse, getValidNames } from '../utils'
2+
import { obtainSlot, toComponentReverse, getValidOwnPropertyNames } from '../utils'
33
import type { OptionBuilder } from '../optionBuilder'
44

55
export function build(cons: VueCons, optionBuilder: OptionBuilder) {
@@ -9,7 +9,7 @@ export function build(cons: VueCons, optionBuilder: OptionBuilder) {
99
const vanillaMap = slot.obtainMap('vanilla')
1010
const protoArr = toComponentReverse(cons.prototype)
1111
protoArr.forEach(proto => {
12-
getValidNames(proto, (des, name) => {
12+
getValidOwnPropertyNames(proto, (des, name) => {
1313
return (typeof des.get === 'function' || typeof des.set === 'function') && !vanillaMap.has(name)
1414
}).forEach(name => {
1515

src/option/data.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import type { VueCons } from '../class'
22
import type { OptionBuilder } from '../optionBuilder'
3-
import { makeObject, obtainSlot, excludeNames, getValidNames } from '../utils'
3+
import { makeObject, obtainSlot, filterNames, getValidOwnPropertyNames } from '../utils'
44

55
export function build(cons: VueCons, optionBuilder: OptionBuilder, vueInstance: any) {
66
optionBuilder.data ??= {}
77
const sample = new cons(optionBuilder, vueInstance) as any
8-
let names = getValidNames(sample, (des, name) => {
8+
let names = getValidOwnPropertyNames(sample, (des, name) => {
99
return !!des.enumerable
1010
&& !optionBuilder.methods?.[name]
1111
&& !optionBuilder.props?.[name]
1212
})
1313
const slot = obtainSlot(cons.prototype)
14-
names = excludeNames(names, slot,(mapName) => {
15-
//include these names:
16-
//provide, user may access field directly
17-
return !['provide'].includes(mapName)
18-
})
14+
//include these names:
15+
//provide, user may access field directly
16+
//customDecorator
17+
names = filterNames(names, slot, ['provide', 'customDecorator'])
1918
Object.assign(optionBuilder.data, makeObject(names, sample))
2019
}

src/option/methodsAndHooks.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { VueCons } from '../class'
22
import type { OptionBuilder } from '../optionBuilder'
3-
import { obtainSlot, toComponentReverse, excludeNames, getValidNames, optionNullableMemberDecorator } from '../utils'
3+
import { obtainSlot, toComponentReverse, filterNames, getValidOwnPropertyNames, optionNullableMemberDecorator } from '../utils'
44

55
export const HookNames: ReadonlyArray<string> = [
66
"beforeCreate",
@@ -39,16 +39,16 @@ export function build(cons: VueCons, optionBuilder: OptionBuilder) {
3939
const HookFunctions: Record<string, Function> = {}
4040
const MethodFunctions: Record<string, Function> = {}
4141
protoArr.forEach(proto => {
42-
let names = getValidNames(proto, (des, name) => {
42+
let names = getValidOwnPropertyNames(proto, (des, name) => {
4343
return typeof des.value === 'function' && name !== 'constructor'
4444
})
45-
names = excludeNames(names, slot, (mapName) => {
46-
//include these names:
47-
//watch, user may call watch method directly
48-
//hooks, user may call hook method directly
49-
//emits, user may have a method name which is same as one of event names
50-
return !['watch', 'hooks', 'emits', 'provide'].includes(mapName)
51-
});
45+
//include these names:
46+
//watch, user may call watch method directly
47+
//hooks, user may call hook method directly
48+
//emits, user may have a method name which is same as one of event names
49+
//provide, user may access field directly
50+
//customDecorator
51+
names = filterNames(names, slot, ['watch', 'hooks', 'emits', 'provide', 'customDecorator']);
5252
names.forEach(name => {
5353
if (HookNames.includes(name) || map.has(name)) {
5454
HookFunctions[name] = proto[name]

src/utils.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ export type SlotMapTypes = {
3131
setup: Map<string, SetupConfig>
3232
customDecorator: Map<string, CustomDecoratorRecord[]>
3333
}
34+
type SlotMapNames = keyof SlotMapTypes
3435
class Slot {
3536
master: any
3637
constructor(master: any) {
3738
this.master = master
3839
}
39-
names: Map<string, SlotMapTypes[keyof SlotMapTypes]> = new Map()
40+
names: Map<keyof SlotMapTypes, SlotMapTypes[keyof SlotMapTypes]> = new Map()
4041
obtainMap<T extends keyof SlotMapTypes>(name: T): SlotMapTypes[T] {
4142
let map = this.getMap(name)
4243
if (!map) {
@@ -112,26 +113,25 @@ export function getSuperSlot(obj: any) {
112113
}
113114

114115
/**
115-
* Exclude decorated names by a filter
116+
* Filter decorated names
116117
*/
117-
export function excludeNames(names: string[], slot: Slot, filter?: (mapName: string) => boolean) {
118+
export function filterNames(names: string[], slot: Slot, mapNames?: SlotMapNames[]) {
118119
return names.filter(name => {
119120
let currSlot: Slot | null = slot
120121
while (currSlot != null) {
121122
for (const mapName of currSlot.names.keys()) {
122-
if (filter && !filter(mapName)) {
123-
continue
124-
}
125123
if (mapName === 'customDecorator') {
126124
const map = currSlot.obtainMap('customDecorator')
127125
if (map.has(name)) {
128126
if (map.get(name)!.every(ite => !ite.preserve)) {
129127
return false
130-
} else {
131-
continue
132128
}
133129
}
134130
}
131+
if (mapNames && mapNames.includes(mapName)) {
132+
continue
133+
}
134+
135135
const map = currSlot.names.get(mapName)!
136136
if (map.has(name)) {
137137
return false
@@ -145,13 +145,22 @@ export function excludeNames(names: string[], slot: Slot, filter?: (mapName: str
145145
}
146146

147147
/**
148-
* Get own properties by a filter
148+
* Get own propertie name by a filter
149149
*/
150-
export function getValidNames(obj: any, filter: (des: PropertyDescriptor, name: string) => boolean) {
150+
export function getValidOwnPropertyNames(obj: any, filter: (des: PropertyDescriptor, name: string) => boolean) {
151151
const descriptors = Object.getOwnPropertyDescriptors(obj)
152152
return Object.keys(descriptors).filter(name => filter(descriptors[name], name))
153153
}
154154

155+
156+
/**
157+
* Transform provide into function.
158+
*/
159+
export function getProviderFunction(provide: any): () => {} {
160+
if (typeof provide === 'function') return provide
161+
return function () { return provide || {} }
162+
}
163+
155164
export function optionNullableMemberDecorator<T>(handler: { (proto: any, name: string, option?: T): any }) {
156165
function decorator(): any
157166
function decorator(option: T): any//option
@@ -197,7 +206,3 @@ export function optionNullableClassDecorator<T>(handler: { (cons: VueCons, optio
197206
return decorator
198207
}
199208

200-
export function getProviderFunction(provide: any): () => {} {
201-
if (typeof provide === 'function') return provide
202-
return function () { return provide || {} }
203-
}

0 commit comments

Comments
 (0)