@quesmed/types 1.3.14 → 1.3.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gql_input_output_types/User.cjs +2 -0
- package/gql_input_output_types/User.js +1 -2
- package/gql_input_output_types/index.cjs +2 -0
- package/gql_input_output_types/index.js +1 -2
- package/index.cjs +20 -0
- package/index.js +4 -7
- package/models/Author.cjs +2 -0
- package/models/Author.js +1 -2
- package/models/Book.cjs +2 -0
- package/models/Book.js +1 -2
- package/models/Card.cjs +2 -0
- package/models/Card.js +1 -2
- package/models/Chapter.cjs +2 -0
- package/models/Chapter.js +1 -2
- package/models/Concept.cjs +2 -0
- package/models/Concept.js +1 -2
- package/models/Difficulty.cjs +10 -0
- package/models/Difficulty.js +2 -5
- package/models/Feedback.cjs +2 -0
- package/models/Feedback.js +1 -2
- package/models/File.cjs +2 -0
- package/models/File.js +1 -2
- package/models/Marksheet.cjs +9 -0
- package/models/Marksheet.js +2 -5
- package/models/MockTest.cjs +2 -0
- package/models/MockTest.js +1 -2
- package/models/OsceMarksheet.cjs +23 -0
- package/models/OsceMarksheet.js +4 -7
- package/models/OsceStation.cjs +45 -0
- package/models/OsceStation.js +3 -6
- package/models/Picture.cjs +6 -0
- package/models/Picture.js +1 -2
- package/models/Promo.cjs +2 -0
- package/models/Promo.js +1 -2
- package/models/Question.cjs +37 -0
- package/models/Question.js +8 -15
- package/models/Subscription.cjs +9 -0
- package/models/Subscription.js +2 -5
- package/models/Todo.cjs +2 -0
- package/models/Todo.js +1 -2
- package/models/Token.cjs +2 -0
- package/models/Token.js +1 -2
- package/models/Topic.cjs +13 -0
- package/models/Topic.js +2 -5
- package/models/Type.cjs +2 -0
- package/models/Type.js +1 -2
- package/models/University.cjs +2 -0
- package/models/University.js +1 -2
- package/models/User.cjs +2 -0
- package/models/User.d.ts +1 -0
- package/models/User.js +1 -2
- package/models/Video.cjs +2 -0
- package/models/Video.js +1 -2
- package/models/index.cjs +35 -0
- package/models/index.js +23 -35
- package/package.json +17 -4
- package/resolvers/apollo.cjs +2 -0
- package/resolvers/apollo.d.ts +8 -0
- package/resolvers/apollo.js +1 -0
- package/resolvers/mutation/admin/algoliaSync.cjs +2 -0
- package/resolvers/mutation/admin/algoliaSync.js +1 -2
- package/resolvers/mutation/admin/index.cjs +14 -0
- package/resolvers/mutation/admin/index.js +2 -14
- package/resolvers/mutation/admin/token.cjs +2 -0
- package/resolvers/mutation/admin/token.js +1 -2
- package/resolvers/mutation/index.cjs +14 -0
- package/resolvers/mutation/index.js +2 -14
- package/resolvers/mutation/restricted/agora.cjs +2 -0
- package/resolvers/mutation/restricted/agora.js +1 -2
- package/resolvers/mutation/restricted/contactUs.cjs +2 -0
- package/resolvers/mutation/restricted/contactUs.js +1 -2
- package/resolvers/mutation/restricted/index.cjs +22 -0
- package/resolvers/mutation/restricted/index.js +10 -22
- package/resolvers/mutation/restricted/marksheet.cjs +2 -0
- package/resolvers/mutation/restricted/marksheet.js +1 -2
- package/resolvers/mutation/restricted/mockTest.cjs +2 -0
- package/resolvers/mutation/restricted/mockTest.js +1 -2
- package/resolvers/mutation/restricted/osce.cjs +2 -0
- package/resolvers/mutation/restricted/osce.js +1 -2
- package/resolvers/mutation/restricted/questionDiscussion.cjs +2 -0
- package/resolvers/mutation/restricted/questionDiscussion.js +1 -2
- package/resolvers/mutation/restricted/todo.cjs +29 -0
- package/resolvers/mutation/restricted/todo.d.ts +5 -0
- package/resolvers/mutation/restricted/todo.js +25 -2
- package/resolvers/mutation/restricted/token.cjs +2 -0
- package/resolvers/mutation/restricted/token.js +1 -2
- package/resolvers/mutation/restricted/users.cjs +2 -0
- package/resolvers/mutation/restricted/users.js +1 -2
- package/resolvers/mutation/restricted/video.cjs +2 -0
- package/resolvers/mutation/restricted/video.js +1 -2
- package/resolvers/mutation/stripe.cjs +2 -0
- package/resolvers/mutation/stripe.js +1 -2
- package/resolvers/mutation/users.cjs +2 -0
- package/resolvers/mutation/users.js +1 -2
- package/resolvers/mutation/validUserToken/index.cjs +13 -0
- package/resolvers/mutation/validUserToken/index.js +1 -13
- package/resolvers/mutation/validUserToken/user.cjs +2 -0
- package/resolvers/mutation/validUserToken/user.js +1 -2
- package/resolvers/query/admin/getUserToken.cjs +2 -0
- package/resolvers/query/admin/getUserToken.js +1 -2
- package/resolvers/query/admin/index.cjs +13 -0
- package/resolvers/query/admin/index.js +1 -13
- package/resolvers/query/author.cjs +2 -0
- package/resolvers/query/author.js +1 -2
- package/resolvers/query/book.cjs +2 -0
- package/resolvers/query/book.js +1 -2
- package/resolvers/query/feedback.cjs +2 -0
- package/resolvers/query/feedback.js +1 -2
- package/resolvers/query/index.cjs +21 -0
- package/resolvers/query/index.js +9 -21
- package/resolvers/query/restricted/anatomy.cjs +2 -0
- package/resolvers/query/restricted/anatomy.js +1 -2
- package/resolvers/query/restricted/index.cjs +22 -0
- package/resolvers/query/restricted/index.js +10 -22
- package/resolvers/query/restricted/marksheet.cjs +2 -0
- package/resolvers/query/restricted/marksheet.js +1 -2
- package/resolvers/query/restricted/mockTests.cjs +2 -0
- package/resolvers/query/restricted/mockTests.js +1 -2
- package/resolvers/query/restricted/osce.cjs +2 -0
- package/resolvers/query/restricted/osce.js +1 -2
- package/resolvers/query/restricted/quesBook.cjs +2 -0
- package/resolvers/query/restricted/quesBook.js +1 -2
- package/resolvers/query/restricted/todos.cjs +2 -0
- package/resolvers/query/restricted/todos.js +1 -2
- package/resolvers/query/restricted/topics.cjs +2 -0
- package/resolvers/query/restricted/topics.js +1 -2
- package/resolvers/query/restricted/university.cjs +2 -0
- package/resolvers/query/restricted/university.js +1 -2
- package/resolvers/query/restricted/user.cjs +2 -0
- package/resolvers/query/restricted/user.js +1 -2
- package/resolvers/query/restricted/video.cjs +2 -0
- package/resolvers/query/restricted/video.js +1 -2
- package/resolvers/query/sampleCards.cjs +2 -0
- package/resolvers/query/sampleCards.js +1 -2
- package/resolvers/query/sampleQuestions.cjs +2 -0
- package/resolvers/query/sampleQuestions.js +1 -2
- package/resolvers/query/subscription.cjs +2 -0
- package/resolvers/query/subscription.js +1 -2
- package/resolvers/query/university.cjs +2 -0
- package/resolvers/query/university.js +1 -2
- package/resolvers/query/user.cjs +2 -0
- package/resolvers/query/user.js +1 -2
- package/resolvers/query/video.cjs +2 -0
- package/resolvers/query/video.js +1 -2
- package/resolvers/subscription/index.cjs +13 -0
- package/resolvers/subscription/index.js +1 -13
- package/resolvers/subscription/osce.cjs +43 -0
- package/resolvers/subscription/osce.js +10 -14
- package/utils/commonFunctions.cjs +223 -0
- package/utils/commonFunctions.js +11 -17
- package/utils/index.cjs +17 -0
- package/utils/index.d.ts +5 -0
- package/utils/index.js +5 -0
- package/utils/lightgallery.cjs +91 -0
- package/utils/lightgallery.d.ts +4 -2
- package/utils/lightgallery.js +5 -6
- package/utils/offlineLink.cjs +237 -0
- package/utils/offlineLink.d.ts +64 -0
- package/utils/offlineLink.js +230 -0
- package/utils/uuid4.cjs +287 -0
- package/utils/uuid4.js +1 -3
- package/utils/wordsToNumber.cjs +48 -0
- package/utils/wordsToNumber.js +1 -5
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { ApolloLink, Observable, } from '@apollo/client';
|
|
11
|
+
import uuid from './uuid4';
|
|
12
|
+
export class OfflineLink extends ApolloLink {
|
|
13
|
+
/**
|
|
14
|
+
* storage
|
|
15
|
+
* Provider that will persist the mutation queue. This can be any AsyncStorage compatible storage instance.
|
|
16
|
+
*
|
|
17
|
+
* retryInterval
|
|
18
|
+
* Milliseconds between attempts to retry failed mutations. Defaults to 0, which disables automatic retry.
|
|
19
|
+
*
|
|
20
|
+
* sequential
|
|
21
|
+
* Indicates if the attempts should be retried in order. Defaults to false which retries all failed mutations in parallel.
|
|
22
|
+
*
|
|
23
|
+
* retryOnServerError
|
|
24
|
+
* Indicates if mutations should be reattempted if there are server side errors, useful to retry mutations on session expiration. Defaults to false.
|
|
25
|
+
*/
|
|
26
|
+
constructor({ storage, retryInterval = 0, sequential = false, retryOnServerError = false, }) {
|
|
27
|
+
super();
|
|
28
|
+
this.queue = new Map();
|
|
29
|
+
this.prefix = 'offlineLink:';
|
|
30
|
+
if (!storage) {
|
|
31
|
+
throw new Error('Storage is required, it can be an AsyncStorage compatible storage instance.');
|
|
32
|
+
}
|
|
33
|
+
this.storage = storage;
|
|
34
|
+
this.sequential = sequential;
|
|
35
|
+
this.retryInterval = retryInterval;
|
|
36
|
+
this.retryOnServerError = retryOnServerError;
|
|
37
|
+
}
|
|
38
|
+
request(operation, forward) {
|
|
39
|
+
const context = operation.getContext();
|
|
40
|
+
const { query, variables } = operation || {};
|
|
41
|
+
if (!context.optimisticResponse) {
|
|
42
|
+
// If the mutation does not have an optimisticResponse then we can't defer it
|
|
43
|
+
return forward(operation);
|
|
44
|
+
}
|
|
45
|
+
return new Observable((observer) => {
|
|
46
|
+
const mutationId = uuid.asBase64();
|
|
47
|
+
this.add({
|
|
48
|
+
mutation: query,
|
|
49
|
+
variables,
|
|
50
|
+
optimisticResponse: context.optimisticResponse,
|
|
51
|
+
}, mutationId);
|
|
52
|
+
const subscription = forward(operation).subscribe({
|
|
53
|
+
next: (result) => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
yield this.remove(mutationId);
|
|
55
|
+
observer.next(result);
|
|
56
|
+
}),
|
|
57
|
+
error: () => {
|
|
58
|
+
// Mutation failed so we try again after a certain amount of time.
|
|
59
|
+
this.delayedSync();
|
|
60
|
+
// Resolve the mutation with the optimistic response so the UI can be updated
|
|
61
|
+
observer.next({
|
|
62
|
+
data: context.optimisticResponse,
|
|
63
|
+
dataPresent: true,
|
|
64
|
+
errors: [],
|
|
65
|
+
});
|
|
66
|
+
// Say we're all done so the UI is re-rendered.
|
|
67
|
+
observer.complete();
|
|
68
|
+
},
|
|
69
|
+
complete: () => observer.complete(),
|
|
70
|
+
});
|
|
71
|
+
return () => {
|
|
72
|
+
subscription.unsubscribe();
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Obtains the queue of mutations that must be sent to the server.
|
|
78
|
+
* These are kept in a Map to preserve the order of the mutations in the queue.
|
|
79
|
+
*/
|
|
80
|
+
getQueue() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const map = new Map();
|
|
83
|
+
try {
|
|
84
|
+
const storedMutations = yield this.storage.getItem(this.prefix + 'Mutations');
|
|
85
|
+
if (!storedMutations) {
|
|
86
|
+
return map;
|
|
87
|
+
}
|
|
88
|
+
const mutationIds = storedMutations.split(',');
|
|
89
|
+
for (const mutationId of mutationIds) {
|
|
90
|
+
const storedMutation = yield this.storage.getItem(this.prefix + mutationId);
|
|
91
|
+
if (!storedMutation) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
map.set(mutationId, JSON.parse(storedMutation));
|
|
95
|
+
}
|
|
96
|
+
return map;
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
console.error(e);
|
|
100
|
+
return map;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Persist the queue so mutations can be retried at a later point in time.
|
|
106
|
+
*/
|
|
107
|
+
saveQueue(mutationId, mutation) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
try {
|
|
110
|
+
if (mutationId && mutation) {
|
|
111
|
+
yield this.storage.setItem(this.prefix + mutationId, JSON.stringify(mutation));
|
|
112
|
+
}
|
|
113
|
+
yield this.storage.setItem(this.prefix + 'Mutations', [...this.queue.keys()].join());
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
console.error(e);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Add a mutation attempt to the queue so that it can be retried at a later point in time.
|
|
122
|
+
*/
|
|
123
|
+
add(item, mutationId) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
// We give the mutation attempt a random id so that it is easy to remove when needed (in sync loop)
|
|
126
|
+
if (!mutationId) {
|
|
127
|
+
mutationId = uuid.asBase64();
|
|
128
|
+
}
|
|
129
|
+
this.queue.set(mutationId, item);
|
|
130
|
+
yield this.saveQueue(mutationId, item);
|
|
131
|
+
return mutationId;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Remove a mutation attempt from the queue.
|
|
136
|
+
*/
|
|
137
|
+
remove(mutationId) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
this.queue.delete(mutationId);
|
|
140
|
+
yield this.storage.removeItem(this.prefix + mutationId);
|
|
141
|
+
yield this.saveQueue();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Takes the mutations in the queue and try to send them to the server again.
|
|
146
|
+
*/
|
|
147
|
+
sync() {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
if (!this.client) {
|
|
150
|
+
throw new Error('Offline Link not setup with ApolloClient');
|
|
151
|
+
}
|
|
152
|
+
if (this.queue.size === 0) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const attempts = Array.from(this.queue);
|
|
156
|
+
if (this.sequential) {
|
|
157
|
+
for (const [mutationId, attempt] of attempts) {
|
|
158
|
+
let serverProcessed = false;
|
|
159
|
+
try {
|
|
160
|
+
yield this.client.mutate(Object.assign(Object.assign({}, attempt), { optimisticResponse: undefined }));
|
|
161
|
+
serverProcessed = true;
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
const err = e;
|
|
165
|
+
const networkError = err.networkError;
|
|
166
|
+
if (!this.retryOnServerError && (networkError === null || networkError === void 0 ? void 0 : networkError.response)) {
|
|
167
|
+
// There are GraphQL errors, which means the server processed the request so we can remove the mutation from the queue
|
|
168
|
+
serverProcessed = true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (serverProcessed) {
|
|
172
|
+
try {
|
|
173
|
+
yield this.remove(mutationId);
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
console.error(e);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
yield Promise.all(attempts.map(([mutationId, attempt]) => __awaiter(this, void 0, void 0, function* () {
|
|
183
|
+
var _a;
|
|
184
|
+
let serverProcessed = false;
|
|
185
|
+
try {
|
|
186
|
+
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.mutate(Object.assign(Object.assign({}, attempt), { optimisticResponse: undefined })));
|
|
187
|
+
serverProcessed = true;
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
const err = e;
|
|
191
|
+
const networkError = err.networkError;
|
|
192
|
+
if (!this.retryOnServerError && (networkError === null || networkError === void 0 ? void 0 : networkError.response)) {
|
|
193
|
+
serverProcessed = true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (serverProcessed) {
|
|
197
|
+
yield this.remove(mutationId);
|
|
198
|
+
}
|
|
199
|
+
})));
|
|
200
|
+
}
|
|
201
|
+
// Remaining mutations in the queue are persisted
|
|
202
|
+
yield this.saveQueue();
|
|
203
|
+
if (this.queue.size !== 0 || this.retryInterval !== 0) {
|
|
204
|
+
this.delayedSync();
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Runs sync() after the retryInterval
|
|
210
|
+
*/
|
|
211
|
+
delayedSync() {
|
|
212
|
+
if (this.retryInterval === 0) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (this.timeoutId) {
|
|
216
|
+
clearTimeout(this.timeoutId);
|
|
217
|
+
}
|
|
218
|
+
this.timeoutId = setTimeout(() => this.sync, this.retryInterval);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Configure the link to use Apollo Client and immediately try to sync the queue (if there's anything there).
|
|
222
|
+
*/
|
|
223
|
+
setup(client) {
|
|
224
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
225
|
+
this.client = client;
|
|
226
|
+
this.queue = yield this.getQueue();
|
|
227
|
+
yield this.sync();
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
package/utils/uuid4.cjs
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class Uuid4 {
|
|
4
|
+
/**
|
|
5
|
+
* Generate a custom base 64 encoded UUID v4 (random).
|
|
6
|
+
*
|
|
7
|
+
* @param {Uint8Array} [data] Should normally be `undefined` to create a
|
|
8
|
+
* truly random v4 UUID.
|
|
9
|
+
* @returns {string} Returns a custom base 64 encoded UUID v4.
|
|
10
|
+
*/
|
|
11
|
+
static asBase64(data) {
|
|
12
|
+
// Add an initial 4 bit zero pad so result is always 22 chars long.
|
|
13
|
+
const binString = '0000' + Uuid4._asBinString(data);
|
|
14
|
+
let result = '';
|
|
15
|
+
let pieces = [
|
|
16
|
+
0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102,
|
|
17
|
+
108, 114, 120, 126,
|
|
18
|
+
];
|
|
19
|
+
for (const piece of pieces) {
|
|
20
|
+
result += Uuid4._base64[binString.substr(piece, 6)];
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Generate a hexadecimal encoded UUID v4 (random).
|
|
26
|
+
*
|
|
27
|
+
* @param {Uint8Array} [data] Should normally be `undefined` to create a
|
|
28
|
+
* truly random v4 UUID.
|
|
29
|
+
* @returns {string} Returns a hexadecimal encoded UUID v4.
|
|
30
|
+
*/
|
|
31
|
+
static asHexString(data) {
|
|
32
|
+
const binString = Uuid4._asBinString(data);
|
|
33
|
+
let hexString = '';
|
|
34
|
+
const pieces = [0, 16, 32, 48, 64, 80, 96, 112];
|
|
35
|
+
for (const piece of pieces) {
|
|
36
|
+
hexString += parseInt(binString.substr(piece, 16), 2)
|
|
37
|
+
.toString(16)
|
|
38
|
+
.padStart(4, '0');
|
|
39
|
+
}
|
|
40
|
+
return hexString;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generate a standard UUID v4 (random).
|
|
44
|
+
*
|
|
45
|
+
* @param {Uint8Array} [data] Should normally be `undefined` to create a
|
|
46
|
+
* truly random v4 UUID.
|
|
47
|
+
* @returns {string} Returns a standard UUID v4.
|
|
48
|
+
*/
|
|
49
|
+
static asUuid(data) {
|
|
50
|
+
const hex = Uuid4.asHexString(data);
|
|
51
|
+
return Uuid4.fromHexStringToUuid(hex);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Convert from a base 64 encoded to a hexadecimal encoded UUID.
|
|
55
|
+
*
|
|
56
|
+
* NOTE: This method does not verify input is valid UUID.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} data A base 64 encoded UUID.
|
|
59
|
+
* @returns {string} Returns a hexadecimal encoded UUID.
|
|
60
|
+
*/
|
|
61
|
+
static fromBase64ToHexString(data) {
|
|
62
|
+
if (22 !== data.length) {
|
|
63
|
+
const mess = `Expected base 64 number length of 22 characters but was given length: ${data.length}`;
|
|
64
|
+
throw new RangeError(mess);
|
|
65
|
+
}
|
|
66
|
+
// Need switched keys and values so reverse lookups can be done.
|
|
67
|
+
let flipped = {};
|
|
68
|
+
for (const [key, value] of Object.entries(Uuid4._base64)) {
|
|
69
|
+
flipped[value] = key;
|
|
70
|
+
}
|
|
71
|
+
let binString = '';
|
|
72
|
+
for (let i = 0, len = data.length; i < len; ++i) {
|
|
73
|
+
binString += flipped[data[i]];
|
|
74
|
+
}
|
|
75
|
+
// Cut off 4 bit zero padding.
|
|
76
|
+
binString = binString.substr(-128);
|
|
77
|
+
let hexString = '';
|
|
78
|
+
const pieces = [0, 32, 64, 96];
|
|
79
|
+
for (const piece of pieces) {
|
|
80
|
+
hexString += parseInt(binString.substr(piece, 32), 2)
|
|
81
|
+
.toString(16)
|
|
82
|
+
.padStart(8, '0');
|
|
83
|
+
}
|
|
84
|
+
return hexString;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Convert from a base 64 encoded to a standard UUID.
|
|
88
|
+
*
|
|
89
|
+
* NOTE: This method does not verify input is valid UUID.
|
|
90
|
+
*
|
|
91
|
+
* @param {string} data The base 64 encoded UUID.
|
|
92
|
+
* @returns {string} Returns a standard UUID.
|
|
93
|
+
*/
|
|
94
|
+
static fromBase64ToUuid(data) {
|
|
95
|
+
const hexString = Uuid4.fromBase64ToHexString(data);
|
|
96
|
+
return Uuid4.fromHexStringToUuid(hexString);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Convert from a hexadecimal encoded to a base 64 encoded UUID.
|
|
100
|
+
*
|
|
101
|
+
* NOTE: This method does not verify input is valid UUID.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} data The hexadecimal encoded UUID.
|
|
104
|
+
* @returns {string} Returns base 64 encoded UUID.
|
|
105
|
+
*/
|
|
106
|
+
static fromHexStringToBase64(data) {
|
|
107
|
+
if (32 !== data.length) {
|
|
108
|
+
const mess = `Expected hex string length of 32 characters but was given length: ${data.length}`;
|
|
109
|
+
throw new RangeError(mess);
|
|
110
|
+
}
|
|
111
|
+
let pieces = [0, 4, 8, 12, 16, 20, 24, 28];
|
|
112
|
+
let binString = '0000';
|
|
113
|
+
for (const piece of pieces) {
|
|
114
|
+
binString += parseInt(data.substr(piece, 4), 16)
|
|
115
|
+
.toString(2)
|
|
116
|
+
.padStart(16, '0');
|
|
117
|
+
}
|
|
118
|
+
let result = '';
|
|
119
|
+
pieces = [
|
|
120
|
+
0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102,
|
|
121
|
+
108, 114, 120, 126,
|
|
122
|
+
];
|
|
123
|
+
for (const piece of pieces) {
|
|
124
|
+
result += Uuid4._base64[binString.substr(piece, 6)];
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Convert from a hexadecimal encoded to a standard UUID.
|
|
130
|
+
*
|
|
131
|
+
* NOTE: This method does not verify input is valid UUID.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} data The hexadecimal encoded UUID.
|
|
134
|
+
* @returns {string} Returns a standard UUID.
|
|
135
|
+
*/
|
|
136
|
+
static fromHexStringToUuid(data) {
|
|
137
|
+
if (32 !== data.length) {
|
|
138
|
+
const mess = `Expected hex string length of 32 characters but was given length: ${data.length}`;
|
|
139
|
+
throw new RangeError(mess);
|
|
140
|
+
}
|
|
141
|
+
const pieces = [8, 4, 4, 4, 12];
|
|
142
|
+
let start = 0;
|
|
143
|
+
let hexArray = [];
|
|
144
|
+
for (const piece of pieces) {
|
|
145
|
+
hexArray.push(data.substr(start, piece));
|
|
146
|
+
start += piece;
|
|
147
|
+
}
|
|
148
|
+
return hexArray.join('-');
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Convert from a standard UUID to a base 64 encoded UUID.
|
|
152
|
+
*
|
|
153
|
+
* NOTE: This method does not verify input is valid UUID.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} data The standard UUID.
|
|
156
|
+
* @returns {string} Returns base 64 encoded UUID.
|
|
157
|
+
*/
|
|
158
|
+
static fromUuidToBase64(data) {
|
|
159
|
+
const hexString = data.replace(/-/g, '');
|
|
160
|
+
return Uuid4.fromHexStringToBase64(hexString);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Convert from a standard UUID to a hexadecimal encoded UUID.
|
|
164
|
+
*
|
|
165
|
+
* NOTE: This method does not verify input is valid UUID.
|
|
166
|
+
*
|
|
167
|
+
* @param {string} data The standard UUID.
|
|
168
|
+
* @returns {string} Returns a hexadecimal encoded UUID.
|
|
169
|
+
*/
|
|
170
|
+
static fromUuidToHexString(data) {
|
|
171
|
+
return data.replace(/-/g, '');
|
|
172
|
+
}
|
|
173
|
+
get [Symbol.toStringTag]() {
|
|
174
|
+
return 'Uuid4';
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Helper method for the common parts of creating new UUID encoded as a binary string.
|
|
178
|
+
*
|
|
179
|
+
* @param {Uint8Array} [data] Should normally be `undefined` to create a
|
|
180
|
+
* truly random v4 UUID.
|
|
181
|
+
* @returns {string} Returns an UUID encoded as a binary string.
|
|
182
|
+
* @private
|
|
183
|
+
*/
|
|
184
|
+
static _asBinString(data) {
|
|
185
|
+
let binArray = data !== null && data !== void 0 ? data : Uuid4._getRandomArray();
|
|
186
|
+
if (16 !== binArray.length) {
|
|
187
|
+
const mess = `Expected data array length of 16 but was given length: ${binArray.length}`;
|
|
188
|
+
throw new RangeError(mess);
|
|
189
|
+
}
|
|
190
|
+
binArray[6] = (binArray[6] & 0x0f) | 0x40;
|
|
191
|
+
binArray[8] = (binArray[8] & 0x3f) | 0x80;
|
|
192
|
+
let binary = '';
|
|
193
|
+
for (const piece of binArray) {
|
|
194
|
+
binary += piece.toString(2).padStart(8, '0');
|
|
195
|
+
}
|
|
196
|
+
return binary;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Used to paste over differences in browser vs node secure random number generation.
|
|
200
|
+
*
|
|
201
|
+
* @returns {Uint8Array} Returns a new random number filled Uint8Array.
|
|
202
|
+
* @private
|
|
203
|
+
*/
|
|
204
|
+
static _getRandomArray() {
|
|
205
|
+
if (typeof window === 'undefined') {
|
|
206
|
+
let crypto = require('crypto');
|
|
207
|
+
return Uint8Array.from(crypto.randomBytes(16));
|
|
208
|
+
}
|
|
209
|
+
// `result` is modified in place.
|
|
210
|
+
let result = new Uint8Array(16);
|
|
211
|
+
window.crypto.getRandomValues(result);
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Used in mapping from binary to base 64 during encoding.
|
|
217
|
+
*
|
|
218
|
+
* @type {object}
|
|
219
|
+
* @private
|
|
220
|
+
*/
|
|
221
|
+
Uuid4._base64 = {
|
|
222
|
+
'000000': 'A',
|
|
223
|
+
'000001': 'B',
|
|
224
|
+
'000010': 'C',
|
|
225
|
+
'000011': 'D',
|
|
226
|
+
'000100': 'E',
|
|
227
|
+
'000101': 'F',
|
|
228
|
+
'000110': 'G',
|
|
229
|
+
'000111': 'H',
|
|
230
|
+
'001000': 'I',
|
|
231
|
+
'001001': 'J',
|
|
232
|
+
'001010': 'K',
|
|
233
|
+
'001011': 'L',
|
|
234
|
+
'001100': 'M',
|
|
235
|
+
'001101': 'N',
|
|
236
|
+
'001110': 'O',
|
|
237
|
+
'001111': 'P',
|
|
238
|
+
'010000': 'Q',
|
|
239
|
+
'010001': 'R',
|
|
240
|
+
'010010': 'S',
|
|
241
|
+
'010011': 'T',
|
|
242
|
+
'010100': 'U',
|
|
243
|
+
'010101': 'V',
|
|
244
|
+
'010110': 'W',
|
|
245
|
+
'010111': 'X',
|
|
246
|
+
'011000': 'Y',
|
|
247
|
+
'011001': 'Z',
|
|
248
|
+
'011010': 'a',
|
|
249
|
+
'011011': 'b',
|
|
250
|
+
'011100': 'c',
|
|
251
|
+
'011101': 'd',
|
|
252
|
+
'011110': 'e',
|
|
253
|
+
'011111': 'f',
|
|
254
|
+
'100000': 'g',
|
|
255
|
+
'100001': 'h',
|
|
256
|
+
'100010': 'i',
|
|
257
|
+
'100011': 'j',
|
|
258
|
+
'100100': 'k',
|
|
259
|
+
'100101': 'l',
|
|
260
|
+
'100110': 'm',
|
|
261
|
+
'100111': 'n',
|
|
262
|
+
'101000': 'o',
|
|
263
|
+
'101001': 'p',
|
|
264
|
+
'101010': 'q',
|
|
265
|
+
'101011': 'r',
|
|
266
|
+
'101100': 's',
|
|
267
|
+
'101101': 't',
|
|
268
|
+
'101110': 'u',
|
|
269
|
+
'101111': 'v',
|
|
270
|
+
'110000': 'w',
|
|
271
|
+
'110001': 'x',
|
|
272
|
+
'110010': 'y',
|
|
273
|
+
'110011': 'z',
|
|
274
|
+
'110100': '0',
|
|
275
|
+
'110101': '1',
|
|
276
|
+
'110110': '2',
|
|
277
|
+
'110111': '3',
|
|
278
|
+
'111000': '4',
|
|
279
|
+
'111001': '5',
|
|
280
|
+
'111010': '6',
|
|
281
|
+
'111011': '7',
|
|
282
|
+
'111100': '8',
|
|
283
|
+
'111101': '9',
|
|
284
|
+
'111110': '-',
|
|
285
|
+
'111111': '_',
|
|
286
|
+
};
|
|
287
|
+
exports.default = Uuid4;
|
package/utils/uuid4.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
class Uuid4 {
|
|
4
2
|
/**
|
|
5
3
|
* Generate a custom base 64 encoded UUID v4 (random).
|
|
@@ -284,4 +282,4 @@ Uuid4._base64 = {
|
|
|
284
282
|
'111110': '-',
|
|
285
283
|
'111111': '_',
|
|
286
284
|
};
|
|
287
|
-
|
|
285
|
+
export default Uuid4;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wordsToNumber = void 0;
|
|
4
|
+
const Small = {
|
|
5
|
+
zero: 0,
|
|
6
|
+
one: 1,
|
|
7
|
+
two: 2,
|
|
8
|
+
three: 3,
|
|
9
|
+
four: 4,
|
|
10
|
+
five: 5,
|
|
11
|
+
six: 6,
|
|
12
|
+
seven: 7,
|
|
13
|
+
eight: 8,
|
|
14
|
+
nine: 9,
|
|
15
|
+
ten: 10,
|
|
16
|
+
eleven: 11,
|
|
17
|
+
twelve: 12,
|
|
18
|
+
thirteen: 13,
|
|
19
|
+
fourteen: 14,
|
|
20
|
+
fifteen: 15,
|
|
21
|
+
sixteen: 16,
|
|
22
|
+
seventeen: 17,
|
|
23
|
+
eighteen: 18,
|
|
24
|
+
nineteen: 19,
|
|
25
|
+
twenty: 20,
|
|
26
|
+
thirty: 30,
|
|
27
|
+
forty: 40,
|
|
28
|
+
fifty: 50,
|
|
29
|
+
sixty: 60,
|
|
30
|
+
seventy: 70,
|
|
31
|
+
eighty: 80,
|
|
32
|
+
ninety: 90,
|
|
33
|
+
};
|
|
34
|
+
function wordsToNumber(s, checkForDigits = false) {
|
|
35
|
+
const words = s.toString().split(/[\s-]+/);
|
|
36
|
+
let total = 0;
|
|
37
|
+
words.forEach((word) => {
|
|
38
|
+
const wordKey = Small[word];
|
|
39
|
+
if (wordKey !== undefined) {
|
|
40
|
+
total += wordKey;
|
|
41
|
+
}
|
|
42
|
+
else if (checkForDigits && !isNaN(parseFloat(word))) {
|
|
43
|
+
total += parseFloat(word);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return total;
|
|
47
|
+
}
|
|
48
|
+
exports.wordsToNumber = wordsToNumber;
|
package/utils/wordsToNumber.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wordsToNumber = void 0;
|
|
4
1
|
const Small = {
|
|
5
2
|
zero: 0,
|
|
6
3
|
one: 1,
|
|
@@ -31,7 +28,7 @@ const Small = {
|
|
|
31
28
|
eighty: 80,
|
|
32
29
|
ninety: 90,
|
|
33
30
|
};
|
|
34
|
-
function wordsToNumber(s, checkForDigits = false) {
|
|
31
|
+
export function wordsToNumber(s, checkForDigits = false) {
|
|
35
32
|
const words = s.toString().split(/[\s-]+/);
|
|
36
33
|
let total = 0;
|
|
37
34
|
words.forEach((word) => {
|
|
@@ -45,4 +42,3 @@ function wordsToNumber(s, checkForDigits = false) {
|
|
|
45
42
|
});
|
|
46
43
|
return total;
|
|
47
44
|
}
|
|
48
|
-
exports.wordsToNumber = wordsToNumber;
|