-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getTable support specify dictType
- Loading branch information
Showing
7 changed files
with
102 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export enum DictType { | ||
Array, | ||
Object, | ||
Map, | ||
} | ||
|
||
declare interface MapTransformOptions { | ||
refs?: Map<any, any>; | ||
dictType?: DictType; | ||
} | ||
|
||
const detectDuplicateKeys = (keys: any[]): boolean => { | ||
const set = new Set(); | ||
for (const _key of keys) { | ||
const key = String(_key); | ||
if (set.has(key)) { | ||
return true; | ||
} | ||
set.add(key); | ||
} | ||
return false; | ||
}; | ||
|
||
const detectDictType = (keys: any[]): DictType => { | ||
if (keys.some((key) => !['number', 'string'].includes(typeof key)) || detectDuplicateKeys(keys)) { | ||
return DictType.Map; | ||
} else if (keys.some((key) => typeof key === 'number')) { | ||
return DictType.Array; | ||
} else { | ||
return DictType.Object; | ||
} | ||
}; | ||
|
||
export const mapTransform = (map: Map<any, any>, options: MapTransformOptions = {}): Record<string, any> | any[] | Map<any, any> => { | ||
if (!options.refs) { | ||
options.refs = new Map(); | ||
} | ||
if (options.refs.has(map)) { | ||
return options.refs.get(map); | ||
} | ||
const keys = [...map.keys()]; | ||
|
||
// detect output type | ||
const dictType = options.dictType ? options.dictType : detectDictType(keys); | ||
if (dictType === DictType.Map) { | ||
return map; | ||
} | ||
const result: Record<any, any> = dictType === DictType.Array ? [] : {}; | ||
options.refs.set(map, result); | ||
|
||
for (const key of keys) { | ||
const value = map.get(key); | ||
if (value instanceof Map) { | ||
result[key] = mapTransform(value, options); | ||
continue; | ||
} | ||
result[key] = value; | ||
} | ||
return result; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
import { Lua } from '../dist/index.js'; | ||
import fs from 'fs'; | ||
|
||
// This file was created as a sandbox to test and debug on vscode | ||
const std = '/home/x3zvawq/workspace/JX3BOX/jx3-raw/unpack/std/scripts/include/CustomFunction.lua'; | ||
const origin = '/home/x3zvawq/workspace/JX3BOX/jx3-raw/unpack/origin/scripts/include/CustomFunction.lua'; | ||
|
||
const std_buffer = new Uint8Array(fs.readFileSync(std).buffer); | ||
const origin_buffer = new Uint8Array(fs.readFileSync(origin).buffer); | ||
|
||
const lua = await Lua.create(); | ||
await lua.doString(`print('Hello World!')`); | ||
lua.mountFile('/std', std_buffer); | ||
lua.mountFile('/origin', origin_buffer); | ||
|
||
await lua.doString(` | ||
function expose_locals() | ||
local i = 1 | ||
while true do | ||
local name, value = debug.getlocal(2, i) | ||
print(name, value) | ||
if not name then break end | ||
-- 将local变量设置为全局变量 | ||
_G[name] = value | ||
i = i + 1 | ||
end | ||
end | ||
`); | ||
await lua.doString(` | ||
local x = 1 | ||
expose_locals() | ||
`); | ||
await lua.doString(`expose_locals()`); | ||
await lua.doString('print(x)'); |