mirror of
https://github.com/kucherenko/jscpd.git
synced 2026-09-19 08:11:03 +08:00
64e471e2b0
Adds 92 new tests (178 → 270 total) with zero source-file modifications: - packages/finder: 13 new test files covering all reporters, subscribers, validators, hooks, utils, options, cloneFound, getFilesToDetect, and InFilesDetector; shared buildClone() factory helper - apps/jscpd: 3 new test files (options-unit, print, store) plus extensions to formats, modes, and options integration tests - vitest.config.ts added to both packages to restrict coverage.include to src/** only, eliminating bin/ and prism.js noise - Fixes 3 tokenizer bugs (trailing space in format lookup, tokenization edge cases) uncovered by the new format fixture suite - Adds fixture files for 40+ language formats to support formats.test.ts
54 lines
1018 B
Lua
54 lines
1018 B
Lua
-- Common Lua utility functions
|
|
|
|
local M = {}
|
|
|
|
function M.validate_input(value)
|
|
if not value or value == "" then
|
|
return nil, "Input cannot be empty"
|
|
end
|
|
if #value > 255 then
|
|
return nil, "Input exceeds maximum length"
|
|
end
|
|
return value, nil
|
|
end
|
|
|
|
function M.table_contains(tbl, element)
|
|
for _, v in ipairs(tbl) do
|
|
if v == element then return true end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function M.map(tbl, fn)
|
|
local result = {}
|
|
for i, v in ipairs(tbl) do
|
|
result[i] = fn(v)
|
|
end
|
|
return result
|
|
end
|
|
|
|
function M.filter(tbl, predicate)
|
|
local result = {}
|
|
for _, v in ipairs(tbl) do
|
|
if predicate(v) then
|
|
result[#result + 1] = v
|
|
end
|
|
end
|
|
return result
|
|
end
|
|
|
|
function M.reduce(tbl, fn, init)
|
|
local acc = init
|
|
for _, v in ipairs(tbl) do
|
|
acc = fn(acc, v)
|
|
end
|
|
return acc
|
|
end
|
|
|
|
-- File-specific: sum a table
|
|
function M.sum(tbl)
|
|
return M.reduce(tbl, function(a, b) return a + b end, 0)
|
|
end
|
|
|
|
return M
|