-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dict.mjs
38 lines (31 loc) · 914 Bytes
/
Dict.mjs
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
import Tuple from './Tuple.mjs';
import { size, keys } from './Tuple.mjs';
const base = Object.create(null);
base.toString = Object.prototype.toString;
base[Symbol.toStringTag] = 'Dict';
base[Symbol.iterator] = function() {
let index = 0;
return { next: () => {
const iteration = index++;
if(this[size] < index)
{
return { done: true };
}
return {value: [this[keys][iteration], this[this[keys][iteration]]], done: false };
}};
};
export default function Dict(obj = {})
{
if(new.target)
{
throw new Error('"Dict" is not a constructor. Create a Record by invoking the function directly.');
}
if(!obj || typeof obj !== 'object')
{
throw new Error('Parameter must be an object.');
}
const keys = Object.keys(obj);
const values = Object.values(obj);
const tagged = Tuple.bind({args: obj, base, length: keys.length, keys});
return tagged('dict', Tuple(...keys), Tuple(...values));
}