smart-path-resolver
Version:
A lightweight and fast JavaScript/TypeScript library for resolving object paths with support for arrays, Maps, Sets, and function calls.
122 lines (118 loc) • 3.73 kB
TypeScript
/**
* Token représentant un segment du chemin analysé
*/
interface Token {
segment: string;
type: "property" | "array" | "mapOrSet" | "function";
arrayIndex?: number;
mapOrSetKey?: string;
functionArgs?: any[];
}
/**
* Options de configuration pour PathResolver
*/
interface PathResolverOptions {
/** Active le cache pour les chemins tokenisés */
useCache?: boolean;
/** Taille maximum du cache (default: 1000) */
maxCacheSize?: number;
}
/**
* Classe principale pour la résolution de chemins d'accès
* avec optimisations de performance
*/
declare class PathResolver {
private context;
private readonly options;
private tokenCache;
private static readonly DEFAULT_OPTIONS;
constructor(context: any, options?: PathResolverOptions);
/**
* Convertit un chemin en tokens avec support de cache
*/
private tokenise;
/**
* Résout un chemin d'accès de manière asynchrone
* @param path - string
* @example
* ```ts
* const path = "path.to.property"
* const path = "path.to.array[0]"
* const path = "path.to.mapOrset[key]" // for Map , resolver return a provide key value but for Set he check if the provid key exist on Set
* const path = "path.to.function(77)"
* ```
*/
resolve(path: string): Promise<any>;
/**
* Résout un chemin d'accès de manière synchrone
* @param {string}
* @example
* ```ts
* const path = "path.to.property"
* const path = "path.to.array[0]"
* const path = "path.to.mapOrset[key]" // for Map , resolver return a provide key value but for Set he check if the provid key exist on Set
* const path = "path.to.function(77)"
* ```
* @throws {Error} Si le chemin contient des fonctions asynchrones ou si le chemin est invalide ou la valeur de retour est {undefined}
*/
resolveSync(path: string): any;
/**
* Résolution récursive interne avec optimisations
*/
private _resolve;
/**
* Parse un segment de type tableau ou Map/Set
*/
private parseArrayOrMapAndSet;
/**
* Parse un segment de type fonction
*/
private parseFunction;
/**
* Vide le cache des tokens
*/
clearCache(): void;
}
/**
* Classe utilitaire avec méthodes optimisées pour la vérification des types
*/
declare class Utils {
static readonly FUNCTION_REGEX: RegExp;
static readonly ARRAY_REGEX: RegExp;
static readonly IDENTIFIER_REGEX: RegExp;
/**
* Vérifie si une fonction est asynchrone
* @param fn : Function a verifier
* @returns boolean
*/
static isAsyncFunction(fn: Function): boolean;
/**
* Vérifie si un segment représente un appel de fonction
* @param segment - segment a verifier String
* @returns boolean
*/
static isFunctionSegment(segment: string): boolean;
/**
* Vérifie si un segment représente un accès à un tableau
* @param segment - segment a verifier String
* @returns boolean
*/
static isArraySegment(segment: string): boolean;
/**
* Verifie si un segment est un identifier (lvalue)
* @param segment - segment a verifier String
* @returns boolean
*/
static isIdentifierSegment(segment: string): boolean;
/**
* Vérifications de type optimisées utilisant instanceof
* @param value : any
* @returns boolean
*/
static isObject(value: any): boolean;
static isFunction(value: any): boolean;
static isArray(value: any): boolean;
static isMap(value: any): boolean;
static isSet(value: any): boolean;
}
export { PathResolver, type PathResolverOptions, type Token, Utils };