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) { 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 BaseMeta = await import('frappe/model/meta');
const BaseDocument = await import('frappe/model/document'); const BaseDocument = await import('frappe/model/document');
Object.assign(frappe, utils.default);
Object.assign(frappe, format.default);
frappe.BaseDocument = BaseDocument.default; frappe.BaseDocument = BaseDocument.default;
frappe.BaseMeta = BaseMeta.default; frappe.BaseMeta = BaseMeta.default;
} }

View File

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

View File

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

View File

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