Table Extensions

The Table Extensions library provides JavaScript-style array and object manipulation methods for Lua tables, making it easier to work with collections of data in a functional programming style.

Overview

This library extends Lua's built-in table functionality with methods inspired by JavaScript's Array methods. It automatically detects whether a table is array-like or object-like and handles operations accordingly.

Functions

table.filter

Creates a new table containing only elements that pass the predicate test.

Parameters:

  • t (table): The table to filter

  • predicate (function): Function that tests each element predicate(value, key)

Returns: New filtered table

Behavior:

  • For arrays: Returns array with filtered values

  • For objects: Returns object with filtered key-value pairs

Examples:

-- Filter array
local numbers = {1, 2, 3, 4, 5, 6}
local evenNumbers = table.filter(numbers, function(value)
    return value % 2 == 0
end)
-- Result: {2, 4, 6}

-- Filter object
local users = {
    john = {age = 25, active = true},
    jane = {age = 30, active = false},
    bob = {age = 35, active = true}
}
local activeUsers = table.filter(users, function(user)
    return user.active
end)
-- Result: {john = {age = 25, active = true}, bob = {age = 35, active = true}}

-- Using both value and key in predicate
local items = {"apple", "banana", "cherry"}
local firstTwo = table.filter(items, function(value, index)
    return index <= 2
end)
-- Result: {"apple", "banana"}

table.map

Creates a new array by transforming each element using the provided function.

Parameters:

  • t (table): The table to map over

  • predicate (function): Transform function predicate(value, index, table)

Returns: New array with transformed values

Note: Always returns an array regardless of input table type

Examples:

table.find

Finds the first element that matches the predicate condition.

Parameters:

  • t (table): The table to search

  • predicate (function): Test function predicate(value, index)

Returns: First matching value, or nil if not found

Examples:

table.findIndex

Finds the index of the first element that matches the condition.

Parameters:

  • t (table): The table to search

  • callbackFn (function): Test function callbackFn(element, index, table)

Returns: Index of first matching element, or -1 if not found

Examples:

table.forEach

Executes a function for each element in the table (arrays only).

Parameters:

  • t (table): The array to iterate over

  • callbackFn (function): Function to execute callbackFn(value, index)

Returns: Nothing (void)

Note: Uses ipairs, so only works with array-like tables

Examples:

table.merge

Merges two arrays by appending t2 to t1.

Parameters:

  • t1 (table): Target array (modified in place)

  • t2 (table): Source array to append

Returns: The modified t1 table

Note: This modifies the original t1 table

Examples:

table.deepClone

Creates a deep copy of a table, recursively cloning nested tables.

Parameters:

  • tbl (table): The table to clone

Returns: Deep copy of the table

Note: Uses table.clone internally and recursively clones nested tables

Examples:

Usage Patterns

Functional Data Processing

Data Validation and Processing

Working with Game Data

Safe Table Operations

Performance Notes

  1. Array Detection: The library uses table.type(t) == 'array' to determine if a table should be treated as an array

  2. Memory Usage: deepClone creates completely new tables, which uses more memory but ensures data isolation

  3. Iteration: forEach uses ipairs for better performance with arrays

  4. Functional Style: These methods create new tables rather than modifying existing ones (except merge)

Integration with Framework

These table extensions integrate seamlessly with other framework components:

Last updated