From d7a94413a45ad2340da95457e1f501294c14620b Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 23 Mar 2026 09:54:23 +0900 Subject: [PATCH 1/6] add Array.createRepeatedElementArray --- lua/wikis/commons/Array.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lua/wikis/commons/Array.lua b/lua/wikis/commons/Array.lua index 28061222ee2..81fc83bb7bb 100644 --- a/lua/wikis/commons/Array.lua +++ b/lua/wikis/commons/Array.lua @@ -52,6 +52,41 @@ function Array.copy(tbl) return copy end +---Creates an array that contains `count` copies of the specified element. +--- +---If `count == 0`, then an empty array is returned. +--- +---If `element` is a table and `copyFunction` is not specified, +---then all elements in the returned array are reference equal. +--- +---This function is expensive if called with `copyFunction`. +---@generic T +---@param element T +---@param count integer +---@param copyFunction? fun(origElement: T): T +---@return T[] +---@nodiscard +function Array.createRepeatedElementArray(element, count, copyFunction) + assert(element ~= nil, 'element must not be nil') + if count < 0 then + error('count must be non-negative') + elseif count == 0 then + return {} + end + if copyFunction then + mw.incrementExpensiveFunctionCount() + end + local arr = {} + for _ = 1, count do + if copyFunction then + table.insert(arr, copyFunction(element)) + else + table.insert(arr, element) + end + end + return arr +end + ---Returns true if two arrays are equal to each other. ---@param arr1 any[] ---@param arr2 any[] From 162f7d7942cc8bc01b932ae513f776f8c86d4ac4 Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 23 Mar 2026 10:05:48 +0900 Subject: [PATCH 2/6] add tests --- lua/spec/array_spec.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/lua/spec/array_spec.lua b/lua/spec/array_spec.lua index 696ac06ee4a..01a018a7c58 100644 --- a/lua/spec/array_spec.lua +++ b/lua/spec/array_spec.lua @@ -45,6 +45,47 @@ describe('array', function() end) end) + describe('createRepeatedArray', function() + it('check', function() + assert.is_true(Array.equals({1}, Array.createRepeatedElementArray(1, 1))) + assert.is_true(Array.equals({2, 2}, Array.createRepeatedElementArray(2, 2))) + assert.is_true(Array.equals({3, 3, 3}, Array.createRepeatedElementArray(3, 3))) + assert.is_true(Array.equals( + { + 'The quick brown fox jumps over the lazy dog', + 'The quick brown fox jumps over the lazy dog', + 'The quick brown fox jumps over the lazy dog', + 'The quick brown fox jumps over the lazy dog', + 'The quick brown fox jumps over the lazy dog' + }, + Array.createRepeatedElementArray('The quick brown fox jumps over the lazy dog', 5) + )) + assert.is_true(Table.deepEquals( + { + {1, 2, 3}, + {1, 2, 3}, + }, + Array.createRepeatedElementArray(Array.range(1, 3), 2) + )) + end) + + it('check count==0', function () + assert.is_true(Array.equals({}, Array.createRepeatedElementArray('Lorem ipsum', 0))) + end) + + it('Error if illegal arguments are passed in', function() + assert.error(function () + return Array.createRepeatedElementArray(nil, 1) + end) + assert.error(function () + return Array.createRepeatedElementArray('nil', -3) + end) + assert.error(function () + return Array.createRepeatedElementArray(nil, 1) + end) + end) + end) + describe('Sub', function() it('check', function() local a = {3, 5, 7, 11} From 55dcda271a245d81d689f4f4e855ad9ab2034c12 Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 23 Mar 2026 10:11:38 +0900 Subject: [PATCH 3/6] call with copyFunction too --- lua/spec/array_spec.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lua/spec/array_spec.lua b/lua/spec/array_spec.lua index 01a018a7c58..44011a61e09 100644 --- a/lua/spec/array_spec.lua +++ b/lua/spec/array_spec.lua @@ -67,6 +67,13 @@ describe('array', function() }, Array.createRepeatedElementArray(Array.range(1, 3), 2) )) + assert.is_true(Table.deepEquals( + { + {1, 2, 3}, + {1, 2, 3}, + }, + Array.createRepeatedElementArray(Array.range(1, 3), 2, Array.copy) + )) end) it('check count==0', function () From 1d34db14fee26b82fd7446678303d4f9d1437fca Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 23 Mar 2026 10:44:23 +0900 Subject: [PATCH 4/6] kick expensive count --- lua/wikis/commons/Array.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lua/wikis/commons/Array.lua b/lua/wikis/commons/Array.lua index 81fc83bb7bb..952f9e9a8e6 100644 --- a/lua/wikis/commons/Array.lua +++ b/lua/wikis/commons/Array.lua @@ -58,8 +58,6 @@ end --- ---If `element` is a table and `copyFunction` is not specified, ---then all elements in the returned array are reference equal. ---- ----This function is expensive if called with `copyFunction`. ---@generic T ---@param element T ---@param count integer @@ -73,9 +71,6 @@ function Array.createRepeatedElementArray(element, count, copyFunction) elseif count == 0 then return {} end - if copyFunction then - mw.incrementExpensiveFunctionCount() - end local arr = {} for _ = 1, count do if copyFunction then From d607f37d31f27b6279053a6463277c3368020b1d Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 23 Mar 2026 10:47:29 +0900 Subject: [PATCH 5/6] use are_same --- lua/spec/array_spec.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lua/spec/array_spec.lua b/lua/spec/array_spec.lua index 44011a61e09..73c33a56959 100644 --- a/lua/spec/array_spec.lua +++ b/lua/spec/array_spec.lua @@ -47,10 +47,10 @@ describe('array', function() describe('createRepeatedArray', function() it('check', function() - assert.is_true(Array.equals({1}, Array.createRepeatedElementArray(1, 1))) - assert.is_true(Array.equals({2, 2}, Array.createRepeatedElementArray(2, 2))) - assert.is_true(Array.equals({3, 3, 3}, Array.createRepeatedElementArray(3, 3))) - assert.is_true(Array.equals( + assert.are_same({1}, Array.createRepeatedElementArray(1, 1)) + assert.are_same({2, 2}, Array.createRepeatedElementArray(2, 2)) + assert.are_same({3, 3, 3}, Array.createRepeatedElementArray(3, 3)) + assert.are_same( { 'The quick brown fox jumps over the lazy dog', 'The quick brown fox jumps over the lazy dog', @@ -59,21 +59,21 @@ describe('array', function() 'The quick brown fox jumps over the lazy dog' }, Array.createRepeatedElementArray('The quick brown fox jumps over the lazy dog', 5) - )) - assert.is_true(Table.deepEquals( + ) + assert.are_same( { {1, 2, 3}, {1, 2, 3}, }, Array.createRepeatedElementArray(Array.range(1, 3), 2) - )) - assert.is_true(Table.deepEquals( + ) + assert.are_same( { {1, 2, 3}, {1, 2, 3}, }, Array.createRepeatedElementArray(Array.range(1, 3), 2, Array.copy) - )) + ) end) it('check count==0', function () From f47203188c59ab0e82e073d9fbe9e5af0cefb075 Mon Sep 17 00:00:00 2001 From: ElectricalBoy <15651807+ElectricalBoy@users.noreply.github.com> Date: Mon, 23 Mar 2026 20:25:36 +0900 Subject: [PATCH 6/6] rename to Array.rep --- lua/spec/array_spec.lua | 20 ++++++++++---------- lua/wikis/commons/Array.lua | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lua/spec/array_spec.lua b/lua/spec/array_spec.lua index 73c33a56959..8c13be8d5ba 100644 --- a/lua/spec/array_spec.lua +++ b/lua/spec/array_spec.lua @@ -47,9 +47,9 @@ describe('array', function() describe('createRepeatedArray', function() it('check', function() - assert.are_same({1}, Array.createRepeatedElementArray(1, 1)) - assert.are_same({2, 2}, Array.createRepeatedElementArray(2, 2)) - assert.are_same({3, 3, 3}, Array.createRepeatedElementArray(3, 3)) + assert.are_same({1}, Array.rep(1, 1)) + assert.are_same({2, 2}, Array.rep(2, 2)) + assert.are_same({3, 3, 3}, Array.rep(3, 3)) assert.are_same( { 'The quick brown fox jumps over the lazy dog', @@ -58,37 +58,37 @@ describe('array', function() 'The quick brown fox jumps over the lazy dog', 'The quick brown fox jumps over the lazy dog' }, - Array.createRepeatedElementArray('The quick brown fox jumps over the lazy dog', 5) + Array.rep('The quick brown fox jumps over the lazy dog', 5) ) assert.are_same( { {1, 2, 3}, {1, 2, 3}, }, - Array.createRepeatedElementArray(Array.range(1, 3), 2) + Array.rep(Array.range(1, 3), 2) ) assert.are_same( { {1, 2, 3}, {1, 2, 3}, }, - Array.createRepeatedElementArray(Array.range(1, 3), 2, Array.copy) + Array.rep(Array.range(1, 3), 2, Array.copy) ) end) it('check count==0', function () - assert.is_true(Array.equals({}, Array.createRepeatedElementArray('Lorem ipsum', 0))) + assert.is_true(Array.equals({}, Array.rep('Lorem ipsum', 0))) end) it('Error if illegal arguments are passed in', function() assert.error(function () - return Array.createRepeatedElementArray(nil, 1) + return Array.rep(nil, 1) end) assert.error(function () - return Array.createRepeatedElementArray('nil', -3) + return Array.rep('nil', -3) end) assert.error(function () - return Array.createRepeatedElementArray(nil, 1) + return Array.rep(nil, 1) end) end) end) diff --git a/lua/wikis/commons/Array.lua b/lua/wikis/commons/Array.lua index 952f9e9a8e6..dad519958fe 100644 --- a/lua/wikis/commons/Array.lua +++ b/lua/wikis/commons/Array.lua @@ -64,7 +64,7 @@ end ---@param copyFunction? fun(origElement: T): T ---@return T[] ---@nodiscard -function Array.createRepeatedElementArray(element, count, copyFunction) +function Array.rep(element, count, copyFunction) assert(element ~= nil, 'element must not be nil') if count < 0 then error('count must be non-negative')