> For the complete documentation index, see [llms.txt](https://hrscripts.gitbook.io/hrscripts-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hrscripts.gitbook.io/hrscripts-documentation/resources/hrlib/modules/table/shared.md).

# Shared

## table.focusedArray

Function to filter indexes in an array of tables with common value(s)

```lua
function HRLib.table.focusedArray(
    array: table<string, any>[],
    focus: table<string, any>,
    cb: fun(i: integer, curr: any)?
)
```

Parameters description

* `array` - the array to filter the indexes from
* `focus` - the table that includes the filter values
* `cb` - the function that is being triggered when a match is found

#### Example

```lua
local array = {
    {
        ownerName = 'player_1',
        cashBalance = 2500
    },
    {
        ownerName = 'player_2',
        cashBalance = 6000
    },
    {
        ownerName = 'player_1',
        cashBalance = 1050
    }
}

HRLib.table.focusedArray(array, { ownerName = 'player_1' }, function(_, curr)
    print(curr.cashBalance)
end) -- the output is two printed values, 2500 and 1050
```

## table.focusedHash

Function to filter keys in a hash table of arrays with common values in the arrays

```lua
function HRLib.table.focusedHash(
    hash: table<string, table<string, any>[]>,
    focus: table|any,
    cb: fun(key: string, value: string, arrayInfo: { i: integer, curr: any })
)
```

Parameters description

* `hash` - the hash table to filter the keys from
* `focus` - the table that includes the filter values or any value to find match with the values from the array
* `cb` - the function that is being triggered when a match is found

#### Example 1

```lua
local hash = {
    player_1 = {
        {
            accountName = 'bank',
            balance = 2500
        },
        {
            accountName = 'cash',
            balance = 1000
        }
    },
    player_2 = {
        {
            accountName = 'bank',
            balance = 7000
        },
        {
            accountName = 'cash',
            balance = 3000
        }
    }
}

HRLib.table.focusedHash(hash, { accountName = 'bank' }, function(k, v, arrayInfo)
    print(k, arrayInfo.curr.balance)
end)
--[[ Ouput is two returned values:
    player_1    2500
    player_2    7000
]]
```

#### Example 2

```lua
local hash = {
    player_1 = {
        'Frankie Bird',
        'Police Officer'
    },
    player_2 = {
        'Santiago Johns',
        'Paramedic'
    },
    player_3 = {
        'Terrance McMillan',
        'Police Officer'
    }
}

HRLib.table.focusedHash(hash, 'Police Officer', function(k, v, arrayInfo)
    print(arrayInfo.curr)
end)
--[[ Ouput is two returned values:
    Frankie Bird
    Terrance McMillan
]]
```

## table.getHashLength

Function to get the number of keys with values in a hash table (if in any scenario you're gonna need it)

```lua
function HRLib.table.getHashLength(hash: table): integer
```

Parameters description

* `hash` - the hash table to get the length of

## table.find

Function to values in any table and different kinds of value

```lua
function HRLib.table.find(
    tbl: any[]|table,
    value: any|any[]|table,
    returnIndex: boolean?,
    isValueKey: boolean?,
    returnUnderIndex: boolean?
):
boolean isFound,
integer|integer[]? index,
string|string[]? underIndex
```

Parameters description

* `tbl` - the table to search the value in
* `value` - the value to search a match with
* `returnIndex` - sets whether or not the index should be returned
* `isValueKey` - sets whether or not the function should be focused on finding a key match instead of value match
* `returnUnderIndex` - sets whether or not the underIndex should be returned if there is underIndex\
  (not tested, not completely done logic)

Return description

* `isFound` - contains the status (is a match found or not)
* `index` - contains the index(es) of the given table where the match is found\
  (only available if `returnIndex` is true)
* `underIndex` - contains a string of the underIndex(es) (only available if returnUnderIndex is true)\
  (not tested, don't recommend to use before fixing its logic and test it)

#### Example 1

```lua
local playerVehicles = {
    '888DD221',
    '000124FD',
    '32551AQK'
}

local found = HRLib.table.find(playerVehicles, '888DD221')
if found then
    print('The player owns the vehicle')
end
-- The output is:
-- "The player owns the vehicle" (because the value '888DD221' is found in the table)
```

#### Example 2

```lua
local playerVehicles = {
    futo = {
        plate = '888DD221'
    },
    asbo = {
        plate = '000124FD'
    }
}

local found = HRLib.table.find(playerVehicles, 'futo', nil, true)
if found then
    print('The player owns a vehicle with model futo')
end
-- It will print it because a value with key 'futo' is found in the table
-- But for that purpose we use the parameter isValueKey to focus the function
-- on seaching for a key match with the given value
```

#### Example 3

```lua
local playerVehicles = {
    ['888DD221'] = {
        model = 'futo'
    },
    ['000124FD'] = {
        model = 'asbo'
    },
    ['32551AQK'] = {
        model = 'futo'
    }
}

local found, index = HRLib.table.find(playerVehicles, { model = 'futo' }, true)
if found then
    if type(index) == 'table' then
        print('The player has more than one vehicle with model futo in its garage')
    else
        print('The player has only one vehicle with model futo in its garage')
    end
end
-- It will print "The player has more than one vehicle with model futo in its garage"
-- because more than one value in the table with underTable's value model is found as futo
```

## table.deepclone

Function to make a deepclone of a table so its changes won't change the main one

```lua
function HRLib.table.deepclone(tbl: table, dontCopyMetatable: boolean?): table
```

Parameters description

* `tbl` - the table to make the deepclone of
* `dontCopyMetatable` - sets whether or not if a metatable exists of the given table, it should be copied

## cloneStateBag

Function to clone a state bag so you can't lose its value even after it's removed

```lua
function HRLib.table.closeStateBag(bagName: string): any[]|false
```

Parameters description

* `bagName` - the state bag name to clone

## table.compare

Function to compare if all values of two tables are same (like tbl1 == tb2 it works with deepcloned tables, because otherwise they won't match because their table identifiers are different)

```lua
function HRLib.table.compare(
    tbl1: table,
    tbl2: table,
    noKeyCompare: boolean?
): boolean
```

Parameters description

* `tbl1` - the first table for the match
* `tbl2` - the second table for the match
* `noKeyCompare` - sets whether or not the function should compare and with keys or not\
  (true to compare tables only with their values, false to compare keys too)

## table.getKeys

Function to get a table's keys in an array of strings or multiple values

```lua
function HRLib.table.getKeys(tbl: table, isArray: boolean?): ...|string[]?
```

Parameters description

* `tbl` - the table to get the keys of
* `isArray` - sets whether or not the return value must be in array format or not

## table.toString

Function to convert lua tables into visible strings that can be loaded via load function,\
(helpful for .lua metadata files)

```lua
function HRLib.table.toString(
    tbl: table,
    indent: boolean?,
    rowsSpace: integer?
): string?
```

Parameters description

* `tbl` - the target table to convert to string
* `indent` - sets whether or not the string should include new rows, tabs and so forth just like\
  the lua tables usually look when they're being created or the string should include the table\
  on one row.
* `rowsSpace` - the number of new rows between values declaration (by default it's `1`)
