mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 12:41:37 +00:00
feat(sync): add flatten() function (#43)
This commit is contained in:
parent
63388761f8
commit
8063ea3142
4 changed files with 89 additions and 0 deletions
28
packages/charm-sync/src/flatten.luau
Normal file
28
packages/charm-sync/src/flatten.luau
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
local types = require(script.Parent.types)
|
||||
type AtomMap = types.AtomMap
|
||||
|
||||
type NestedAtomMap = {
|
||||
[string]: NestedAtomMap | () -> (),
|
||||
}
|
||||
|
||||
local function flatten(atoms: NestedAtomMap): AtomMap
|
||||
local result: AtomMap = {}
|
||||
|
||||
local function visit(node: NestedAtomMap, path: string)
|
||||
for key, value in node do
|
||||
local location = if path == "" then key else path .. "/" .. key
|
||||
|
||||
if type(value) == "table" then
|
||||
visit(value, location)
|
||||
else
|
||||
result[location] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
visit(atoms, "")
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
return flatten
|
||||
Loading…
Add table
Add a link
Reference in a new issue