Skip to content

Commit 15a5a05

Browse files
Fixes table_merge function (#544)
* Adding table_find and safe_insert_and_replace * Updating version number and changelog --------- Co-authored-by: KiwiHawk <59639+KiwiHawk@users.noreply.github.com>
1 parent 3db39e5 commit 15a5a05

3 files changed

Lines changed: 58 additions & 21 deletions

File tree

boblibrary/changelog.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
---------------------------------------------------------------------------------------------------
2+
Version: 2.0.4
3+
Date: ???
4+
Bugfixes:
5+
- Changed table_merge function to check values not just keys #544
6+
---------------------------------------------------------------------------------------------------
27
Version: 2.0.3
38
Date: 15. 01. 2026
49
Changes:

boblibrary/functions.lua

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,63 @@
1-
-- Merges table2's contents into table1.
1+
--- Searches table for given value
2+
--- Returns index if found, nil otherwise
3+
local function table_find(table1, value)
4+
for index, value_1 in pairs(table1) do
5+
if type(value) == "table" then
6+
local is_table_same = true
7+
8+
-- Iterate inner table
9+
for _, inner_value in pairs(value) do
10+
if not table_find(value_1, inner_value) then
11+
is_table_same = false
12+
end
13+
end
14+
15+
-- Table is same as value table, thus the value is found
16+
if is_table_same then
17+
return index
18+
end
19+
else
20+
if value_1 == value then
21+
return index
22+
end
23+
end
24+
end
25+
26+
return nil;
27+
end
28+
29+
-- Inserts the new item into the table only if it doesn't already exist. (Index optional.)
30+
function bobmods.lib.safe_insert(array, new_item, index)
31+
if not table_find(array, new_item) then
32+
if index then
33+
table.insert(array, index, new_item)
34+
else
35+
table.insert(array, new_item)
36+
end
37+
end
38+
end
39+
40+
--- Safely inserts new items when index is numerical, replaces non-numeric matching indices
41+
local function safe_insert_and_replace(array, new_item, index)
42+
if type(index) == "number" then
43+
bobmods.lib.safe_insert(array, new_item, index)
44+
else
45+
array[index] = new_item
46+
end
47+
end
48+
49+
--- Merges table2's contents into table1.
50+
--- This will append non-existing values and replace values with matching non-numeric key
251
function bobmods.lib.table_merge(table1, table2)
352
for index, value in pairs(table2) do
453
if type(value) == "table" then
554
if type(table1[index]) == "table" then
655
bobmods.lib.table_merge(table1[index], table2[index])
756
else
8-
table1[index] = util.table.deepcopy(table2[index])
57+
safe_insert_and_replace(table1, util.table.deepcopy(table2[index]), index)
958
end
1059
else
11-
table1[index] = value
60+
safe_insert_and_replace(table1, value, index)
1261
end
1362
end
1463
end
@@ -38,23 +87,6 @@ function bobmods.lib.belt_speed_ips(ips)
3887
return ips * 1 / 480
3988
end
4089

41-
--Inserts the new item into the table only if it doesn't already exist. (Index optional. Designed to insert strings only.)
42-
function bobmods.lib.safe_insert(array, new_item, index)
43-
local addit = true
44-
for i, item in pairs(array) do
45-
if item == new_item then
46-
addit = false
47-
end
48-
end
49-
if addit then
50-
if index then
51-
table.insert(array, index, new_item)
52-
else
53-
table.insert(array, new_item)
54-
end
55-
end
56-
end
57-
5890
--takes an item/fluid/entity(maybe even recipe) and returns a complete icons array.
5991
--if it has no icons= tag, it builds one from icon and icon_size.
6092
--Example use bobmods.lib.icons_from_item(data.raw.item.wood)

boblibrary/info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "boblibrary",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"factorio_version": "2.0",
55
"title": "Bob's Functions Library mod",
66
"author": "Bobingabout",

0 commit comments

Comments
 (0)