-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Record.mjs
38 lines (32 loc) · 1 KB
/
Record.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] = 'Record';
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 Record(obj = {})
{
if(new.target)
{
throw new Error('"Record" 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 entries = Object.fromEntries(Object.entries(obj).sort((a, b) => a[0] < b[0] ? -1 : 1));
const keys = Object.keys(entries);
const values = Object.values(entries);
const tagged = Tuple.bind({args: obj, base, length: keys.length, keys});
return tagged(Tuple(...keys), Tuple(...values), 'record');
}