mirror of
https://github.com/frappe/books.git
synced 2024-11-09 23:30:56 +00:00
whitespace
This commit is contained in:
parent
f25c4ab81b
commit
077d098599
8
cli.js
8
cli.js
@ -5,9 +5,9 @@ const process = require('process');
|
||||
const boilerplate = require('frappejs/model/boilerplate');
|
||||
|
||||
program.command('new-model <name>')
|
||||
.description('Create a new model in the `models/doctype` folder')
|
||||
.action((name) => {
|
||||
boilerplate.make_model_files(name);
|
||||
});
|
||||
.description('Create a new model in the `models/doctype` folder')
|
||||
.action((name) => {
|
||||
boilerplate.make_model_files(name);
|
||||
});
|
||||
|
||||
program.parse(process.argv);
|
@ -2,12 +2,12 @@ const FloatControl = require('./float');
|
||||
const frappe = require('frappejs');
|
||||
|
||||
class CurrencyControl extends FloatControl {
|
||||
parse(value) {
|
||||
return frappe.parse_number(value);
|
||||
}
|
||||
format(value) {
|
||||
return frappe.format_number(value);
|
||||
}
|
||||
parse(value) {
|
||||
return frappe.parse_number(value);
|
||||
}
|
||||
format(value) {
|
||||
return frappe.format_number(value);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = CurrencyControl;
|
@ -3,7 +3,7 @@ const BaseControl = require('./base');
|
||||
class FloatControl extends BaseControl {
|
||||
make() {
|
||||
super.make();
|
||||
this.input.setAttribute('type', 'text');
|
||||
this.input.setAttribute('type', 'text');
|
||||
this.input.classList.add('text-right');
|
||||
this.input.addEventListener('focus', () => {
|
||||
setTimeout(() => {
|
||||
|
@ -1,21 +1,21 @@
|
||||
class BaseError extends Error {
|
||||
constructor(status_code, ...params) {
|
||||
super(...params);
|
||||
this.status_code = status_code;
|
||||
}
|
||||
constructor(status_code, ...params) {
|
||||
super(...params);
|
||||
this.status_code = status_code;
|
||||
}
|
||||
}
|
||||
|
||||
class ValidationError extends BaseError {
|
||||
constructor(...params) { super(417, ...params); }
|
||||
constructor(...params) { super(417, ...params); }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ValidationError: ValidationError,
|
||||
ValueError: class ValueError extends ValidationError { },
|
||||
NotFound: class NotFound extends BaseError {
|
||||
constructor(...params) { super(404, ...params); }
|
||||
},
|
||||
Forbidden: class Forbidden extends BaseError {
|
||||
constructor(...params) { super(403, ...params); }
|
||||
},
|
||||
ValidationError: ValidationError,
|
||||
ValueError: class ValueError extends ValidationError { },
|
||||
NotFound: class NotFound extends BaseError {
|
||||
constructor(...params) { super(404, ...params); }
|
||||
},
|
||||
Forbidden: class Forbidden extends BaseError {
|
||||
constructor(...params) { super(403, ...params); }
|
||||
},
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
module.exports = {
|
||||
input: './src/index.js',
|
||||
output: {
|
||||
file: './dist/js/bundle.js',
|
||||
format: 'iife',
|
||||
name: 'desk',
|
||||
globals: ['io', 'nunjucks'] // for socketio client, which is imported directly
|
||||
},
|
||||
plugins: [
|
||||
require('rollup-plugin-commonjs')(),
|
||||
require('rollup-plugin-json')(),
|
||||
require('rollup-plugin-node-resolve')({
|
||||
preferBuiltins: true
|
||||
}),
|
||||
],
|
||||
input: './src/index.js',
|
||||
output: {
|
||||
file: './dist/js/bundle.js',
|
||||
format: 'iife',
|
||||
name: 'desk',
|
||||
globals: ['io', 'nunjucks'] // for socketio client, which is imported directly
|
||||
},
|
||||
plugins: [
|
||||
require('rollup-plugin-commonjs')(),
|
||||
require('rollup-plugin-json')(),
|
||||
require('rollup-plugin-node-resolve')({
|
||||
preferBuiltins: true
|
||||
}),
|
||||
],
|
||||
}
|
@ -247,7 +247,7 @@ module.exports = class BaseDocument extends Observable {
|
||||
|
||||
// helper functions
|
||||
getSum(tablefield, childfield) {
|
||||
return this[tablefield].map(d => (d[childfield] || 0)).reduce((a, b) => a + b, 0);
|
||||
return this[tablefield].map(d => (d[childfield] || 0)).reduce((a, b) => a + b, 0);
|
||||
}
|
||||
|
||||
async getFrom(doctype, name, fieldname) {
|
||||
|
@ -1,22 +1,22 @@
|
||||
module.exports = {
|
||||
"name": "NumberSeries",
|
||||
"documentClass": require('./NumberSeriesDocument.js'),
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "name",
|
||||
"label": "Name",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "current",
|
||||
"label": "Current",
|
||||
"fieldtype": "Int",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
"name": "NumberSeries",
|
||||
"documentClass": require('./NumberSeriesDocument.js'),
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "name",
|
||||
"label": "Name",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "current",
|
||||
"label": "Current",
|
||||
"fieldtype": "Int",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
const BaseDocument = require('frappejs/model/document');
|
||||
|
||||
module.exports = class NumberSeries extends BaseDocument {
|
||||
validate() {
|
||||
if (this.current===null || this.current===undefined) {
|
||||
this.current = 0;
|
||||
}
|
||||
}
|
||||
async next() {
|
||||
this.validate();
|
||||
this.current++;
|
||||
await this.update();
|
||||
return this.current;
|
||||
}
|
||||
validate() {
|
||||
if (this.current===null || this.current===undefined) {
|
||||
this.current = 0;
|
||||
}
|
||||
}
|
||||
async next() {
|
||||
this.validate();
|
||||
this.current++;
|
||||
await this.update();
|
||||
return this.current;
|
||||
}
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
module.exports = {
|
||||
"name": "Role",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "name",
|
||||
"label": "Name",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
"name": "Role",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "name",
|
||||
"label": "Name",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
}
|
@ -1,21 +1,21 @@
|
||||
module.exports = {
|
||||
"name": "Session",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "username",
|
||||
"label": "Username",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "password",
|
||||
"label": "Password",
|
||||
"fieldtype": "Password",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
"name": "Session",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "username",
|
||||
"label": "Username",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "password",
|
||||
"label": "Password",
|
||||
"fieldtype": "Password",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
}
|
@ -1,27 +1,27 @@
|
||||
module.exports = {
|
||||
"name": "SingleValue",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "parent",
|
||||
"label": "Parent",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "fieldname",
|
||||
"label": "Fieldname",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "value",
|
||||
"label": "Value",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
"name": "SingleValue",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "parent",
|
||||
"label": "Parent",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "fieldname",
|
||||
"label": "Fieldname",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "value",
|
||||
"label": "Value",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
}
|
||||
]
|
||||
}
|
@ -1,24 +1,24 @@
|
||||
module.exports = {
|
||||
name: "SystemSettings",
|
||||
label: "System Settings",
|
||||
doctype: "DocType",
|
||||
isSingle: 1,
|
||||
isChild: 0,
|
||||
keywordFields: [],
|
||||
fields: [
|
||||
{
|
||||
fieldname: "dateFormat",
|
||||
label: "Date Format",
|
||||
fieldtype: "Select",
|
||||
options: [
|
||||
"dd/mm/yyyy",
|
||||
"mm/dd/yyyy",
|
||||
"dd-mm-yyyy",
|
||||
"mm-dd-yyyy",
|
||||
"yyyy-mm-dd"
|
||||
],
|
||||
default: "yyyy-mm-dd",
|
||||
required: 1
|
||||
}
|
||||
]
|
||||
name: "SystemSettings",
|
||||
label: "System Settings",
|
||||
doctype: "DocType",
|
||||
isSingle: 1,
|
||||
isChild: 0,
|
||||
keywordFields: [],
|
||||
fields: [
|
||||
{
|
||||
fieldname: "dateFormat",
|
||||
label: "Date Format",
|
||||
fieldtype: "Select",
|
||||
options: [
|
||||
"dd/mm/yyyy",
|
||||
"mm/dd/yyyy",
|
||||
"dd-mm-yyyy",
|
||||
"mm-dd-yyyy",
|
||||
"yyyy-mm-dd"
|
||||
],
|
||||
default: "yyyy-mm-dd",
|
||||
required: 1
|
||||
}
|
||||
]
|
||||
}
|
@ -1,34 +1,34 @@
|
||||
module.exports = {
|
||||
"autoname": "random",
|
||||
"name": "ToDo",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"keywordFields": [
|
||||
"subject",
|
||||
"description"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "subject",
|
||||
"label": "Subject",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "status",
|
||||
"label": "Status",
|
||||
"fieldtype": "Select",
|
||||
"options": [
|
||||
"Open",
|
||||
"Closed"
|
||||
],
|
||||
"default": "Open",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "description",
|
||||
"label": "Description",
|
||||
"fieldtype": "Text"
|
||||
}
|
||||
]
|
||||
"autoname": "random",
|
||||
"name": "ToDo",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"keywordFields": [
|
||||
"subject",
|
||||
"description"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "subject",
|
||||
"label": "Subject",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "status",
|
||||
"label": "Status",
|
||||
"fieldtype": "Select",
|
||||
"options": [
|
||||
"Open",
|
||||
"Closed"
|
||||
],
|
||||
"default": "Open",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "description",
|
||||
"label": "Description",
|
||||
"fieldtype": "Text"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,18 +1,18 @@
|
||||
module.exports = {
|
||||
"name": "User",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [
|
||||
"name": "User",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 0,
|
||||
"keywordFields": [
|
||||
"name",
|
||||
"full_name"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "name",
|
||||
"label": "Name",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "name",
|
||||
"label": "Name",
|
||||
"fieldtype": "Data",
|
||||
"required": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "full_name",
|
||||
@ -26,5 +26,5 @@ module.exports = {
|
||||
"fieldtype": "Table",
|
||||
"childtype": "UserRole"
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
module.exports = {
|
||||
"name": "UserRole",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 1,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "role",
|
||||
"label": "Role",
|
||||
"fieldtype": "Link",
|
||||
"target": "Role"
|
||||
"name": "UserRole",
|
||||
"doctype": "DocType",
|
||||
"isSingle": 0,
|
||||
"isChild": 1,
|
||||
"keywordFields": [],
|
||||
"fields": [
|
||||
{
|
||||
"fieldname": "role",
|
||||
"label": "Role",
|
||||
"fieldtype": "Link",
|
||||
"target": "Role"
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
@ -47,13 +47,13 @@ describe('Document', () => {
|
||||
});
|
||||
|
||||
it('should not allow incorrect Select option', async () => {
|
||||
let doc = test_doc();
|
||||
try {
|
||||
await doc.set('status', 'Illegal');
|
||||
assert.fail();
|
||||
} catch (e) {
|
||||
assert.ok(e instanceof frappe.errors.ValueError);
|
||||
}
|
||||
let doc = test_doc();
|
||||
try {
|
||||
await doc.set('status', 'Illegal');
|
||||
assert.fail();
|
||||
} catch (e) {
|
||||
assert.ok(e instanceof frappe.errors.ValueError);
|
||||
}
|
||||
});
|
||||
|
||||
it('should delete a document', async () => {
|
||||
|
@ -2,7 +2,7 @@
|
||||
// server.start({
|
||||
// backend: 'mysql',
|
||||
// connectionParams: {
|
||||
// host : "test_frappejs",
|
||||
// host : "test_frappejs",
|
||||
// username : "test_frappejs",
|
||||
// password : "test_frappejs",
|
||||
// db_name : "test_frappejs"
|
||||
|
@ -2,29 +2,29 @@ const Router = require('frappejs/common/router');
|
||||
const assert = require('assert');
|
||||
|
||||
describe('Router', () => {
|
||||
it('router should sort static routes', () => {
|
||||
let router = new Router();
|
||||
router.add('/a', 'x');
|
||||
router.add('/a/b', 'y');
|
||||
router.add('/a/b/clong/', 'z');
|
||||
it('router should sort static routes', () => {
|
||||
let router = new Router();
|
||||
router.add('/a', 'x');
|
||||
router.add('/a/b', 'y');
|
||||
router.add('/a/b/clong/', 'z');
|
||||
|
||||
assert.equal(router.match('/a/b').handler, 'y');
|
||||
assert.equal(router.match('/a').handler, 'x');
|
||||
assert.equal(router.match('/a/b/clong/').handler, 'z');
|
||||
});
|
||||
assert.equal(router.match('/a/b').handler, 'y');
|
||||
assert.equal(router.match('/a').handler, 'x');
|
||||
assert.equal(router.match('/a/b/clong/').handler, 'z');
|
||||
});
|
||||
|
||||
it('router should sort dynamic routes', () => {
|
||||
let router = new Router();
|
||||
router.add('/edit/:doctype', 'catch');
|
||||
router.add('/edit/:doctype/:name', 'all');
|
||||
router.add('/edit/ToDo/:name', 'ToDo');
|
||||
router.add('/edit/ToDo/mytest', 'static');
|
||||
it('router should sort dynamic routes', () => {
|
||||
let router = new Router();
|
||||
router.add('/edit/:doctype', 'catch');
|
||||
router.add('/edit/:doctype/:name', 'all');
|
||||
router.add('/edit/ToDo/:name', 'ToDo');
|
||||
router.add('/edit/ToDo/mytest', 'static');
|
||||
|
||||
assert.equal(router.match('/edit/ToDo/test').handler, 'ToDo');
|
||||
assert.equal(router.match('/edit/user/test').handler, 'all');
|
||||
assert.equal(router.match('/edit/ToDo/mytest').handler, 'static');
|
||||
assert.equal(router.match('/edit/user').handler, 'catch');
|
||||
});
|
||||
assert.equal(router.match('/edit/ToDo/test').handler, 'ToDo');
|
||||
assert.equal(router.match('/edit/user/test').handler, 'all');
|
||||
assert.equal(router.match('/edit/ToDo/mytest').handler, 'static');
|
||||
assert.equal(router.match('/edit/user').handler, 'catch');
|
||||
});
|
||||
|
||||
|
||||
});
|
@ -2,34 +2,34 @@ const number_format = require('frappejs/utils/number_format');
|
||||
const assert = require('assert');
|
||||
|
||||
describe('Number Formatting', () => {
|
||||
it('should format numbers', () => {
|
||||
assert.equal(number_format.format_number(100), '100.00');
|
||||
assert.equal(number_format.format_number(1000), '1,000.00');
|
||||
assert.equal(number_format.format_number(10000), '10,000.00');
|
||||
assert.equal(number_format.format_number(100000), '100,000.00');
|
||||
assert.equal(number_format.format_number(1000000), '1,000,000.00');
|
||||
assert.equal(number_format.format_number(100.1234), '100.12');
|
||||
assert.equal(number_format.format_number(1000.1234), '1,000.12');
|
||||
});
|
||||
it('should format numbers', () => {
|
||||
assert.equal(number_format.format_number(100), '100.00');
|
||||
assert.equal(number_format.format_number(1000), '1,000.00');
|
||||
assert.equal(number_format.format_number(10000), '10,000.00');
|
||||
assert.equal(number_format.format_number(100000), '100,000.00');
|
||||
assert.equal(number_format.format_number(1000000), '1,000,000.00');
|
||||
assert.equal(number_format.format_number(100.1234), '100.12');
|
||||
assert.equal(number_format.format_number(1000.1234), '1,000.12');
|
||||
});
|
||||
|
||||
it('should parse numbers', () => {
|
||||
assert.equal(number_format.parse_number('100.00'), 100);
|
||||
assert.equal(number_format.parse_number('1,000.00'), 1000);
|
||||
assert.equal(number_format.parse_number('10,000.00'), 10000);
|
||||
assert.equal(number_format.parse_number('100,000.00'), 100000);
|
||||
assert.equal(number_format.parse_number('1,000,000.00'), 1000000);
|
||||
assert.equal(number_format.parse_number('100.1234'), 100.1234);
|
||||
assert.equal(number_format.parse_number('1,000.1234'), 1000.1234);
|
||||
});
|
||||
it('should parse numbers', () => {
|
||||
assert.equal(number_format.parse_number('100.00'), 100);
|
||||
assert.equal(number_format.parse_number('1,000.00'), 1000);
|
||||
assert.equal(number_format.parse_number('10,000.00'), 10000);
|
||||
assert.equal(number_format.parse_number('100,000.00'), 100000);
|
||||
assert.equal(number_format.parse_number('1,000,000.00'), 1000000);
|
||||
assert.equal(number_format.parse_number('100.1234'), 100.1234);
|
||||
assert.equal(number_format.parse_number('1,000.1234'), 1000.1234);
|
||||
});
|
||||
|
||||
it('should format lakhs and crores', () => {
|
||||
assert.equal(number_format.format_number(100, '#,##,###.##'), '100.00');
|
||||
assert.equal(number_format.format_number(1000, '#,##,###.##'), '1,000.00');
|
||||
assert.equal(number_format.format_number(10000, '#,##,###.##'), '10,000.00');
|
||||
assert.equal(number_format.format_number(100000, '#,##,###.##'), '1,00,000.00');
|
||||
assert.equal(number_format.format_number(1000000, '#,##,###.##'), '10,00,000.00');
|
||||
assert.equal(number_format.format_number(10000000, '#,##,###.##'), '1,00,00,000.00');
|
||||
assert.equal(number_format.format_number(100.1234, '#,##,###.##'), '100.12');
|
||||
assert.equal(number_format.format_number(1000.1234, '#,##,###.##'), '1,000.12');
|
||||
});
|
||||
it('should format lakhs and crores', () => {
|
||||
assert.equal(number_format.format_number(100, '#,##,###.##'), '100.00');
|
||||
assert.equal(number_format.format_number(1000, '#,##,###.##'), '1,000.00');
|
||||
assert.equal(number_format.format_number(10000, '#,##,###.##'), '10,000.00');
|
||||
assert.equal(number_format.format_number(100000, '#,##,###.##'), '1,00,000.00');
|
||||
assert.equal(number_format.format_number(1000000, '#,##,###.##'), '10,00,000.00');
|
||||
assert.equal(number_format.format_number(10000000, '#,##,###.##'), '1,00,00,000.00');
|
||||
assert.equal(number_format.format_number(100.1234, '#,##,###.##'), '100.12');
|
||||
assert.equal(number_format.format_number(1000.1234, '#,##,###.##'), '1,000.12');
|
||||
});
|
||||
});
|
@ -1,93 +1,93 @@
|
||||
const number_formats = {
|
||||
"#,###.##": { fraction_sep: ".", group_sep: ",", precision: 2 },
|
||||
"#.###,##": { fraction_sep: ",", group_sep: ".", precision: 2 },
|
||||
"# ###.##": { fraction_sep: ".", group_sep: " ", precision: 2 },
|
||||
"# ###,##": { fraction_sep: ",", group_sep: " ", precision: 2 },
|
||||
"#'###.##": { fraction_sep: ".", group_sep: "'", precision: 2 },
|
||||
"#, ###.##": { fraction_sep: ".", group_sep: ", ", precision: 2 },
|
||||
"#,##,###.##": { fraction_sep: ".", group_sep: ",", precision: 2 },
|
||||
"#,###.###": { fraction_sep: ".", group_sep: ",", precision: 3 },
|
||||
"#.###": { fraction_sep: "", group_sep: ".", precision: 0 },
|
||||
"#,###": { fraction_sep: "", group_sep: ",", precision: 0 },
|
||||
"#,###.##": { fraction_sep: ".", group_sep: ",", precision: 2 },
|
||||
"#.###,##": { fraction_sep: ",", group_sep: ".", precision: 2 },
|
||||
"# ###.##": { fraction_sep: ".", group_sep: " ", precision: 2 },
|
||||
"# ###,##": { fraction_sep: ",", group_sep: " ", precision: 2 },
|
||||
"#'###.##": { fraction_sep: ".", group_sep: "'", precision: 2 },
|
||||
"#, ###.##": { fraction_sep: ".", group_sep: ", ", precision: 2 },
|
||||
"#,##,###.##": { fraction_sep: ".", group_sep: ",", precision: 2 },
|
||||
"#,###.###": { fraction_sep: ".", group_sep: ",", precision: 3 },
|
||||
"#.###": { fraction_sep: "", group_sep: ".", precision: 0 },
|
||||
"#,###": { fraction_sep: "", group_sep: ",", precision: 0 },
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
// parse a formatted number string
|
||||
// from "4,555,000.34" -> 4555000.34
|
||||
parse_number(number, format='#,###.##') {
|
||||
if (!number) {
|
||||
return 0;
|
||||
}
|
||||
if (typeof number === 'number') {
|
||||
return number;
|
||||
}
|
||||
const info = this.get_format_info(format);
|
||||
return parseFloat(this.remove_separator(number, info.group_sep));
|
||||
},
|
||||
// parse a formatted number string
|
||||
// from "4,555,000.34" -> 4555000.34
|
||||
parse_number(number, format='#,###.##') {
|
||||
if (!number) {
|
||||
return 0;
|
||||
}
|
||||
if (typeof number === 'number') {
|
||||
return number;
|
||||
}
|
||||
const info = this.get_format_info(format);
|
||||
return parseFloat(this.remove_separator(number, info.group_sep));
|
||||
},
|
||||
|
||||
format_number(number, format = '#,###.##', precision = null) {
|
||||
if (!number) {
|
||||
number = 0;
|
||||
}
|
||||
let info = this.get_format_info(format);
|
||||
if (precision) {
|
||||
info.precision = precision;
|
||||
}
|
||||
let is_negative = false;
|
||||
format_number(number, format = '#,###.##', precision = null) {
|
||||
if (!number) {
|
||||
number = 0;
|
||||
}
|
||||
let info = this.get_format_info(format);
|
||||
if (precision) {
|
||||
info.precision = precision;
|
||||
}
|
||||
let is_negative = false;
|
||||
|
||||
number = this.parse_number(number);
|
||||
if (number < 0) {
|
||||
is_negative = true;
|
||||
}
|
||||
number = Math.abs(number);
|
||||
number = number.toFixed(info.precision);
|
||||
number = this.parse_number(number);
|
||||
if (number < 0) {
|
||||
is_negative = true;
|
||||
}
|
||||
number = Math.abs(number);
|
||||
number = number.toFixed(info.precision);
|
||||
|
||||
var parts = number.split('.');
|
||||
var parts = number.split('.');
|
||||
|
||||
// get group position and parts
|
||||
var group_position = info.group_sep ? 3 : 0;
|
||||
// get group position and parts
|
||||
var group_position = info.group_sep ? 3 : 0;
|
||||
|
||||
if (group_position) {
|
||||
var integer = parts[0];
|
||||
var str = '';
|
||||
var offset = integer.length % group_position;
|
||||
for (var i = integer.length; i >= 0; i--) {
|
||||
var l = this.remove_separator(str, info.group_sep).length;
|
||||
if (format == "#,##,###.##" && str.indexOf(",") != -1) { // INR
|
||||
group_position = 2;
|
||||
l += 1;
|
||||
}
|
||||
if (group_position) {
|
||||
var integer = parts[0];
|
||||
var str = '';
|
||||
var offset = integer.length % group_position;
|
||||
for (var i = integer.length; i >= 0; i--) {
|
||||
var l = this.remove_separator(str, info.group_sep).length;
|
||||
if (format == "#,##,###.##" && str.indexOf(",") != -1) { // INR
|
||||
group_position = 2;
|
||||
l += 1;
|
||||
}
|
||||
|
||||
str += integer.charAt(i);
|
||||
str += integer.charAt(i);
|
||||
|
||||
if (l && !((l + 1) % group_position) && i != 0) {
|
||||
str += info.group_sep;
|
||||
}
|
||||
}
|
||||
parts[0] = str.split("").reverse().join("");
|
||||
}
|
||||
if (parts[0] + "" == "") {
|
||||
parts[0] = "0";
|
||||
}
|
||||
if (l && !((l + 1) % group_position) && i != 0) {
|
||||
str += info.group_sep;
|
||||
}
|
||||
}
|
||||
parts[0] = str.split("").reverse().join("");
|
||||
}
|
||||
if (parts[0] + "" == "") {
|
||||
parts[0] = "0";
|
||||
}
|
||||
|
||||
// join decimal
|
||||
parts[1] = (parts[1] && info.fraction_sep) ? (info.fraction_sep + parts[1]) : "";
|
||||
// join decimal
|
||||
parts[1] = (parts[1] && info.fraction_sep) ? (info.fraction_sep + parts[1]) : "";
|
||||
|
||||
// join
|
||||
return (is_negative ? "-" : "") + parts[0] + parts[1];
|
||||
},
|
||||
// join
|
||||
return (is_negative ? "-" : "") + parts[0] + parts[1];
|
||||
},
|
||||
|
||||
get_format_info(format) {
|
||||
let format_info = number_formats[format];
|
||||
get_format_info(format) {
|
||||
let format_info = number_formats[format];
|
||||
|
||||
if (!format_info) {
|
||||
throw `Unknown number format "${format}"`;
|
||||
}
|
||||
if (!format_info) {
|
||||
throw `Unknown number format "${format}"`;
|
||||
}
|
||||
|
||||
return format_info;
|
||||
},
|
||||
return format_info;
|
||||
},
|
||||
|
||||
round(num, precision) {
|
||||
round(num, precision) {
|
||||
var is_negative = num < 0 ? true : false;
|
||||
var d = parseInt(precision || 0);
|
||||
var m = Math.pow(10, d);
|
||||
@ -96,9 +96,9 @@ module.exports = {
|
||||
var r = ((!precision && f == 0.5) ? ((i % 2 == 0) ? i : i + 1) : Math.round(n));
|
||||
r = d ? r / m : r;
|
||||
return is_negative ? -r : r;
|
||||
},
|
||||
},
|
||||
|
||||
remove_separator(text, sep) {
|
||||
return text.replace(new RegExp(sep === "." ? "\\." : sep, "g"), '');
|
||||
}
|
||||
remove_separator(text, sep) {
|
||||
return text.replace(new RegExp(sep === "." ? "\\." : sep, "g"), '');
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user