km-traversal
Version:
This utility provides powerful object traversal capabilities with pattern matching and transformation features.
163 lines (162 loc) • 6.06 kB
TypeScript
/**
* Represents any nested data structure that can be traversed
* - Objects: { key: value, nested: { ... } }
* - Arrays: [item1, item2, ...]
* - Mixed: { items: [ { id: 1 }, { id: 2 } ] }
*/
export type IEntryData = object | any[];
/**
* Defines a single step in the traversal pattern
* - Determines how to navigate through data at each level
* - Used to build complex traversal paths
*/
export type PatternStep = {
type: 'property';
name: string;
} | {
type: 'single-star';
} | {
type: 'double-star';
depth?: number;
} | {
type: 'single-key';
key: string;
} | {
type: 'multi-key';
keys: string[];
} | {
type: 'object-cond';
conditions: Record<string, any>;
} | {
type: 'array-cond';
conditions: Record<string, any>[];
};
/**
* Defines a custom condition function for filtering during traversal
* @property name - Unique identifier for the condition (e.g., 'startsWith')
* @property action - Function that evaluates the condition
*/
export interface IJsonPatternCondition<NAME extends string = string> {
name: NAME;
action: (key: string | number, value: any, target: any, conditionValue: any) => boolean;
}
type IPatternShortcust = {
singleStar: boolean;
doubleStar: boolean;
braketScope: boolean;
};
/**
* Configuration options for the customEach function
* @property injectedConditions - Array of custom condition functions
*/
type ICustomEachOptions<CONDITIONS extends CONDITION[], CONDITION extends IJsonPatternCondition<NAME>, NAME extends string> = {
injectedConditions: [...CONDITIONS];
shortcuts?: IPatternShortcust;
};
/**
* Parameters passed to traversal callbacks
* @property key - Current key/index in parent
* @property value - Current value
* @property objectPath - Full path to current node
* @property parent - Reference to parent object/array
* @property setKey - Function to rename current key (object properties only)
* @property setValue - Function to modify current value
*/
export interface CallbackParams {
key: string | number;
value: any;
objectPath: (string | number)[];
parent: any;
setKey: (newKey: string) => void;
setValue: (newValue: any) => void;
remove: () => void;
removeNears: () => void;
}
/**
* Callback function type for traversal operations
*/
export type Callback = (params: CallbackParams) => void;
/**
* Represents a node during traversal
* @property node - Current data node
* @property parent - Parent of current node
* @property key - Key/index in parent
* @property path - Accumulated path to node
*/
export interface TraversalNode {
node: any;
parent: any;
key: string | number | null;
path: (string | number)[];
}
/**
* Specialized node for double-star traversal with depth tracking
*/
export interface DoubleStarNode extends TraversalNode {
depth: number;
}
declare const _default: {
traverseIn: <ENTRY_DATA extends IEntryData, OPTIONS extends ICustomEachOptions<[...CONDITIONS], CONDITION, NAME>, CONDITIONS extends CONDITION[], CONDITION extends IJsonPatternCondition<NAME>, NAME extends string>(data: ENTRY_DATA, options: OPTIONS, patterns: (string | string[] | ((o: {
setCondName: (name: OPTIONS["injectedConditions"][number]["name"]) => string;
}) => string))[], callbacks: Callback[]) => void;
adapter: () => {
register: <CONDITIONS_1 extends CONDITION_1[], CONDITION_1 extends IJsonPatternCondition<NAME_1>, NAME_1 extends string>(conditions: CONDITIONS_1) => {
traverseIn: <ENTRY_DATA_1 extends IEntryData>(data: ENTRY_DATA_1, patterns: (string | ((o: {
setCondName: (name: CONDITIONS_1[number]["name"]) => string;
}) => string))[], callbacks: Callback[]) => void;
};
};
defaultConditions: [{
name: "startsWith";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "endsWith";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "includes";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "matches";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "greaterThan";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: ">";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "lessThan";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "<";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "between";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "equalWith";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "notEqual";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "isString";
action: (_: string | number, __: any, target: any) => boolean;
}, {
name: "isNumber";
action: (_: string | number, __: any, target: any) => boolean;
}, {
name: "isArray";
action: (_: string | number, __: any, target: any) => boolean;
}, {
name: "isObject";
action: (_: string | number, __: any, target: any) => boolean;
}, {
name: "arrayIncludes";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}, {
name: "length";
action: (_: string | number, __: any, target: any, conditionValue: any) => boolean;
}];
};
export default _default;