forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
37 lines (29 loc) · 1.05 KB
/
utils.js
File metadata and controls
37 lines (29 loc) · 1.05 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
/* @flow */
function objectToList(o: Object): Array<any> {
let l = [];
Object.keys(o).forEach((key) => l.push(key));
return l;
}
function encodeString(input: string): string {
return encodeURIComponent(input).replace(/[!~*'()]/g, (x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
}
function objectToListSorted(o: Object): Array<any> {
return objectToList(o).sort();
}
function signPamFromParams(params: Object): string {
let l = objectToListSorted(params);
return l.map((paramKey) => `${paramKey}=${encodeString(params[paramKey])}`).join('&');
}
function endsWith(searchString: string, suffix: string): boolean {
return searchString.indexOf(suffix, this.length - suffix.length) !== -1;
}
function createPromise() {
let successResolve;
let failureResolve;
let promise: Promise<any> = new Promise((fulfill, reject) => {
successResolve = fulfill;
failureResolve = reject;
});
return { promise, reject: failureResolve, fulfill: successResolve };
}
module.exports = { signPamFromParams, endsWith, createPromise, encodeString };