Modules@robinpath/collection
collection

@robinpath/collection

0.1.1Node.jsPublic

Array and collection manipulation utilities: filtering, sorting, grouping, aggregation, and set operations

Collection

Array and collection manipulation utilities: filtering, sorting, grouping, aggregation, and set operations

Package: @robinpath/collection | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the collection module when you need to:

  • Extract a single property value from each object in an array -- Use collection.pluck to perform this operation
  • Filter array to items where a property equals a given value -- Use collection.where to perform this operation
  • Filter array to items where a numeric property is greater than a value -- Use collection.whereGt to perform this operation
  • Filter array to items where a numeric property is less than a value -- Use collection.whereLt to perform this operation
  • Filter array to items where a numeric property is greater than or equal to a value -- Use collection.whereGte to perform this operation

Quick Reference

FunctionDescriptionReturns
pluckExtract a single property value from each object in an arrayArray of extracted property values
whereFilter array to items where a property equals a given valueFiltered array of matching objects
whereGtFilter array to items where a numeric property is greater than a valueFiltered array where property > value
whereLtFilter array to items where a numeric property is less than a valueFiltered array where property < value
whereGteFilter array to items where a numeric property is greater than or equal to a valueFiltered array where property >= value
whereLteFilter array to items where a numeric property is less than or equal to a valueFiltered array where property <= value
whereNotFilter array to items where a property does not equal a given valueFiltered array of non-matching objects
sortBySort an array of objects by a property in ascending orderNew array sorted in ascending order by the property
sortByDescSort an array of objects by a property in descending orderNew array sorted in descending order by the property
uniqueRemove duplicate values from an arrayNew array with duplicates removed
flattenFlatten nested arrays by one levelNew flattened array (one level deep)
reverseReverse the order of elements in an arrayNew array with elements in reverse order
chunkSplit an array into chunks of a given sizeArray of arrays, each containing up to size elements
firstGet the first element of an arrayFirst element of the array, or null if empty
lastGet the last element of an arrayLast element of the array, or null if empty
countCount the number of elements in an arrayNumber of elements in the array
sumSum numeric values in an array, optionally by a property nameSum of the numeric values
avgCalculate the average of numeric values in an array, optionally by a property nameAverage of the numeric values
minFind the minimum numeric value in an array, optionally by a property nameMinimum numeric value
maxFind the maximum numeric value in an array, optionally by a property nameMaximum numeric value
groupByGroup array items by a property valueObject with group values as keys and arrays of items as values
compactRemove all falsy values (null, undefined, false, 0, empty string) from an arrayNew array with falsy values removed
zipZip two arrays together into an array of pairsArray of [a, b] pairs from the two arrays
differenceGet elements that are in the first array but not in the secondElements present in array1 but not in array2
intersectionGet elements that exist in both arraysElements present in both arrays
unionCombine two arrays into one with unique elementsCombined array with duplicates removed
takeTake the first N elements from an arrayNew array with the first N elements
skipSkip the first N elements of an arrayNew array with the first N elements removed
containsCheck if an array contains a specific valueTrue if the value exists in the array
indexOfFind the index of a value in an arrayIndex of the value, or -1 if not found

Functions

pluck

Extract a single property value from each object in an array

Module: collection | Returns: array -- Array of extracted property values

collection.pluck $arr "name"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to pluck from
keystringYesProperty name to extract

where

Filter array to items where a property equals a given value

Module: collection | Returns: array -- Filtered array of matching objects

collection.where $arr "age" 25
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to filter
keystringYesProperty name to compare
valueanyYesValue to match against

whereGt

Filter array to items where a numeric property is greater than a value

Module: collection | Returns: array -- Filtered array where property > value

collection.whereGt $arr "age" 25
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to filter
keystringYesProperty name to compare
valuenumberYesThreshold value (exclusive)

whereLt

Filter array to items where a numeric property is less than a value

Module: collection | Returns: array -- Filtered array where property < value

collection.whereLt $arr "age" 25
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to filter
keystringYesProperty name to compare
valuenumberYesThreshold value (exclusive)

whereGte

Filter array to items where a numeric property is greater than or equal to a value

Module: collection | Returns: array -- Filtered array where property >= value

collection.whereGte $arr "age" 25
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to filter
keystringYesProperty name to compare
valuenumberYesThreshold value (inclusive)

whereLte

Filter array to items where a numeric property is less than or equal to a value

Module: collection | Returns: array -- Filtered array where property <= value

collection.whereLte $arr "age" 25
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to filter
keystringYesProperty name to compare
valuenumberYesThreshold value (inclusive)

whereNot

Filter array to items where a property does not equal a given value

Module: collection | Returns: array -- Filtered array of non-matching objects

collection.whereNot $arr "role" "admin"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to filter
keystringYesProperty name to compare
valueanyYesValue to exclude

sortBy

Sort an array of objects by a property in ascending order

Module: collection | Returns: array -- New array sorted in ascending order by the property

collection.sortBy $arr "name"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to sort
keystringYesProperty name to sort by

sortByDesc

Sort an array of objects by a property in descending order

Module: collection | Returns: array -- New array sorted in descending order by the property

collection.sortByDesc $arr "age"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to sort
keystringYesProperty name to sort by

unique

Remove duplicate values from an array

Module: collection | Returns: array -- New array with duplicates removed

