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

incr: remove utils, format from common

This commit is contained in:
18alantom 2022-03-21 11:28:52 +05:30
parent f243326126
commit 807a5d2da7
4 changed files with 61 additions and 86 deletions

View File

@ -1,11 +1,7 @@
export default async function initLibs(frappe) {
const utils = await import('../utils');
const format = await import('../utils/format');
const BaseMeta = await import('frappe/model/meta');
const BaseDocument = await import('frappe/model/document');
Object.assign(frappe, utils.default);
Object.assign(frappe, format.default);
frappe.BaseDocument = BaseDocument.default;
frappe.BaseMeta = BaseMeta.default;
}

View File

@ -2,17 +2,21 @@ import initLibs from 'frappe/common';
import { getMoneyMaker } from 'pesa';
import { markRaw } from 'vue';
import * as errors from './common/errors';
import utils from './utils';
import { asyncHandler, getDuplicates, getRandomString } from './utils';
import {
DEFAULT_DISPLAY_PRECISION,
DEFAULT_INTERNAL_PRECISION,
} from './utils/consts';
import { format } from './utils/format';
import Observable from './utils/observable';
import { t, T } from './utils/translation';
class Frappe {
t = t;
T = T;
format = format;
getRandomString = getRandomString;
errors = errors;
isElectron = false;
isServer = false;
@ -114,7 +118,7 @@ class Frappe {
let fieldnames = (metaDefinition.fields || [])
.map((df) => df.fieldname)
.sort();
let duplicateFieldnames = utils.getDuplicates(fieldnames);
let duplicateFieldnames = getDuplicates(fieldnames);
if (duplicateFieldnames.length > 0) {
throw new Error(
`Duplicate fields in ${doctype}: ${duplicateFieldnames.join(', ')}`
@ -144,7 +148,7 @@ class Frappe {
// add to router if client-server
this.app.post(
`/api/method/${method}`,
this.asyncHandler(async function (request, response) {
asyncHandler(async function (request, response) {
let data = await handler(request.body);
if (data === undefined) {
data = {};

View File

@ -2,8 +2,7 @@ import frappe from 'frappe';
import { DateTime } from 'luxon';
import { DEFAULT_DISPLAY_PRECISION, DEFAULT_LOCALE } from './consts';
export default {
format(value, df, doc) {
export function format(value, df, doc) {
if (!df) {
return value;
}
@ -47,10 +46,7 @@ export default {
}
}
return value;
},
formatCurrency,
formatNumber,
};
}
function formatCurrency(value, currency) {
let valueString;

View File

@ -1,15 +1,6 @@
const { pesa } = require('pesa');
import { pesa } from 'pesa';
Array.prototype.equals = function (array) {
return (
this.length == array.length &&
this.every(function (item, i) {
return item == array[i];
})
);
};
function slug(str) {
export function slug(str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
@ -17,17 +8,17 @@ function slug(str) {
.replace(/\s+/g, '');
}
function getRandomString() {
export function getRandomString() {
return Math.random().toString(36).substr(3);
}
async function sleep(seconds) {
export async function sleep(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
function getQueryString(params) {
export function getQueryString(params) {
if (!params) return '';
let parts = [];
for (let key in params) {
@ -40,7 +31,7 @@ function getQueryString(params) {
return parts.join('&');
}
function asyncHandler(fn) {
export function asyncHandler(fn) {
return (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch((err) => {
console.log(err);
@ -53,13 +44,13 @@ function asyncHandler(fn) {
* Returns array from 0 to n - 1
* @param {Number} n
*/
function range(n) {
export function range(n) {
return Array(n)
.fill()
.map((_, i) => i);
}
function unique(list, key = (it) => it) {
export function unique(list, key = (it) => it) {
var seen = {};
return list.filter((item) => {
var k = key(item);
@ -67,7 +58,7 @@ function unique(list, key = (it) => it) {
});
}
function getDuplicates(array) {
export function getDuplicates(array) {
let duplicates = [];
for (let i in array) {
let previous = array[i - 1];
@ -82,18 +73,6 @@ function getDuplicates(array) {
return duplicates;
}
function isPesa(value) {
export function isPesa(value) {
return value instanceof pesa().constructor;
}
module.exports = {
slug,
getRandomString,
sleep,
getQueryString,
asyncHandler,
range,
unique,
getDuplicates,
isPesa,
};