Index

src/utils/evaluate-condition.ts

evaluateCondition
Default value : <T>(datum: T, condition: SimpleCondition<T>): boolean => { const { field, operator, value, customCompare, } = condition; // If a custom comparison function is provided, use it. if (customCompare) { return customCompare(datum[field], value); } switch (operator) { case '$eq': return datum[field] === value; case '$neq': return datum[field] !== value; case '$gt': return datum[field] > value; case '$gte': return datum[field] >= value; case '$lt': return datum[field] < value; case '$lte': return datum[field] <= value; case '$in': return Array.isArray(value) && value.includes(datum[field]); case '$nin': return Array.isArray(value) && !value.includes(datum[field]); case '$regex': { if (value && typeof value === 'object' && ('$regex' in value)) { const regexCondition = value as RegexCondition; // Cast value to RegexCondition const regex = regexCondition.$regex instanceof RegExp ? regexCondition.$regex : new RegExp(regexCondition.$regex, regexCondition.$options || ''); // Apply options if $regex is a string // Check if the field is a string before testing const fieldValue = datum[field]; return typeof fieldValue === 'string' ? regex.test(fieldValue) : false; } return false; // Invalid regex condition } default: return false; // Unsupported operator } }

Evaluates a condition against a given datum (data object).

This function supports various comparison operators including equality, inequality, greater than, less than, inclusion/exclusion in a set, and regular expression matching.

src/utils/evaluate-node.ts

evaluateNode
Default value : <T extends object>(datum: T, node: ConditionNode<T>): boolean => { // Retrieve or initialize the cache for the current datum, ensuring type safety let datumCache: Map<ConditionNode<T>, boolean> | undefined = memoCache.get(datum); if (!datumCache) { datumCache = new Map<ConditionNode<T>, boolean>(); memoCache.set(datum, datumCache); } // Check if the result is already cached for this condition node if (datumCache.has(node)) { return datumCache.get(node)!; // Use the cached result, safely asserting it exists } let result: boolean; // Evaluate the condition if ('$and' in node) { // AND logic: all conditions must pass result = node.$and.every((subNode) => evaluateNode(datum, subNode)); } else if ('$or' in node) { // OR logic: at least one condition must pass result = node.$or.some((subNode) => evaluateNode(datum, subNode)); } else { // Handle simple condition result = evaluateCondition(datum, node); } // Cache the result for the current node and datum datumCache.set(node, result); return result; }

Evaluates a logical condition node against a given datum (data object) with memoization.

This function supports both AND and OR logical operations:

  • For AND ($and), all conditions must evaluate to true.
  • For OR ($or), at least one condition must evaluate to true. If the node is a simple condition, it will be evaluated directly.

Memoization is applied to avoid redundant computations of the same condition for the same datum.

memoCache
Default value : new WeakMap<object, Map<ConditionNode<any>, boolean>>()

A cache that maps a datum to a Map of condition nodes and their evaluation results. The WeakMap ensures that the cache is memory-safe and automatically cleans up when the datum is no longer in use.

src/utils/get-many.ts

getMany
Default value : <T extends object>(data: T[], query: ConditionNode<T>): T[] => data.filter((datum) => evaluateNode(datum, query))

Filters the dataset to find all items that match the provided condition node.

This function uses evaluateNode to efficiently evaluate each datum against the condition node, leveraging memoization to avoid redundant computations.

src/utils/get-one.ts

getOne
Default value : <T extends object>(data: T[], query: ConditionNode<T>): T | undefined => data.find((datum) => evaluateNode(datum, query))

Finds the first item in the dataset that matches the provided condition node.

This function uses evaluateNode to efficiently evaluate each datum against the condition node, stopping as soon as a match is found to improve performance.

src/utils/is-object.ts

isObject
Default value : (value: unknown): boolean => typeof value === 'object' && value !== null && !Array.isArray(value) && Reflect.ownKeys(value).length > 0

Checks if the provided value is a non-null object with at least one own property (excluding arrays).

src/utils/is-valid-memoz-id.ts

isValidMemozId
Default value : (memozId: string): memozId is MEMOZID => memozId.length >= 36 && MEMOZ_ID_PATTERN.test(memozId)

src/constants/memoz-id.ts

MEMOZ_ID_PATTERN
Default value : /^[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}-[A-Za-z0-9]{9}$/i
MEMOZ_ID_TEMPLATE
Type : string
Default value : 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'

results matching ""

    No results matching ""