forked from teambit/bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlane-objects.ts
More file actions
54 lines (47 loc) · 1.48 KB
/
lane-objects.ts
File metadata and controls
54 lines (47 loc) · 1.48 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
import { toBase64ArrayBuffer } from '../utils';
import { Lane } from './models';
import BitObject from './objects/object';
export default class LaneObjects {
lane: Buffer;
objects: Buffer[];
constructor(lane: Buffer, objects: Buffer[]) {
this.lane = lane;
this.objects = objects;
}
toString(): string {
return JSON.stringify({
lane: toBase64ArrayBuffer(this.lane),
objects: this.objects.map(toBase64ArrayBuffer),
});
}
static fromString(str: string): LaneObjects {
return LaneObjects.fromObject(JSON.parse(str));
}
static fromObject(object: Record<string, any>): LaneObjects {
const { lane, objects } = object;
return new LaneObjects(_from64Buffer(lane), objects.map(_from64Buffer));
}
/**
* prefer using `this.toObjectsAsync()` if not must to be sync.
*/
toObjects(): { lane: Lane; objects: BitObject[] } {
return {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
lane: BitObject.parseSync(this.lane),
objects: this.objects.map((obj) => BitObject.parseSync(obj)),
};
}
/**
* see `this.toObject()` for the sync version
*/
async toObjectsAsync(): Promise<{ lane: Lane; objects: BitObject[] }> {
return {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
lane: await BitObject.parseObject(this.lane),
objects: await Promise.all(this.objects.map((obj) => BitObject.parseObject(obj))),
};
}
}
function _from64Buffer(val): Buffer {
return Buffer.from(val, 'base64');
}