-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathfunctions.js
More file actions
235 lines (191 loc) · 5.21 KB
/
functions.js
File metadata and controls
235 lines (191 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import GraphemeSplitter from 'grapheme-splitter';
import Protocol from './protocol';
import Core from '../core';
import proplists from './erlang_compat/proplists';
import erlang from './erlang_compat/erlang';
function* call_property(item, property) {
if (!property) {
if (item instanceof Function || typeof item === 'function') {
return yield* item();
}
return item;
}
if (item instanceof Map) {
let prop = null;
if (item.has(property)) {
prop = property;
} else if (item.has(Symbol.for(property))) {
prop = Symbol.for(property);
}
if (prop === null) {
throw new Error(`Property ${property} not found in ${item}`);
}
return item.get(prop);
}
let prop = null;
if (
typeof item === 'number' ||
typeof item === 'symbol' ||
typeof item === 'boolean' ||
typeof item === 'string'
) {
if (item[property] !== undefined) {
prop = property;
} else if (item[Symbol.for(property)] !== undefined) {
prop = Symbol.for(property);
}
} else if (property in item) {
prop = property;
} else if (Symbol.for(property) in item) {
prop = Symbol.for(property);
}
if (prop === null) {
throw new Error(`Property ${property} not found in ${item}`);
}
if (item[prop] instanceof Function || typeof item[prop] === 'function') {
return item[prop]();
}
return item[prop];
}
function defprotocol(spec) {
return new Protocol(spec);
}
function defimpl(protocol, type, impl) {
protocol.implementation(type, impl);
}
function build_namespace(ns, ns_string) {
let parts = ns_string.split('.');
const root = ns;
let parent = ns;
if (parts[0] === 'Elixir') {
parts = parts.slice(1);
}
for (const part of parts) {
if (typeof parent[part] === 'undefined') {
parent[part] = {};
}
parent = parent[part];
}
root.__table__ = ns.__table__ || {};
root.__table__[Symbol.for(ns_string)] = parent;
return parent;
}
function map_to_object(map, options = []) {
const opt_keys = proplists.get_value(Symbol.for('keys'), options);
const opt_symbols = proplists.get_value(Symbol.for('symbols'), options);
const object = {};
for (const entry of map.entries()) {
let key = entry[0];
const value = entry[1];
if (opt_keys === Symbol.for('string') && typeof key === 'number') {
key = key.toString();
} else if (
(opt_keys === Symbol.for('string') || opt_symbols !== Symbol.for('undefined')) &&
typeof key === 'symbol'
) {
key = erlang.atom_to_binary(key);
}
if (value instanceof Map) {
object[key] = map_to_object(value, options);
} else if (opt_symbols !== Symbol.for('undefined') && typeof value === 'symbol') {
object[key] = erlang.atom_to_binary(value);
} else {
object[key] = value;
}
}
return object;
}
function object_to_map(object, options = []) {
const opt_atom_keys = proplists.get_value(Symbol.for('keys'), options) === Symbol.for('atom');
const opt_recurse_array = proplists.get_value(Symbol.for('recurse_array'), options) === true;
if (object.constructor === Object) {
const map = new Map();
Reflect.ownKeys(object).forEach((key) => {
let key2 = key;
let value = object[key];
if (opt_atom_keys && typeof key === 'string') {
key2 = Symbol.for(key);
}
if (
value !== null &&
(value.constructor === Object || (value instanceof Array && opt_recurse_array))
) {
value = object_to_map(value, options);
}
map.set(key2, value);
});
return map;
} else if (object instanceof Array && opt_recurse_array) {
return object.map((ele) => {
if (ele !== null && (ele.constructor === Object || ele instanceof Array)) {
return object_to_map(ele, options);
}
return ele;
});
}
throw new Error(`Object ${object} is not an native object or array`);
}
class Recurse {
constructor(func) {
this.func = func;
}
}
function* trampoline(f) {
let currentValue = f;
while (currentValue && currentValue instanceof Recurse) {
currentValue = yield* currentValue.func();
}
return currentValue;
}
function split_at(value, position) {
const splitter = new GraphemeSplitter();
const splitValues = splitter.splitGraphemes(value);
if (position < 0) {
const newPosition = splitValues.length + position;
if (newPosition < 0) {
return new Core.Tuple('', value);
}
return split_at(value, newPosition);
}
let first = '';
let second = '';
let index = 0;
for (const character of splitValues) {
if (index < position) {
first += character;
} else {
second += character;
}
index += 1;
}
return new Core.Tuple(first, second);
}
function graphemes(str) {
const splitter = new GraphemeSplitter();
return splitter.splitGraphemes(str);
}
function concat(head, tail) {
return [head].concat(tail);
}
function to_js_function(generator) {
const genObj = generator();
let value = null;
while (genObj.done === false) {
value = genObj.next(value);
}
return value;
}
export default {
call_property,
defprotocol,
defimpl,
build_namespace,
map_to_object,
object_to_map,
trampoline,
Recurse,
split_at,
graphemes,
concat,
to_js_function,
};