-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtomatreshka.js
32 lines (25 loc) · 1014 Bytes
/
tomatreshka.js
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
// recursively converts objects and arrays to Matreshka.Object and Matreshka.Array instances
export default function toMatreshka(data) {
// fix circular ref issue
const MatreshkaObject = require('./object');
const MatreshkaArray = require('./array');
// convert only objects
if (data && typeof data === 'object') {
if ('length' in data) {
// if length is given convert it to Matreshka.Array instance
const arrayItems = Array(data.length);
nofn.forEach(data, (item, index) => {
arrayItems[index] = toMatreshka(item);
});
return new MatreshkaArray().recreate(arrayItems);
}
// if length is not given convert it to Matreshka.Object instance
const object = {};
nofn.forOwn(data, (value, key) => {
object[key] = toMatreshka(value);
});
return new MatreshkaObject(object);
}
// for all non-objects just return passed data
return data;
}