1
/**
2
 * Changes object keys to camel case. If optional parameter `keys` is given, then we extract only the
3
 * keys specified in `keys`.
4
 */
5 1
export function makeCamelCase(obj: object, keys?: string[]) {
6 1
	if (!obj) return undefined;
7 1
	const newObj = {};
8 1
	const keysToRename = keys ? keys : Object.keys(obj);
9 1
	keysToRename.forEach(key => {
10 1
		if (obj.hasOwnProperty(key)) {
11
			// change the key to camelcase.
12 1
			const camelCaseKey = key.charAt(0).toLowerCase() + key.substr(1);
13 1
			Object.assign(newObj, { [camelCaseKey]: obj[key] });
14
		}
15
	});
16 1
	return newObj;
17
}
18

19
/**
20
 * Given an array of object, call makeCamelCase(...) on each option.
21
 */
22 1
export function makeCamelCaseArray(objArr: object[], keys?: string[]) {
23 1
	if (!objArr) return undefined;
24 1
	return objArr.map(obj => makeCamelCase(obj, keys));
25
}
26

27
/**
28
 * Converts blob to array buffer
29
 */
30 1
export function blobToArrayBuffer(blob: Blob): Promise<Uint8Array> {
31 1
	return new Promise((res, rej) => {
32 1
		const reader = new FileReader();
33 1
		reader.onload = _event => {
34 0
			res(reader.result as Uint8Array);
35
		};
36 1
		reader.onerror = err => {
37 0
			rej(err);
38
		};
39 1
		try {
40 1
			reader.readAsArrayBuffer(blob);
41
		} catch (err) {
42 0
			rej(err); // in case user gives invalid type
43
		}
44
	});
45
}

Read our documentation on viewing source code .

Loading