2022-03-21 11:28:52 +05:30
|
|
|
import { pesa } from 'pesa';
|
2021-12-27 13:13:38 +05:30
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function slug(str) {
|
2021-12-27 13:13:38 +05:30
|
|
|
return str
|
|
|
|
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
|
|
|
|
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
|
|
|
|
})
|
|
|
|
.replace(/\s+/g, '');
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function getRandomString() {
|
2021-12-27 13:13:38 +05:30
|
|
|
return Math.random().toString(36).substr(3);
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2018-02-01 16:37:36 +05:30
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export async function sleep(seconds) {
|
2021-12-27 13:13:38 +05:30
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, seconds * 1000);
|
|
|
|
});
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2018-02-07 18:53:52 +05:30
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function getQueryString(params) {
|
2021-12-27 13:13:38 +05:30
|
|
|
if (!params) return '';
|
|
|
|
let parts = [];
|
|
|
|
for (let key in params) {
|
|
|
|
if (key != null && params[key] != null) {
|
|
|
|
parts.push(
|
|
|
|
encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
|
|
|
|
);
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2021-12-27 13:13:38 +05:30
|
|
|
}
|
|
|
|
return parts.join('&');
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2018-03-26 17:48:07 +05:30
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function asyncHandler(fn) {
|
2021-12-27 13:13:38 +05:30
|
|
|
return (req, res, next) =>
|
|
|
|
Promise.resolve(fn(req, res, next)).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
// handle error
|
|
|
|
res.status(err.statusCode || 500).send({ error: err.message });
|
|
|
|
});
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2018-01-25 15:34:48 +05:30
|
|
|
|
2018-06-24 11:22:23 +05:30
|
|
|
/**
|
|
|
|
* Returns array from 0 to n - 1
|
|
|
|
* @param {Number} n
|
|
|
|
*/
|
2022-03-21 11:28:52 +05:30
|
|
|
export function range(n) {
|
2022-01-20 14:33:41 +05:30
|
|
|
return Array(n)
|
|
|
|
.fill()
|
|
|
|
.map((_, i) => i);
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
2018-04-19 20:01:17 +05:30
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function unique(list, key = (it) => it) {
|
2021-12-27 13:13:38 +05:30
|
|
|
var seen = {};
|
|
|
|
return list.filter((item) => {
|
|
|
|
var k = key(item);
|
|
|
|
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
|
|
|
|
});
|
2018-06-24 11:22:23 +05:30
|
|
|
}
|
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function getDuplicates(array) {
|
2019-12-18 23:48:53 +05:30
|
|
|
let duplicates = [];
|
|
|
|
for (let i in array) {
|
|
|
|
let previous = array[i - 1];
|
|
|
|
let current = array[i];
|
|
|
|
|
|
|
|
if (current === previous) {
|
|
|
|
if (!duplicates.includes(current)) {
|
|
|
|
duplicates.push(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return duplicates;
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:28:52 +05:30
|
|
|
export function isPesa(value) {
|
2021-12-27 13:13:38 +05:30
|
|
|
return value instanceof pesa().constructor;
|
2019-12-18 23:48:53 +05:30
|
|
|
}
|