mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-25 02:10:33 +03:00
fix: fix/improve resolver types
This commit is contained in:
@@ -17,9 +17,9 @@ export type PathSegments = Array<string>
|
|||||||
|
|
||||||
export type ResolveContext = {}
|
export type ResolveContext = {}
|
||||||
|
|
||||||
export type ResolveMethod = (
|
export type ResolveMethod<T = ResolveContext> = (
|
||||||
options: Array<any>,
|
options: Array<any>,
|
||||||
contexts: Array<ResolveContext>,
|
contexts: Array<T>,
|
||||||
active: MergedObjectValue,
|
active: MergedObjectValue,
|
||||||
key: string | number | symbol,
|
key: string | number | symbol,
|
||||||
pathSegments: PathSegments,
|
pathSegments: PathSegments,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ResolveContext, ResolveMethod } from '../object-merge'
|
import type { MetaResolveContext } from '../types'
|
||||||
import { MetaResolveContext } from '../types'
|
import { resolveOption } from './index'
|
||||||
import { resolveOption } from '.'
|
|
||||||
|
|
||||||
type MergeResolveContextDeepest = MetaResolveContext & {
|
type MergeResolveContextDeepest = MetaResolveContext & {
|
||||||
depth: number
|
depth: number
|
||||||
@@ -24,9 +23,12 @@ export function setup (context: MergeResolveContextDeepest): void {
|
|||||||
context.depth = depth
|
context.depth = depth
|
||||||
}
|
}
|
||||||
|
|
||||||
export const resolve: ResolveMethod = resolveOption((acc: any, context: ResolveContext) => {
|
export const resolve = resolveOption<number, MergeResolveContextDeepest>((currentValue, context) => {
|
||||||
const { depth } = (context as unknown as MergeResolveContextDeepest)
|
const { depth } = context
|
||||||
if (!acc || depth > acc) {
|
|
||||||
return acc
|
if (!currentValue || depth > currentValue) {
|
||||||
|
return depth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return currentValue
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { ResolveContext, ResolveMethod } from '../object-merge'
|
import type { ResolveContext, ResolveMethod } from '../object-merge'
|
||||||
|
|
||||||
export type ResolveOptionReducer = (accumulator: any, context: ResolveContext) => ResolveMethod
|
export interface ResolveOptionPredicament<T, U> {
|
||||||
|
(currentValue: T | undefined, context: U): T
|
||||||
|
}
|
||||||
|
|
||||||
export const resolveOption: (predicament: ResolveOptionReducer) => ResolveMethod = predicament => (options, contexts) => {
|
export const resolveOption = <T, U = ResolveContext>(predicament: ResolveOptionPredicament<T, U>, initialValue?: T): ResolveMethod<U> => (options, contexts) => {
|
||||||
let resolvedIndex = -1
|
let resolvedIndex = -1
|
||||||
|
|
||||||
contexts.reduce((acc: ResolveContext | undefined, context, index) => {
|
contexts.reduce((acc, context, index) => {
|
||||||
const retval = predicament(acc, context)
|
const retval = predicament(acc, context)
|
||||||
|
|
||||||
if (retval !== acc) {
|
if (retval !== acc) {
|
||||||
@@ -14,7 +16,7 @@ export const resolveOption: (predicament: ResolveOptionReducer) => ResolveMethod
|
|||||||
}
|
}
|
||||||
|
|
||||||
return acc
|
return acc
|
||||||
}, undefined)
|
}, initialValue)
|
||||||
|
|
||||||
if (resolvedIndex > -1) {
|
if (resolvedIndex > -1) {
|
||||||
return options[resolvedIndex]
|
return options[resolvedIndex]
|
||||||
|
|||||||
Reference in New Issue
Block a user