-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
77 lines (65 loc) · 1.78 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { CloudMessageValue } from '../client/ICloudClient';
class ArduinoCloudError extends Error {
constructor(public code: number, message: string) {
super(message);
this.name = this.constructor.name;
try {
Error.captureStackTrace(this, this.constructor);
} catch (error) {}
}
}
function isObject(value: CloudMessageValue): value is object {
return typeof value === 'object';
}
function isNumber(value: CloudMessageValue): value is number {
return typeof value === 'number';
}
function isString(value: CloudMessageValue): value is string {
return typeof value === 'string';
}
function isBoolean(value: CloudMessageValue): value is boolean {
return typeof value === 'boolean';
}
function isArray<T>(value: CloudMessageValue): value is T[] {
return Array.isArray(value);
}
function isNotAnEmptyObject(value): boolean {
return !(typeof value === 'object' && Object.keys(value).length == 0);
}
function toArrayBuffer(buf: { length: number }): ArrayBuffer {
const ab = new ArrayBuffer(buf.length);
const view = new Uint8Array(ab);
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return ab;
}
function toBuffer(ab: ArrayBuffer): Buffer {
const buffer = new Buffer(ab.byteLength);
const view = new Uint8Array(ab);
for (let i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
function arrayBufferToBase64(buf: ArrayBuffer): string {
let binary = '';
const bytes = new Uint8Array(buf);
const len = bytes.byteLength;
for (let i = 0; i < len; i += 1) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
export default {
ArduinoCloudError,
isObject,
isNumber,
isString,
isBoolean,
isArray,
toArrayBuffer,
toBuffer,
arrayBufferToBase64,
isNotAnEmptyObject,
};