Skip to content

Commit b4ea5e8

Browse files
committed
Added lualib extensions
1 parent aa4fd66 commit b4ea5e8

File tree

4 files changed

+122
-8
lines changed

4 files changed

+122
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules/
33
*.lua
44
!dist/*.js
5+
!dist/lualib/*.lua

dist/lualib/lib-typescript.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
declare class Set<T> {
2+
constructor(other?: Set<T>);
3+
add(item: T): void;
4+
contains(item: T): boolean;
5+
remove(item: T): boolean;
6+
items(): T[];
7+
count(): number;
8+
9+
//forEach<U>((item: T) => U): U[];
10+
}
11+
12+
declare class Map<S,T> {
13+
constructor(other?: Map<S,T>);
14+
put(key: S, value: T): void;
15+
remove(key: S): boolean;
16+
get(key: S): T;
17+
containsKey(key: S): boolean;
18+
keys(): S[];
19+
values(): T[];
20+
items(): {key: S, value: T}[];
21+
count(): number;
22+
}

dist/lualib/typescript.lua

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
-- Ternary operator
2+
function TS_ITE(condition, v1f, v2f)
3+
if condition then
4+
return v1f()
5+
else
6+
return v2f()
7+
end
8+
end
9+
10+
-- Set data structure implementation
11+
Set = Set or {}
12+
Set.__index = Set
13+
function Set.new(construct, ...)
14+
local instance = setmetatable({}, Set)
15+
Set.constructor(instance, ...)
16+
return instance
17+
end
18+
function Set.constructor(self, other)
19+
self._items = {}
20+
if other then
21+
for a, _ in pairs(other) do
22+
self._items[a] = true
23+
end
24+
end
25+
end
26+
function Set.add(self, item) self._items[item] = true end
27+
function Set.contains(self, item) return self._items[item] ~= nil end
28+
function Set.remove(self, item)
29+
local contains = Set.contains(self, item)
30+
self._items[item] = nil
31+
return contains
32+
end
33+
function Set.items(self)
34+
local out = {}
35+
for item, _ in pairs(self._items) do
36+
table.insert(out, item)
37+
end
38+
return out
39+
end
40+
function Set.count(self)
41+
local count = 0
42+
for item, _ in pairs(self._items) do
43+
count = count + 1
44+
end
45+
return count
46+
end
47+
48+
-- Set data structure implementation
49+
Map = Map or {}
50+
Map.__index = Map
51+
function Map.new(construct, ...)
52+
local instance = setmetatable({}, Map)
53+
Map.constructor(instance, ...)
54+
return instance
55+
end
56+
function Map.constructor(self, other)
57+
self._items = {}
58+
if other then
59+
for k, v in pairs(other) do
60+
self._items[k] = v
61+
end
62+
end
63+
end
64+
function Map.put(self, key, value) self._items[key] = value end
65+
function Map.containsKey(self, key) return self._items[key] ~= nil end
66+
function Map.remove(self, key)
67+
local contains = self.containsKey(self, key)
68+
self._items[key] = nil
69+
return contains
70+
end
71+
function Map.get(self, key) return self._items[key] end
72+
function Map.keys(self)
73+
local out = {}
74+
for k, v in pairs(self._items) do
75+
table.insert(out, k)
76+
end
77+
return out
78+
end
79+
function Map.values(self)
80+
local out = {}
81+
for k, v in pairs(self._items) do
82+
table.insert(out, v)
83+
end
84+
return out
85+
end
86+
function Map.items(self)
87+
local out = {}
88+
for k, v in pairs(self._items) do
89+
table.insert(out, {key=k, value=v})
90+
end
91+
return out
92+
end
93+
function Map.count(self)
94+
local count = 0
95+
for k, v in pairs(self._items) do
96+
count = count + 1
97+
end
98+
return count
99+
end

dist/typescript.lua

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)