-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
94 lines (81 loc) · 2.29 KB
/
utility.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
export function Utf8ArrayToStr(array) {
// adopted from:
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <[email protected]>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
export function getDid(didDoc) {
// it might be a light or full kilt DID
didDoc = typeof didDoc === "string" ? JSON.parse(didDoc) : didDoc;
if (didDoc.uri) // ligth
return didDoc.uri.split(`:`, 4).join(`:`);
else
return didDoc.fullDid.uri;
}
export function strToCType(str) {
let obj = {};
let buf = str.split("~");
for (const i in buf) {
obj[i] = {
type: "string"
};
}
return obj;
}
export function getUri(obj) {
return obj.uri ? obj.uri : obj.fullDid.uri;
}
export function generateRandomNumber() {
var min = 1;
var max = 100;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
export function sortBy(key, arr) {
return arr.sort((a, b) => {
if (a[key] < b[key]) {
return -1;
} else if (a[key] > b[key]) {
return 1;
} else {
return 0;
}
});
}
export function matchProperty(attr, values) {
let props = {};
attr.map((x, i) => props[x] = values[i]);
return props;
}