/** * Array Expression Operators */ import { ReturnArrayExpression } from '../array'; import { NumberLikeExpression } from '../number'; /** Returns the element at the specified array index. */ export const $arrayElemAt = "$arrayElemAt"; /** Converts an array of key value pairs to a document. */ export const $arrayToObject = "$arrayToObject"; /** Concatenates arrays to return the concatenated array. */ export const $concatArrays = "$concatArrays"; /** Selects a subset of the array to return an array with only the elements that match the filter condition. */ export const $filter = "$filter"; /** Returns the first array element. Distinct from $first accumulator. */ export const $first = "$first"; /** Returns a boolean indicating whether a specified value is in an array. */ export const $in = "$in"; /** Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. If the substring is not found, returns -1. */ export const $indexOfArray = "$indexOfArray"; /** Determines if the operand is an array. Returns a boolean. */ export const $isArray = "$isArray"; /** Returns the last array element. Distinct from $last accumulator. */ export const $last = "$last"; /** Applies a subexpression to each element of an array and returns the array of resulting values in order. Accepts named parameters. */ export const $map = "$map"; /** Converts a document to an array of documents representing key-value pairs. */ export const $objectToArray = "$objectToArray"; /** Outputs an array containing a sequence of integers according to user-defined inputs. */ export const $range = "$range"; /** Applies an expression to each element in an array and combines them into a single value. */ export const $reduce = "$reduce"; /** Returns an array with the elements in reverse order. */ export const $reverseArray = "$reverseArray"; /** Returns the number of elements in the array. Accepts a single expression as argument. */ export const $size = "$size"; /** Returns a subset of an array. */ export const $slice = "$slice"; /** Merge two arrays together. */ export const $zip = "$zip"; const ARRAY_OPERATORS = [ $arrayElemAt, $arrayToObject, $concatArrays, $filter, $first, $in, $indexOfArray, $isArray, $last, $map, $objectToArray, $range, $reduce, $reverseArray, $size, $slice, $zip, ] as const; export type ArrayExpressionOperator = typeof ARRAY_OPERATORS[number]; export const isArrayExpressionOperator = ( val: any ): val is ArrayExpressionOperator => val in ARRAY_OPERATORS; type ArrayLike = A[] | ReturnArrayExpression type NumberLike = number | NumberLikeExpression; type ArrayExpressionArgument = any; type UnaryArray = ArrayLike; type BinaryArray = [ArrayLike, ArrayLike]; type RangeArray = [ArrayLike, ...ArrayLike[]]; /** Returns the element at the specified array index. */ export type $arrayElemAtExpression = { [$arrayElemAt]: RangeArray; }; /** Converts an array of key value pairs to a document. */ export type $arrayToObjectExpression = { [$arrayToObject]: ArrayLike; }; /** Concatenates arrays to return the concatenated array. */ export type $concatArraysExpression = { [$concatArrays]: RangeArray; }; /** Selects a subset of the array to return an array with only the elements that match the filter condition. */ export type $filterExpression = { [$filter]: RangeArray; }; /** Returns the first array element. Distinct from $first accumulator. */ export type $firstExpression = { [$first]: RangeArray; }; /** Returns a boolean indicating whether a specified value is in an array. */ export type $inExpression = { [$in]: BinaryArray; }; /** Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. If the substring is not found, returns -1. */ export type $indexOfArrayExpression = { [$indexOfArray]: RangeArray; }; /** Determines if the operand is an array. Returns a boolean. */ export type $isArrayExpression = { [$isArray]: RangeArray; }; /** Returns the last array element. Distinct from $last accumulator. */ export type $lastExpression = { [$last]: RangeArray; }; /** Applies a subexpression to each element of an array and returns the array of resulting values in order. Accepts named parameters. */ export type $mapExpression = { [$map]: RangeArray; }; /** Converts a document to an array of documents representing key-value pairs. */ export type $objectToArrayExpression = { [$objectToArray]: A; }; /** Outputs an array containing a sequence of integers according to user-defined inputs. */ export type $rangeExpression = { [$range]: [ NumberLike, NumberLike, NumberLike ] | [ NumberLike, NumberLike ]; }; /** Applies an expression to each element in an array and combines them into a single value. */ export type $reduceExpression = { [$reduce]: { input: number[] | $rangeExpression; initialValue: A; in: A; }; }; /** Returns an array with the elements in reverse order. */ export type $reverseArrayExpression = { [$reverseArray]: RangeArray; }; /** Returns the number of elements in the array. Accepts a single expression as argument. */ export type $sizeExpression = { [$size]: RangeArray; }; /** Returns a subset of an array. */ export type $sliceExpression = { [$slice]: RangeArray; }; /** Merge two arrays together. */ export type $zipExpression = { [$zip]: RangeArray; }; export type ArrayExpression = | $arrayElemAtExpression | $arrayToObjectExpression | $concatArraysExpression | $filterExpression | $firstExpression | $inExpression | $indexOfArrayExpression | $isArrayExpression | $lastExpression | $mapExpression | $objectToArrayExpression | $rangeExpression | $reduceExpression | $reverseArrayExpression | $sizeExpression | $sliceExpression | $zipExpression;