-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup.mjs
32 lines (29 loc) · 821 Bytes
/
Group.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
import Tuple from './Tuple.mjs';
import { _index, size } from './Tuple.mjs';
const base = Object.create(null);
base.toString = Object.prototype.toString;
base[Symbol.toStringTag] = 'Group';
base.toJSON = function() {
return [...this];
};
base[Symbol.iterator] = function() {
let index = 0;
return { next: () => {
const iteration = index++;
if(this[size] < index)
{
return { done: true };
}
return { value: this[iteration], done: false };
}};
};
export default function Group(...args)
{
if(new.target)
{
throw new Error('"Group" is not a constructor. Create a Group by invoking the function directly.');
}
const tuples = args.map(a => Tuple(a)).sort((a, b) => a[_index] < b[_index] ? -1 : 1);
const tagged = Tuple.bind({args: tuples.map(t => t[0]), base});
return tagged(...tuples, 'group');
}