Skip to content

Commit bf64b94

Browse files
committed
improve utils.ts
1 parent 3f0410a commit bf64b94

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/identity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type IdentityType = {
88
type AssertIs<O, T extends O> = T
99

1010
export interface Identity<T extends IdentityType = IdentityType> {
11+
[index: string | number | symbol]: any
1112
[IdentitySymbol]: T
1213
}
1314

src/option/data.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { VueCons } from '../class'
22
import type { OptionBuilder } from '../optionBuilder'
3-
import { makeObject, filterNames, getValidOwnPropertyNames } from '../utils'
3+
import { filterNames, getValidOwnPropertyNames } from '../utils'
44
import { obtainSlot } from '../slot'
55
export function build(cons: VueCons, optionBuilder: OptionBuilder, vueInstance: any) {
66
optionBuilder.data ??= {}
7-
const sample = new cons(optionBuilder, vueInstance) as any
7+
const sample = new cons(optionBuilder, vueInstance)
88
let names = getValidOwnPropertyNames(sample, (des, name) => {
99
return !!des.enumerable
1010
&& !optionBuilder.methods?.[name]
@@ -15,5 +15,10 @@ export function build(cons: VueCons, optionBuilder: OptionBuilder, vueInstance:
1515
//provide, user may access field directly
1616
//customDecorator
1717
names = filterNames(names, slot, ['provide', 'customDecorator'])
18-
Object.assign(optionBuilder.data, makeObject(names, sample))
18+
Object.assign(optionBuilder.data,
19+
names.reduce<Record<string, any>>((pv, cv) => {
20+
pv[cv] = sample[cv]
21+
return pv
22+
}, {})
23+
)
1924
}

src/utils.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@ import { compatibleMemberDecorator, compatibleClassDecorator } from './deco3/uti
33
import { type VueCons, Base } from './class';
44
import { getSlot, type Slot, type SlotMapNames } from './slot'
55

6-
export function makeObject(names: string[], obj: any) {
7-
return names.reduce<Record<string, any>>((pv, cv) => {
8-
pv[cv] = obj[cv]
9-
return pv
10-
}, {})
6+
export function getPrototypeOf(proto: Identity): Identity | null {
7+
const p = Object.getPrototypeOf(proto)
8+
if (!(p instanceof Base)) {
9+
return null
10+
}
11+
return p
1112
}
1213

13-
export function toComponentReverse(obj: any) {
14-
const arr: any[] = []
15-
let curr = obj
14+
export function toComponentReverse(proto: Identity) {
15+
const arr: Identity[] = []
16+
let curr: Identity | null = proto
1617
do {
1718
arr.unshift(curr)
18-
curr = Object.getPrototypeOf(curr)
19-
} while (curr.constructor !== Base && !getSlot(curr))
19+
curr = getPrototypeOf(curr)
20+
} while (curr !== null && !getSlot(curr))
2021
return arr
2122
}
2223

23-
export function getSuperSlot(obj: any) {
24-
let curr = Object.getPrototypeOf(obj)
24+
export function getSuperSlot(proto: Identity) {
25+
let curr = getPrototypeOf(proto)
2526

26-
while (curr.constructor !== Base) {
27+
while (curr !== null) {
2728
const slot = getSlot(curr)
2829
if (slot) {
2930
return slot
3031
}
31-
curr = Object.getPrototypeOf(curr)
32+
curr = getPrototypeOf(proto)
3233
}
3334
return null
3435
}

0 commit comments

Comments
 (0)