mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-18 03:20: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 ResolveMethod = (
|
||||
export type ResolveMethod<T = ResolveContext> = (
|
||||
options: Array<any>,
|
||||
contexts: Array<ResolveContext>,
|
||||
contexts: Array<T>,
|
||||
active: MergedObjectValue,
|
||||
key: string | number | symbol,
|
||||
pathSegments: PathSegments,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { ResolveContext, ResolveMethod } from '../object-merge'
|
||||
import { MetaResolveContext } from '../types'
|
||||
import { resolveOption } from '.'
|
||||
import type { MetaResolveContext } from '../types'
|
||||
import { resolveOption } from './index'
|
||||
|
||||
type MergeResolveContextDeepest = MetaResolveContext & {
|
||||
depth: number
|
||||
@@ -24,9 +23,12 @@ export function setup (context: MergeResolveContextDeepest): void {
|
||||
context.depth = depth
|
||||
}
|
||||
|
||||
export const resolve: ResolveMethod = resolveOption((acc: any, context: ResolveContext) => {
|
||||
const { depth } = (context as unknown as MergeResolveContextDeepest)
|
||||
if (!acc || depth > acc) {
|
||||
return acc
|
||||
export const resolve = resolveOption<number, MergeResolveContextDeepest>((currentValue, context) => {
|
||||
const { depth } = context
|
||||
|
||||
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
|
||||
|
||||
contexts.reduce((acc: ResolveContext | undefined, context, index) => {
|
||||
contexts.reduce((acc, context, index) => {
|
||||
const retval = predicament(acc, context)
|
||||
|
||||
if (retval !== acc) {
|
||||
@@ -14,7 +16,7 @@ export const resolveOption: (predicament: ResolveOptionReducer) => ResolveMethod
|
||||
}
|
||||
|
||||
return acc
|
||||
}, undefined)
|
||||
}, initialValue)
|
||||
|
||||
if (resolvedIndex > -1) {
|
||||
return options[resolvedIndex]
|
||||
|
||||
Reference in New Issue
Block a user