import { isNumber, toNumber } from "@3fv/guard" /** * Copyright (C) 2019-present, Rimeto, LLC. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ /** * A generic type that cannot be `undefined`. */ export type Defined = Exclude /** * `PropChainObjectWrapper` gives TypeScript visibility into the properties of * an `PropChainType` object at compile-time. */ export type PropChainObjectWrapper = { [K in keyof T]-?: PropChainType } /** * Data accessor interface to dereference the value of the `TSOCType`. */ export interface PropChainDataAccessor { /** * Data accessor without a default value. If no data exists, * `undefined` is returned. */ >( callback: Callback ): PropChainOnFinish } /** * `PropChainArrayWrapper` gives TypeScript visibility into the `PropChainType` values of an array * without exposing Array methods (it is problematic to attempt to invoke methods during * the course of an optional chain traversal). */ export interface PropChainArrayWrapper< S, T extends PropChainCallback > { length: PropChainType [K: number]: PropChainType } /** * Data accessor interface to dereference the value of an `any` type. * @extends PropChainDataAccessor */ export interface PropChainAny extends PropChainDataAccessor { [K: string]: PropChainAny // Enable deep traversal of arbitrary props } /** * `PropChainDataWrapper` selects between `PropChainArrayWrapper`, `PropChainObjectWrapper`, and `PropChainDataAccessor` * to wrap Arrays, Objects and all other types respectively. */ export type PropChainDataWrapper = 0 extends 1 & T // Is T any? (https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360) ? PropChainAny & PropChainDataAccessor : T extends any[] // Is T array-like? ? PropChainArrayWrapper : T extends object // Is T object-like? ? PropChainObjectWrapper : PropChainDataAccessor /** * An object that supports optional chaining */ export type PropChainType = PropChainDataAccessor & PropChainDataWrapper> export type PropChainCallback = ( getter: (state: S) => T, keyPath: Array ) => any /** * Callback returned by prop-chain */ export type PropChainOnFinish< S, T, Callback extends PropChainCallback > = ReturnType extends (...args: infer P) => infer R ? (...args: P) => R : never export function continuePropertyChain< S, T, Callback, DataAccessor >( state: S, data: T, keyPath: Array = [] ): PropChainType { keyPath = keyPath || [] // noinspection DuplicatedCode return new Proxy( (overrideCallback: PropChainCallback) => { // TRACK FIRST PROP ACCESS const firstGet = keyPath.map(() => true) // CHECK IF KEY SHOULD BE NUMBER function resolveKey(value, key, index) { if (firstGet[index]) { if (Array.isArray(value)) { const keyNum = toNumber(key) if (isNumber(keyNum)) { key = keyPath[index] = keyNum } } firstGet[index] = false } return key } const getter = (state: S) => keyPath.reduce((value, key, index) => { return value[resolveKey(value, key, index)] }, state) return overrideCallback(getter, keyPath) }, { get: (target, key) => { return continuePropertyChain(state, undefined, [...keyPath, key as any]) } } ) as PropChainType } /** * Start a new prop chain of the type specified * * @param state */ export function propertyChain(state: S = undefined): PropChainType { return continuePropertyChain< S, S, PropChainCallback, PropChainDataAccessor >(state, state) }