Skip to content

Commit ee71dff

Browse files
authored
Added documentation for LuaTableHas and LuaTableDelete (#46)
* Added documentation for LuaTableHas and LuaTableDelete * Update prettier * Revert "Update prettier" This reverts commit be3477d. * fix prettier again
1 parent 203c0e9 commit ee71dff

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

docs/advanced/language-extensions.md

+31-10
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,25 @@ You can also map functions to table accessors (`__index` and `__newindex`). See
230230

231231
## Lua Table Types
232232

233+
The `LuaTable` type is provided to allow direct creation and manipulation of Lua tables. This is useful if you want to use a table that uses types other than string for its keys, as that is not supported by Typescript. Calls to lua method tables are translated to simple lua:
234+
235+
- `table.get(key)` Get a value by key -> `table[key]`
236+
- `table.set(key, value)` Set a value for key -> `table[key] = value`
237+
- `table.has(key)` Check if key is in table -> `table[key] ~= nil`
238+
- `table.delete(key)` Remove key (and its value) from table -> `table[key] = nil`
239+
240+
### Generic key and value types
241+
242+
`LuaTable` can be used without explicitly providing types for the keys and values, but also allows you to specify the type of keys and values in the table:
243+
244+
```ts
245+
const typedLuaTable = new LuaTable<KeyType, ValueType>();
246+
const untypedLuaTable = new LuaTable(); // Same as LuaTable<AnyNotNil, any>
247+
```
248+
233249
### Getting and Setting
234250

235-
The `LuaTable` type is provided to allow direct creation and manipulation of Lua tables. This is useful if you want to use a table that uses types other than string for its keys, as that is not supported by Typescript. Calls to `get` and `set` on the table will transpile directly to `value = table[key]` and `table[key] = value`.
251+
Calls to `get` and `set` on the table will transpile directly to `value = table[key]` and `table[key] = value`.
236252

237253
Example:
238254

@@ -273,7 +289,7 @@ print(#tbl)
273289
To iterate over a `LuaTable`, use `pairs()`. (This requires the `lua-types` library to be installed.)
274290

275291
```ts
276-
const tbl = new LuaTable();
292+
const tbl = new LuaTable<number, string>();
277293

278294
tbl.set(3, "bar");
279295
tbl.set(4, "bar");
@@ -287,14 +303,6 @@ for (const [key, value] of pairs(tbl)) {
287303

288304
(Remember that in Lua, `pairs()` returns the keys in a random order.)
289305

290-
### Restricting the Types
291-
292-
`LuaTable` can also be restricted to use only certain types as keys and values:
293-
294-
```ts
295-
const tbl = new LuaTable<KeyType, ValueType>();
296-
```
297-
298306
### Custom Getters and Setters
299307

300308
If you have a type that uses non-string keys, you can use `LuaTableGet` and `LuaTableSet` function types to declare your own getters & setters, similar to [Operator Map Types](#operator-map-types).
@@ -347,6 +355,19 @@ IdDictionary.set(dict, id, "bar");
347355
console.log(IdDictionary.get(dict, id));
348356
```
349357

358+
### All custom LuaTable functions
359+
360+
There are more LuaTable functions other than `LuaTableGet` and `LuaTableSet` that you can use:
361+
362+
- `LuaTableGet` - Standalone function that gets a value by key from a table.
363+
- `LuaTableGetMethod` - Method that gets a value by key from the table containing this method.
364+
- `LuaTableSet` - Standlone function that sets a value to a key in a table.
365+
- `LuaTableSetMethod` - Method that sets a value to a key in the table containing this method.
366+
- `LuaTableHas` - Standalone function that checks if a key is present in a table.
367+
- `LuaTableHasMethod` - Method that checks if a key is present in the table containing this method.
368+
- `LuaTableDelete` - Standalone function that removes a key and its value from a table.
369+
- `LuaTableDeleteMethod` - Method that removes a key and its value from table containing this method.
370+
350371
## $vararg Constant
351372

352373
Lua allows use of the ellipsis operator (`...`) to access command line arguments passed when executing a script file. To access this from Typescript, you can use the `$vararg` constant in a spread expression.

docusaurus-plugin.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ module.exports = () => ({
1818
resolve: {
1919
alias: {
2020
// Replace vendored monaco-typescript services build with typescript, already used by typescript-to-lua
21-
[require.resolve(
22-
"monaco-editor/esm/vs/language/typescript/lib/typescriptServices.js",
23-
)]: require.resolve("typescript"),
21+
[require.resolve("monaco-editor/esm/vs/language/typescript/lib/typescriptServices.js")]:
22+
require.resolve("typescript"),
2423

2524
// Exclude builtin monaco-typescript libs
2625
[require.resolve("monaco-editor/esm/vs/language/typescript/lib/lib.js")]: resolve(

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"file-loader": "^6.1.1",
4040
"fork-ts-checker-webpack-plugin": "^5.2.0",
4141
"null-loader": "^4.0.1",
42-
"prettier": "^2.1.2",
42+
"prettier": "^2.3.1",
4343
"raw-loader": "^4.0.2",
4444
"sass": "^1.27.0",
4545
"sass-loader": "^10.0.4",

0 commit comments

Comments
 (0)