2017-07-22 21:27:38 +05:30
|
|
|
// @flow
|
2016-05-02 11:21:47 +05:30
|
|
|
let lastId = 0;
|
2017-07-22 21:27:38 +05:30
|
|
|
export function uniqueId(prefix: string = 'id'): string {
|
2019-11-27 14:33:32 +05:30
|
|
|
return `${prefix}${++lastId}`;
|
2016-05-02 11:21:47 +05:30
|
|
|
}
|
2016-07-29 22:44:52 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} obj
|
2019-11-27 14:33:32 +05:30
|
|
|
* @param {Array} keys
|
2016-07-29 22:44:52 +05:30
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object}
|
2016-07-29 22:44:52 +05:30
|
|
|
*/
|
2017-07-22 21:27:38 +05:30
|
|
|
export function omit(obj: Object, keys: Array<string>): Object {
|
2019-11-27 14:33:32 +05:30
|
|
|
const newObj = { ...obj };
|
2016-07-29 22:44:52 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
keys.forEach(key => {
|
|
|
|
Reflect.deleteProperty(newObj, key);
|
|
|
|
});
|
2016-07-29 22:44:52 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return newObj;
|
2016-07-29 22:44:52 +05:30
|
|
|
}
|
2016-07-31 19:23:16 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously loads script
|
|
|
|
*
|
|
|
|
* @param {string} src
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Promise}
|
2016-07-31 19:23:16 +05:30
|
|
|
*/
|
2017-07-22 21:27:38 +05:30
|
|
|
export function loadScript(src: string): Promise<*> {
|
2019-11-27 14:33:32 +05:30
|
|
|
const script = document.createElement('script');
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
script.async = true;
|
|
|
|
script.defer = true;
|
|
|
|
script.src = src;
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
script.onload = resolve;
|
|
|
|
script.onerror = reject;
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (document && document.body) {
|
|
|
|
document.body.appendChild(script);
|
|
|
|
}
|
|
|
|
});
|
2016-07-31 19:23:16 +05:30
|
|
|
}
|
2016-08-14 14:31:04 +05:30
|
|
|
|
2017-01-03 11:26:38 +05:30
|
|
|
/**
|
|
|
|
* Returns a function, that, as long as it continues to be invoked, will not
|
|
|
|
* be triggered. The function will be called after it stops being called for
|
|
|
|
* N milliseconds. If `immediate` is passed, trigger the function on the
|
|
|
|
* leading edge, instead of the trailing. The function also has a property 'clear'
|
|
|
|
* that is a function which will clear the timer to prevent previously scheduled executions.
|
|
|
|
*
|
|
|
|
* @source https://github.com/component/debounce
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @param {Function} function - function to wrap
|
2017-01-03 11:26:38 +05:30
|
|
|
* @param {number} [timeout=100] - timeout in ms
|
|
|
|
* @param {bool} [immediate=false] - whether to execute at the beginning
|
|
|
|
*/
|
2017-07-22 21:27:38 +05:30
|
|
|
export { default as debounce } from 'debounce';
|
2017-01-06 11:04:39 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} jwt
|
|
|
|
*
|
|
|
|
* @throws {Error} If can not decode token
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object} - decoded jwt payload
|
2017-01-06 11:04:39 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export function getJwtPayloads(
|
|
|
|
jwt: string,
|
|
|
|
): {
|
|
|
|
sub: string,
|
|
|
|
jti: number,
|
|
|
|
exp: number,
|
2019-05-20 19:02:07 +05:30
|
|
|
} {
|
2019-11-27 14:33:32 +05:30
|
|
|
const parts = (jwt || '').split('.');
|
2017-01-06 11:04:39 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (parts.length !== 3) {
|
|
|
|
throw new Error('Invalid jwt token');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(atob(parts[1]));
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error('Can not decode jwt token');
|
|
|
|
}
|
2017-01-06 11:04:39 +05:30
|
|
|
}
|