aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r--src/gleam_stdlib.js49
1 files changed, 34 insertions, 15 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index 0cb2417..df27db2 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -259,6 +259,10 @@ export function regex_scan(regex, string) {
return List.fromArray(matches);
}
+export function map_hash(obj) {
+ return JSON.stringify(obj);
+}
+
export function new_map() {
return new Map();
}
@@ -268,57 +272,72 @@ export function map_size(map) {
}
export function map_to_list(map) {
- const result = [];
- map.forEach(([a, b]) => result.push([JSON.parse(a), b]))
+ return List.fromArray([...map.values()]);
}
export function map_from_list(list) {
- return new Map(list.map(([a, b]) => [JSON.stringify(a), b]));
+ return new Map(list.toArray().map(([a, b]) => [map_hash(a), [a, b]]));
}
export function map_has_key(k, map) {
- return map.contains(JSON.stringify(k));
+ return map.has(map_hash(k));
}
export function map_remove(k, map) {
- new Map(map).remove(JSON.stringify(k));
+ const result = new Map(map);
+ result.delete(map_hash(k));
+ return result;
}
export function map_filter(f, map) {
const result = new Map();
- map.entries.forEach(([a, b]) => {
- if (f(JSON.parse(a))) {
- result.set(a, b);
+ map.forEach(([key, value], hash) => {
+ if (f(key)) {
+ result.set(hash, [key, value]);
}
})
+ return result;
}
export function map_get(from, get) {
- from.get(JSON.stringify(get));
+ const entry = from.get(map_hash(get));
+ if (entry) {
+ return new Ok(entry[1]); // [0] is the key, [1] is the value
+ } else {
+ return new Error(Nil);
+ }
}
export function map_insert(key, value, map) {
- return new Map(map).set(JSON.stringify(key), value);
+ return new Map(map).set(map_hash(key), [key, value]);
}
export function map_keys(map) {
- return [...map.keys()].map(key => JSON.parse(key));
+ return List.fromArray([...map.values()].map(([key, value]) => key));
}
export function map_values(map) {
- return [...map.values()];
+ return List.fromArray([...map.values()].map(([key, value]) => value));
}
export function map_map_values(fn, map) {
const result = new Map();
- map.forEach(([a, b]) => result.set(JSON.stringify(fn(JSON.parse(a))), b));
+ map.forEach(([key, value], hash) => result.set(hash, [key, fn(key, value)]));
+ return result;
}
export function map_merge(into, merge) {
- return new Map(...into, ...merge);
+ return new Map([...into, ...merge]);
}
export function map_take(keys, map) {
const result = new Map();
- keys.forEach(key => result.set(JSON.stringify(key), map.get(JSON.stringify(key))));
+ keys.toArray().forEach(key => {
+ const hash = map_hash(key);
+ const keyValue = map.get(hash);
+ if (keyValue !== undefined) {
+ result.set(hash, keyValue);
+ }
+ });
+ return result;
} \ No newline at end of file