collectype

collectype v0.11.0


collectype / BaseFunctions / BaseFunctions

Class: BaseFunctions<T>

Defined in: BaseFunctions.ts:23

Core collection manipulation class providing chainable operations. Serves as the foundation for filtering, sorting, and transforming collections of any type.

Extended by

Type Parameters

T

T

The type of items in the collection.

Implements

Constructors

Constructor

new BaseFunctions<T>(items): BaseFunctions<T>

Defined in: BaseFunctions.ts:33

Creates an instance of BaseFunctions.

Parameters

items

T[]

Array of items to operate on.

Returns

BaseFunctions<T>

Properties

_items

protected _items: T[]

Defined in: BaseFunctions.ts:24


_pageState?

protected optional _pageState: PageState

Defined in: BaseFunctions.ts:25


_sortState?

protected optional _sortState: SortState

Defined in: BaseFunctions.ts:26


_stepManager

protected _stepManager: StepManager

Defined in: BaseFunctions.ts:27

Accessors

items

Get Signature

get items(): T[]

Defined in: BaseFunctions.ts:41

Gets the array of items.

Returns

T[]

The current array of items.

Implementation of

Collectable.items


count

Get Signature

get count(): number

Defined in: BaseFunctions.ts:49

Gets the number of items.

Returns

number

The count of items.

Implementation of

Collectable.count


info

Get Signature

get info(): CollectionInfo

Defined in: BaseFunctions.ts:57

Gets information about the current state of the collection.

Returns

CollectionInfo

Information about pagination, sorting, filters, and operations.

Methods

begin()

begin(name): this

Defined in: BaseFunctions.ts:70

Starts a named step with stack management for nested calls. Automatically handles nested step calls without breaking the chain.

Parameters

name

string

Returns

this


end()

end(): this

Defined in: BaseFunctions.ts:82

Ends the current step and pops from stack.

Returns

this


where()

where(fn): this

Defined in: BaseFunctions.ts:97

Filters items using the provided predicate function. This is the preferred, concise method for filtering collections.

Parameters

fn

PredicateFn<T>

The predicate function to filter items.

Returns

this

The instance for chaining.

Example

collection.fn.where(p => p.is_legendary)

all()

all(): this

Defined in: BaseFunctions.ts:110

Returns the current instance (all items).

Returns

this

The instance for chaining.


sort()

sort<K>(field, dir?, type?): this

Defined in: BaseFunctions.ts:124

Sorts the items by a specified field with automatic type detection. Automatically infers the sort type from the field value if not explicitly provided.

Type Parameters

K

K extends string | number | symbol

The key of the field to sort by.

Parameters

field

K

The field name to sort on.

dir?

SortDir = SortDirEnum.ASC

Sort direction (‘asc’ or ‘desc’). Default is ‘asc’.

type?

SortType

Sort type (‘string’, ‘number’, ‘boolean’, ‘date’). Auto-detected if omitted.

Returns

this

The instance for chaining.

Throws

Error if an unsupported sort type is provided.


page()

page(current, perPage?): this

Defined in: BaseFunctions.ts:171

Paginates the items by returning a specific page of results.

Parameters

current

number

The current page number (1-based).

perPage?

number = 20

The number of items per page. Default is 20.

Returns

this

The instance for chaining.

Throws

Error if current is less than 1 or perPage is less than 1.


pipe()

pipe(expression): this

Defined in: BaseFunctions.ts:211

Executes a sequence of chainable operations from a pipe expression string. Parses and executes multiple method calls in sequence, ensuring each returns ‘this’ for chaining.

Parameters

expression

string

The pipe expression string (e.g., “method1(arg) method2(arg)”).

Returns

this

The instance for chaining.

Throws

Error if a reserved method is used, unknown method is called, or method breaks the chain.