import { NodeDefinition, NodeLike, StatelessGraphNode, StatelessNodeDefinition, StatelessNodeType } from '../../../types/graph'; /** * An instance of the [[slice]] node. * See the [[slice]] documentation to find out more. */ export interface SliceNode extends StatelessGraphNode<'slice', SliceNodeProperties> { } /** * A definition of the [[slice]] node. * See the [[slice]] documentation to find out more. */ export interface SliceNodeDefinition extends StatelessNodeDefinition<'slice', SliceNodeProperties> { } export interface SliceNodeProperties { offset: NodeDefinition; length: NodeDefinition; } /** * The implementation of the [[slice]] node. * See the [[slice]] documentation to learn more. */ export declare const SliceNodeType: StatelessNodeType<'slice', SliceNodeProperties>; export declare type RelativeSliceBounds = { offset: NodeDefinition | NodeLike; length: NodeDefinition | NodeLike; }; export declare function isRelativeSliceBounds(value: any): value is RelativeSliceBounds; export declare type InclusiveSliceBounds = { from: NodeDefinition | NodeLike; to: NodeDefinition | NodeLike; }; export declare function isInclusiveSliceBounds(value: any): value is InclusiveSliceBounds; export declare type ExclusiveSliceBounds = { begin: NodeDefinition | NodeLike; end: NodeDefinition | NodeLike; }; export declare function isExclusiveSliceBounds(value: any): value is ExclusiveSliceBounds; /** * Creates a new instance of a [[slice]] node, which is a type of collection transform used when limiting * the number of items returned from a collection. * It lets you skip a number of items from the start and define the number of items to return. * One use case for this node is returning paginated items. * It behaves in a similar way to the JavaScript's `Array.slice()` method. * * The [[slice]] allows for three forms of specifying the range: * - `slice({ offset: number, length: number })` * - `slice({ from: number, to: number })` * - `slice({ begin: number, end: number })` * * * @example **Simple range selection** * ```js * import muster, { entries, key, query, ref, slice, withTransforms } from '@dws/muster'; * * const app = muster({ * numbers: [1, 2, 3, 4, 5], * }); * * const firstTwoNumbers = await app.resolve(query(ref('numbers'), withTransforms([ * slice({ offset: 0, length: 2 }), * ], entries()))); * // firstTwoNumbers = [1, 2] * * const otherTwoNumbers = await app.resolve(query(ref('numbers'), withTransforms([ * slice({ offset: 2, length: 2 }), * ], entries()))); * // otherTwoNumbers = [3, 4] * ``` * This example shows how to take two items out of a collection with the help of a * [[slice]]. * * * @example **Paginated collection** * ```ts * import muster, { * applyTransforms, * entries, * key, * multiply, * query, * ref, * set, * slice, * variable, * } from '@dws/muster'; * * const app = muster({ * pageSize: 2, * pageIndex: variable(0), * pageOffset: multiply(ref('pageIndex'), ref('pageSize')), * numbers: applyTransforms( * [1, 2, 3, 4, 5, 6], * [slice({ offset: ref('pageOffset'), length: ref('pageSize') })] * ), * }); * * app.resolve(query(ref('numbers'), entries())).subscribe((items: any) => { * console.log(items); * }); * * console.log('Changing page index to 1'); * await app.resolve(set('pageIndex', 1)); * * // Console output: * // [1, 2] * // Changing page index to 1 * // [3, 4] * ``` * This example shows how to implement the simple pagination of a local collection. */ export declare function slice(range: RelativeSliceBounds | InclusiveSliceBounds | ExclusiveSliceBounds): SliceNodeDefinition;