-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (34 loc) · 1.02 KB
/
index.ts
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
import type from "@unction/type";
import {merge} from "most";
export default function mergeRight<A> (left: Array<A> | Set<A> | Record<string | number | symbol, B> | Map<B, A> | string) {
return function mergeRightLeft (right: Array<A> | Set<A> | Record<string | number | symbol, B> | Map<B, A> | string) {
if (type(left) !== type(right)) {
throw new Error(`mergeRight received a ${type(left)} and ${type(right)} which aren't the same`);
}
switch (type(left)) {
case "Array": {
return [...left, ...right];
}
case "Object": {
return {...left,
...right,
};
}
case "Map": {
return new Map([...left, ...right]);
}
case "Set": {
return new Set([...left, ...right]);
}
case "String": {
return `${left}${right}`;
}
case "Stream": {
return merge(left, right);
}
default: {
throw new Error(`mergeRight doesn't know how to deal with ${type(left)}`);
}
}
};
}