-
Notifications
You must be signed in to change notification settings - Fork 820
/
mapper.ts
34 lines (31 loc) · 874 Bytes
/
mapper.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
import { JavaMap, createMapProxy } from './map';
import { JavaArray } from './array';
import { JavaString } from './string';
import { isPlainObject } from 'lodash';
export function map(value: any) {
if (value instanceof JavaMap) return value;
if (value instanceof JavaArray) return value;
if (Array.isArray(value)) {
return new JavaArray(value.map(x => map(x)), map);
}
if (isPlainObject(value)) {
return createMapProxy(
new JavaMap(
Object.entries(value).reduce((sum, [k, v]) => {
return {
...sum,
[k]: map(v),
};
}, {}),
map
)
);
}
// eslint-disable-next-line
if (typeof value === 'string' && !((value as any) instanceof JavaString)) {
// eslint-disable-next-line
return new JavaString(value);
}
// for now we don't handle number.
return value;
}