collection.unique $arr
ParameterTypeRequiredDescription
arrayarrayYesArray to deduplicate

flatten

Flatten nested arrays by one level

Module: collection | Returns: array -- New flattened array (one level deep)

collection.flatten $arr
ParameterTypeRequiredDescription
arrayarrayYesArray with nested arrays to flatten

reverse

Reverse the order of elements in an array

Module: collection | Returns: array -- New array with elements in reverse order

collection.reverse $arr
ParameterTypeRequiredDescription
arrayarrayYesArray to reverse

chunk

Split an array into chunks of a given size

Module: collection | Returns: array -- Array of arrays, each containing up to size elements

collection.chunk $arr 3
ParameterTypeRequiredDescription
arrayarrayYesArray to split into chunks
sizenumberYesNumber of elements per chunk

first

Get the first element of an array

Module: collection | Returns: any -- First element of the array, or null if empty

collection.first $arr
ParameterTypeRequiredDescription
arrayarrayYesArray to get first element from

last

Get the last element of an array

Module: collection | Returns: any -- Last element of the array, or null if empty

collection.last $arr
ParameterTypeRequiredDescription
arrayarrayYesArray to get last element from

count

Count the number of elements in an array

Module: collection | Returns: number -- Number of elements in the array

collection.count $arr
ParameterTypeRequiredDescription
arrayarrayYesArray to count

sum

Sum numeric values in an array, optionally by a property name

Module: collection | Returns: number -- Sum of the numeric values

collection.sum $arr "price"
ParameterTypeRequiredDescription
arrayarrayYesArray of numbers or objects
keystringNoProperty name to sum (omit to sum array values directly)

avg

Calculate the average of numeric values in an array, optionally by a property name

Module: collection | Returns: number -- Average of the numeric values

collection.avg $arr "price"
ParameterTypeRequiredDescription
arrayarrayYesArray of numbers or objects
keystringNoProperty name to average (omit to average array values directly)

min

Find the minimum numeric value in an array, optionally by a property name

Module: collection | Returns: number -- Minimum numeric value

collection.min $arr "price"
ParameterTypeRequiredDescription
arrayarrayYesArray of numbers or objects
keystringNoProperty name to find minimum of (omit for array values directly)

max

Find the maximum numeric value in an array, optionally by a property name

Module: collection | Returns: number -- Maximum numeric value

collection.max $arr "price"
ParameterTypeRequiredDescription
arrayarrayYesArray of numbers or objects
keystringNoProperty name to find maximum of (omit for array values directly)

groupBy

Group array items by a property value

Module: collection | Returns: object -- Object with group values as keys and arrays of items as values

collection.groupBy $arr "category"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects to group
keystringYesProperty name to group by

compact

Remove all falsy values (null, undefined, false, 0, empty string) from an array

Module: collection | Returns: array -- New array with falsy values removed

collection.compact $arr
ParameterTypeRequiredDescription
arrayarrayYesArray to compact

zip

Zip two arrays together into an array of pairs

Module: collection | Returns: array -- Array of [a, b] pairs from the two arrays

collection.zip $a $b
ParameterTypeRequiredDescription
array1arrayYesFirst array
array2arrayYesSecond array

difference

Get elements that are in the first array but not in the second

Module: collection | Returns: array -- Elements present in array1 but not in array2

collection.difference $a $b
ParameterTypeRequiredDescription
array1arrayYesSource array
array2arrayYesArray of elements to exclude

intersection

Get elements that exist in both arrays

Module: collection | Returns: array -- Elements present in both arrays

collection.intersection $a $b
ParameterTypeRequiredDescription
array1arrayYesFirst array
array2arrayYesSecond array

union

Combine two arrays into one with unique elements

Module: collection | Returns: array -- Combined array with duplicates removed

collection.union $a $b
ParameterTypeRequiredDescription
array1arrayYesFirst array
array2arrayYesSecond array

take

Take the first N elements from an array

Module: collection | Returns: array -- New array with the first N elements

collection.take $arr 5
ParameterTypeRequiredDescription
arrayarrayYesSource array
nnumberYesNumber of elements to take

skip

Skip the first N elements of an array

Module: collection | Returns: array -- New array with the first N elements removed

collection.skip $arr 5
ParameterTypeRequiredDescription
arrayarrayYesSource array
nnumberYesNumber of elements to skip

contains

Check if an array contains a specific value

Module: collection | Returns: boolean -- True if the value exists in the array

collection.contains $arr "value"
ParameterTypeRequiredDescription
arrayarrayYesArray to search in
valueanyYesValue to search for

indexOf

Find the index of a value in an array

Module: collection | Returns: number -- Index of the value, or -1 if not found

collection.indexOf $arr "value"
ParameterTypeRequiredDescription
arrayarrayYesArray to search in
valueanyYesValue to find

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Pluck and validate result"
do
  set $result as collection.pluck $arr "name"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Collection workflow

Chain multiple collection operations together.

@desc "Pluck, where, and more"
do
  set $r_pluck as collection.pluck $arr "name"
  set $r_where as collection.where $arr "age" 25
  set $r_whereGt as collection.whereGt $arr "age" 25
  print "All operations complete"
enddo

2. Safe pluck with validation

Check results before proceeding.

@desc "Pluck and validate result"
do
  set $result as collection.pluck $arr "name"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/collection

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size10.6 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities