2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

feat: util to check if pesa, also fix indents

This commit is contained in:
18alantom 2021-12-27 13:13:38 +05:30
parent ac642b205b
commit 4b0cf18b38

View File

@ -1,12 +1,20 @@
Array.prototype.equals = function( array ) {
return this.length == array.length &&
this.every( function(item,i) { return item == array[i] } );
}
const { pesa } = require('pesa');
Array.prototype.equals = function (array) {
return (
this.length == array.length &&
this.every(function (item, i) {
return item == array[i];
})
);
};
function slug(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
})
.replace(/\s+/g, '');
}
function getRandomString() {
@ -14,7 +22,7 @@ function getRandomString() {
}
async function sleep(seconds) {
return new Promise(resolve => {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
@ -29,18 +37,16 @@ function stringReplace(str, args) {
args = [args];
}
if(str==undefined) return str;
if (str == undefined) return str;
let unkeyed_index = 0;
return str.replace(/\{(\w*)\}/g, (match, key) => {
if (key === '') {
key = unkeyed_index;
unkeyed_index++
unkeyed_index++;
}
if (key == +key) {
return args[key] !== undefined
? args[key]
: match;
return args[key] !== undefined ? args[key] : match;
}
});
}
@ -49,19 +55,21 @@ function getQueryString(params) {
if (!params) return '';
let parts = [];
for (let key in params) {
if (key!=null && params[key]!=null) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
if (key != null && params[key] != null) {
parts.push(
encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
);
}
}
return parts.join('&');
}
function asyncHandler(fn) {
return (req, res, next) => Promise.resolve(fn(req, res, next))
.catch((err) => {
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});
res.status(err.statusCode || 500).send({ error: err.message });
});
}
@ -70,12 +78,12 @@ function asyncHandler(fn) {
* @param {Number} n
*/
function range(n) {
return Array.from(Array(4)).map((d, i) => i)
return Array.from(Array(4)).map((d, i) => i);
}
function unique(list, key = it => it) {
function unique(list, key = (it) => it) {
var seen = {};
return list.filter(item => {
return list.filter((item) => {
var k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
});
@ -96,6 +104,10 @@ function getDuplicates(array) {
return duplicates;
}
function isPesa(value) {
return value instanceof pesa().constructor;
}
module.exports = {
_,
slug,
@ -106,5 +118,6 @@ module.exports = {
asyncHandler,
range,
unique,
getDuplicates
}
getDuplicates,
isPesa,
};