dtable-ui-component 0.1.75-beta → 0.1.75-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app.css +20 -0
- package/lib/app.js +96 -0
- package/lib/assets/images/avatar/default_avatar.png +0 -0
- package/lib/assets/images/file/192/excel.png +0 -0
- package/lib/assets/images/file/192/file.png +0 -0
- package/lib/assets/images/file/192/music.png +0 -0
- package/lib/assets/images/file/192/pdf.png +0 -0
- package/lib/assets/images/file/192/pic.png +0 -0
- package/lib/assets/images/file/192/ppt.png +0 -0
- package/lib/assets/images/file/192/txt.png +0 -0
- package/lib/assets/images/file/192/video.png +0 -0
- package/lib/assets/images/file/192/word.png +0 -0
- package/lib/assets/images/file/24/excel.png +0 -0
- package/lib/assets/images/file/24/file.png +0 -0
- package/lib/assets/images/file/24/music.png +0 -0
- package/lib/assets/images/file/24/pdf.png +0 -0
- package/lib/assets/images/file/24/pic.png +0 -0
- package/lib/assets/images/file/24/ppt.png +0 -0
- package/lib/assets/images/file/24/txt.png +0 -0
- package/lib/assets/images/file/24/video.png +0 -0
- package/lib/assets/images/file/24/word.png +0 -0
- package/lib/assets/images/folder/folder-192.png +0 -0
- package/lib/assets/images/folder/folder-24.png +0 -0
- package/lib/components/cell-editor/checkbox-editor.js +111 -0
- package/lib/components/cell-editor/collaborator-editor.js +249 -0
- package/lib/components/cell-editor/date-editor.js +171 -0
- package/lib/components/cell-editor/index.js +40 -0
- package/lib/components/cell-editor/link-editor.js +321 -0
- package/lib/components/cell-editor/multiple-select-editor.js +250 -0
- package/lib/components/cell-editor/number-editor.js +162 -0
- package/lib/components/cell-editor/single-select-editor.js +216 -0
- package/lib/components/cell-editor/text-editor.js +129 -0
- package/lib/components/cell-editor-dialog/pc-file-editor-dialog.js +66 -0
- package/lib/components/cell-editor-dialog/pc-files-addition/index.js +1 -0
- package/lib/components/cell-editor-dialog/pc-files-addition/pc-file-uploaded-item.js +1 -0
- package/lib/components/cell-editor-dialog/pc-files-preview/index.js +1 -0
- package/lib/components/cell-editor-dialog/pc-files-preview/pc-file-item-preview.js +1 -0
- package/lib/components/cell-editor-popover/mb-collaborator-editor-popover.js +183 -0
- package/lib/components/cell-editor-popover/mb-date-editor-popover.js +262 -0
- package/lib/components/cell-editor-popover/mb-link-editor-popover.js +176 -0
- package/lib/components/cell-editor-popover/mb-select-editor-popover.js +243 -0
- package/lib/components/cell-editor-popover/pc-collaborator-editor-popover.js +124 -0
- package/lib/components/cell-editor-popover/pc-date-editor-popover.js +156 -0
- package/lib/components/cell-editor-popover/pc-link-editor-popover.js +128 -0
- package/lib/components/cell-editor-popover/pc-select-editor-popover.js +157 -0
- package/lib/components/cell-factory/cell-formatter-factory.js +28 -0
- package/lib/components/cell-formatter/auto-number-formatter.js +45 -0
- package/lib/components/cell-formatter/button-formatter.js +66 -0
- package/lib/components/cell-formatter/checkbox-formatter.js +56 -0
- package/lib/components/cell-formatter/collaborator-formatter.js +111 -0
- package/lib/components/cell-formatter/creator-formatter.js +105 -0
- package/lib/components/cell-formatter/ctime-formatter.js +70 -0
- package/lib/components/cell-formatter/date-formatter.js +73 -0
- package/lib/components/cell-formatter/duration-formatter.js +49 -0
- package/lib/components/cell-formatter/email-formatter.js +45 -0
- package/lib/components/cell-formatter/file-formatter.js +77 -0
- package/lib/components/cell-formatter/formatter-config.js +66 -0
- package/lib/components/cell-formatter/formula-formatter.js +189 -0
- package/lib/components/cell-formatter/geolocation-formatter.js +66 -0
- package/lib/components/cell-formatter/image-formatter.js +171 -0
- package/lib/components/cell-formatter/index.js +112 -0
- package/lib/components/cell-formatter/last-modifier-formatter.js +105 -0
- package/lib/components/cell-formatter/link-formatter.js +165 -0
- package/lib/components/cell-formatter/long-text-formatter.js +46 -0
- package/lib/components/cell-formatter/mtime-formatter.js +70 -0
- package/lib/components/cell-formatter/multiple-select-formatter.js +84 -0
- package/lib/components/cell-formatter/number-formatter.js +60 -0
- package/lib/components/cell-formatter/rate-formatter.js +93 -0
- package/lib/components/cell-formatter/single-select-formatter.js +84 -0
- package/lib/components/cell-formatter/text-formatter.js +67 -0
- package/lib/components/cell-formatter/url-formatter.js +45 -0
- package/lib/components/cell-formatter/widgets/file-item-formatter.js +63 -0
- package/lib/components/cell-formatter/widgets/image-previewer-lightbox.js +152 -0
- package/lib/components/cell-formatter/widgets/long-text-formatter/html-long-text-formatter.js +107 -0
- package/lib/components/cell-formatter/widgets/long-text-formatter/simple-long-text-formatter.js +110 -0
- package/lib/components/common/collaborator-item.js +79 -0
- package/lib/components/common/edit-editor-button.js +67 -0
- package/lib/components/common/images-lazy-load.js +158 -0
- package/lib/components/common/link-editor-option.js +121 -0
- package/lib/components/common/mobile/mb-editor-header.js +61 -0
- package/lib/components/common/modal-portal.js +51 -0
- package/lib/components/common/select-editor-option.js +122 -0
- package/lib/components/common/select-item.js +73 -0
- package/lib/components/file-uploader/index.js +63 -0
- package/lib/components/loading.js +16 -0
- package/lib/components/toast/alert.js +164 -0
- package/lib/components/toast/index.js +12 -0
- package/lib/components/toast/toast.js +223 -0
- package/lib/components/toast/toastManager.js +189 -0
- package/lib/components/toast/toaster.js +92 -0
- package/lib/constants/cell-types.js +54 -0
- package/lib/constants/index.js +70 -0
- package/lib/css/cell-editor.css +614 -0
- package/lib/css/cell-formatter.css +247 -0
- package/lib/css/custom-rc-calendar.css +118 -0
- package/lib/css/image-previewer-ligntbox.css +87 -0
- package/lib/css/loading.css +54 -0
- package/lib/index.js +64 -0
- package/lib/lang/index.js +66 -0
- package/lib/locals/de.js +7 -0
- package/lib/locals/en.js +22 -0
- package/lib/locals/fr.js +7 -0
- package/lib/locals/zh-CN.js +22 -0
- package/lib/utils/cell-value-validator.js +41 -0
- package/lib/utils/column-utils.js +15 -0
- package/lib/utils/editor-utils.js +81 -0
- package/lib/utils/markdown2html.js +68 -0
- package/lib/utils/normalize-long-text-value.js +74 -0
- package/lib/utils/number-precision.js +181 -0
- package/lib/utils/unified/index.js +470 -0
- package/lib/utils/utils.js +141 -0
- package/lib/utils/value-format-utils.js +545 -0
- package/lib/utils/vfile/core.js +172 -0
- package/lib/utils/vfile/index.js +48 -0
- package/package.json +3 -3
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
|
|
6
|
+
var CellType = _interopRequireWildcard(require("../constants/cell-types"));
|
|
7
|
+
|
|
8
|
+
var _constants = require("../constants");
|
|
9
|
+
|
|
10
|
+
var _validators;
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
var numberValidator = function numberValidator(value) {
|
|
17
|
+
return (value || value === 0) && Object.prototype.toString.call(value) === '[object Number]';
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var textValidator = function textValidator(value) {
|
|
21
|
+
return !!value;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
var checkboxValidator = function checkboxValidator(value) {
|
|
25
|
+
return typeof value === 'boolean';
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var validators = (_validators = {}, _validators[CellType.NUMBER] = numberValidator, _validators[CellType.RATE] = numberValidator, _validators[CellType.DURATION] = numberValidator, _validators[CellType.CHECKBOX] = checkboxValidator, _validators[CellType.TEXT] = textValidator, _validators[CellType.DATE] = textValidator, _validators[CellType.CTIME] = textValidator, _validators[CellType.MTIME] = textValidator, _validators[CellType.GEOLOCATION] = textValidator, _validators[CellType.AUTO_NUMBER] = textValidator, _validators[CellType.URL] = textValidator, _validators[CellType.EMAIL] = textValidator, _validators[_constants.FORMULA_RESULT_TYPE.DATE] = textValidator, _validators[_constants.FORMULA_RESULT_TYPE.STRING] = textValidator, _validators[_constants.FORMULA_RESULT_TYPE.NUMBER] = numberValidator, _validators[_constants.FORMULA_RESULT_TYPE.BOOL] = checkboxValidator, _validators);
|
|
29
|
+
|
|
30
|
+
var cellValueValidator = function cellValueValidator(cellValue, type) {
|
|
31
|
+
var validator = validators[type];
|
|
32
|
+
|
|
33
|
+
if (validator) {
|
|
34
|
+
return validator(cellValue);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
var _default = cellValueValidator;
|
|
41
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.isArrayFormalColumn = isArrayFormalColumn;
|
|
5
|
+
exports.isSimpleCellFormatter = isSimpleCellFormatter;
|
|
6
|
+
|
|
7
|
+
var _constants = require("../constants");
|
|
8
|
+
|
|
9
|
+
function isSimpleCellFormatter(type) {
|
|
10
|
+
return _constants.SIMPLE_CELL_COLUMNS.includes(type) || _constants.SIMPLE_CELL_FORMULA_RESULTS.includes(type);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isArrayFormalColumn(columnType) {
|
|
14
|
+
return _constants.ARRAY_FORMAL_COLUMNS.includes(columnType);
|
|
15
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.initDateEditorLanguage = exports.getSelectOptionItem = void 0;
|
|
5
|
+
|
|
6
|
+
var zhCN = require('@seafile/seafile-calendar/lib/locale/zh_CN');
|
|
7
|
+
|
|
8
|
+
var zhTW = require('@seafile/seafile-calendar/lib/locale/zh_TW');
|
|
9
|
+
|
|
10
|
+
var enUS = require('@seafile/seafile-calendar/lib/locale/en_US');
|
|
11
|
+
|
|
12
|
+
var frFR = require('@seafile/seafile-calendar/lib/locale/fr_FR');
|
|
13
|
+
|
|
14
|
+
var deDE = require('@seafile/seafile-calendar/lib/locale/de_DE');
|
|
15
|
+
|
|
16
|
+
var esES = require('@seafile/seafile-calendar/lib/locale/es_ES');
|
|
17
|
+
|
|
18
|
+
var plPL = require('@seafile/seafile-calendar/lib/locale/pl_PL');
|
|
19
|
+
|
|
20
|
+
var csCZ = require('@seafile/seafile-calendar/lib/locale/cs_CZ');
|
|
21
|
+
|
|
22
|
+
var initDateEditorLanguage = function initDateEditorLanguage(lang) {
|
|
23
|
+
var language;
|
|
24
|
+
|
|
25
|
+
switch (lang) {
|
|
26
|
+
case 'zh-cn':
|
|
27
|
+
language = zhCN;
|
|
28
|
+
break;
|
|
29
|
+
|
|
30
|
+
case 'zh-tw':
|
|
31
|
+
language = zhTW;
|
|
32
|
+
break;
|
|
33
|
+
|
|
34
|
+
case 'en':
|
|
35
|
+
language = enUS;
|
|
36
|
+
break;
|
|
37
|
+
|
|
38
|
+
case 'fr':
|
|
39
|
+
language = frFR;
|
|
40
|
+
break;
|
|
41
|
+
|
|
42
|
+
case 'de':
|
|
43
|
+
language = deDE;
|
|
44
|
+
break;
|
|
45
|
+
|
|
46
|
+
case 'es':
|
|
47
|
+
language = esES;
|
|
48
|
+
break;
|
|
49
|
+
|
|
50
|
+
case 'es-ar':
|
|
51
|
+
language = esES;
|
|
52
|
+
break;
|
|
53
|
+
|
|
54
|
+
case 'es-mx':
|
|
55
|
+
language = esES;
|
|
56
|
+
break;
|
|
57
|
+
|
|
58
|
+
case 'pl':
|
|
59
|
+
language = plPL;
|
|
60
|
+
break;
|
|
61
|
+
|
|
62
|
+
case 'cs':
|
|
63
|
+
language = csCZ;
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
default:
|
|
67
|
+
language = zhCN;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return language;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
exports.initDateEditorLanguage = initDateEditorLanguage;
|
|
74
|
+
|
|
75
|
+
var getSelectOptionItem = function getSelectOptionItem(options, optionId) {
|
|
76
|
+
return options.find(function (option) {
|
|
77
|
+
return option.id === optionId;
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.getSelectOptionItem = getSelectOptionItem;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.processorGetAST = exports.processor = void 0;
|
|
5
|
+
|
|
6
|
+
var unified = require('./unified');
|
|
7
|
+
|
|
8
|
+
var markdown = require('remark-parse');
|
|
9
|
+
|
|
10
|
+
var slug = require('remark-slug');
|
|
11
|
+
|
|
12
|
+
var breaks = require('remark-breaks');
|
|
13
|
+
|
|
14
|
+
var remark2rehype = require('remark-rehype');
|
|
15
|
+
|
|
16
|
+
var format = require('rehype-format');
|
|
17
|
+
|
|
18
|
+
var raw = require('rehype-raw');
|
|
19
|
+
|
|
20
|
+
var xtend = require('xtend');
|
|
21
|
+
|
|
22
|
+
var toHTML = require('hast-util-to-html');
|
|
23
|
+
|
|
24
|
+
var sanitize = require('hast-util-sanitize');
|
|
25
|
+
|
|
26
|
+
var gh = require('hast-util-sanitize/lib/github');
|
|
27
|
+
|
|
28
|
+
var deepmerge = require('deepmerge').default;
|
|
29
|
+
|
|
30
|
+
var mathjax = require('rehype-mathjax/browser');
|
|
31
|
+
|
|
32
|
+
var math = require('remark-math/block');
|
|
33
|
+
|
|
34
|
+
function stringify(config) {
|
|
35
|
+
var settings = xtend(config, this.data('settings'));
|
|
36
|
+
var schema = deepmerge(gh, {
|
|
37
|
+
'attributes': {
|
|
38
|
+
'input': ['type'],
|
|
39
|
+
'li': ['className'],
|
|
40
|
+
'code': ['className'],
|
|
41
|
+
'span': ['className'],
|
|
42
|
+
'div': ['className']
|
|
43
|
+
},
|
|
44
|
+
'tagNames': ['input', 'code', 'span', 'div']
|
|
45
|
+
});
|
|
46
|
+
this.Compiler = compiler;
|
|
47
|
+
|
|
48
|
+
function compiler(tree) {
|
|
49
|
+
// use sanity to remove dangerous html, the default is
|
|
50
|
+
// GitHub style sanitation
|
|
51
|
+
var hast = sanitize(tree, schema);
|
|
52
|
+
return toHTML(hast, settings);
|
|
53
|
+
}
|
|
54
|
+
} // markdown -> mdast -> html AST -> html
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
var processor = unified().use(markdown, {
|
|
58
|
+
commonmark: true
|
|
59
|
+
}).use(math).use(breaks).use(slug).use(remark2rehype, {
|
|
60
|
+
allowDangerousHTML: true
|
|
61
|
+
}).use(mathjax, {
|
|
62
|
+
displayMath: ['$$', '$$']
|
|
63
|
+
}).use(raw).use(format).use(stringify);
|
|
64
|
+
exports.processor = processor;
|
|
65
|
+
var processorGetAST = unified().use(markdown, {
|
|
66
|
+
commonmark: true
|
|
67
|
+
}).use(slug);
|
|
68
|
+
exports.processorGetAST = processorGetAST;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var hrefReg = /\[.+\]\(\S+\)|<img src=(\S+).+\/>|!\[\]\(\S+\)|<\S+>/g;
|
|
6
|
+
var imageReg1 = /^<img( width=[\\|/|"](\d)+[\\|/|"])? src="(\S+)" .?\/>/;
|
|
7
|
+
var imageReg2 = /^!\[\]\((\S+)\)/;
|
|
8
|
+
var linkReg1 = /^\[.+\]\(\S+\)/;
|
|
9
|
+
var linkReg2 = /^<\S+>$/;
|
|
10
|
+
|
|
11
|
+
var getLinks = function getLinks(hrefList) {
|
|
12
|
+
var hrefObj = {
|
|
13
|
+
links: [],
|
|
14
|
+
images: []
|
|
15
|
+
};
|
|
16
|
+
hrefList.forEach(function (href) {
|
|
17
|
+
if (href.search(linkReg1) >= 0 || href.search(linkReg2) >= 0) {
|
|
18
|
+
hrefObj.links.push(href);
|
|
19
|
+
} else {
|
|
20
|
+
var imageSrcList = href.match(imageReg1);
|
|
21
|
+
var imageSrcList1 = href.match(imageReg2);
|
|
22
|
+
|
|
23
|
+
if (imageSrcList) {
|
|
24
|
+
hrefObj.images.push(imageSrcList[1]);
|
|
25
|
+
} else if (imageSrcList1) {
|
|
26
|
+
hrefObj.images.push(imageSrcList1[1]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return hrefObj;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
var getPreviewContent = function getPreviewContent(markdownContent) {
|
|
34
|
+
var preview = '';
|
|
35
|
+
var newMarkdownContent = markdownContent.replace(hrefReg, '');
|
|
36
|
+
|
|
37
|
+
for (var index = 0; index < newMarkdownContent.length; index++) {
|
|
38
|
+
if (newMarkdownContent[index] === '#') {
|
|
39
|
+
continue;
|
|
40
|
+
} else if (newMarkdownContent[index] === '\n') {
|
|
41
|
+
preview += ' ';
|
|
42
|
+
} else {
|
|
43
|
+
preview += newMarkdownContent[index];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (preview.length === 150) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
var hrefList = markdownContent.match(hrefReg);
|
|
52
|
+
|
|
53
|
+
if (hrefList) {
|
|
54
|
+
var _getLinks = getLinks(hrefList),
|
|
55
|
+
images = _getLinks.images,
|
|
56
|
+
links = _getLinks.links;
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
preview: preview,
|
|
60
|
+
images: images,
|
|
61
|
+
links: links
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
preview: preview,
|
|
67
|
+
images: [],
|
|
68
|
+
links: [],
|
|
69
|
+
text: markdownContent
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
var _default = getPreviewContent;
|
|
74
|
+
exports.default = _default;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
exports.digitLength = digitLength;
|
|
6
|
+
exports.divide = divide;
|
|
7
|
+
exports.enableBoundaryChecking = enableBoundaryChecking;
|
|
8
|
+
exports.float2Fixed = float2Fixed;
|
|
9
|
+
exports.minus = minus;
|
|
10
|
+
exports.plus = plus;
|
|
11
|
+
exports.round = round;
|
|
12
|
+
exports.strip = strip;
|
|
13
|
+
exports.times = times;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @desc Solve the problem of floating calculation, avoid multiple digits after the decimal point and loss of calculation accuracy.
|
|
17
|
+
* example: 3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Correct wrong data
|
|
22
|
+
* strip(0.09999999999999998)=0.1
|
|
23
|
+
*/
|
|
24
|
+
function strip(num, precision) {
|
|
25
|
+
if (precision === void 0) {
|
|
26
|
+
precision = 12;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return +parseFloat(num.toPrecision(precision));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Return digits length of a number
|
|
33
|
+
* @param {*number} num Input number
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
function digitLength(num) {
|
|
38
|
+
// Get digit length of e
|
|
39
|
+
var eSplit = num.toString().split(/[eE]/);
|
|
40
|
+
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
|
|
41
|
+
return len > 0 ? len : 0;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Convert decimals to integers and support scientific notation. If it is a decimal, it is enlarged to an integer
|
|
45
|
+
* @param {*number} num Number of inputs
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
function float2Fixed(num) {
|
|
50
|
+
if (num.toString().indexOf('e') === -1) {
|
|
51
|
+
return Number(num.toString().replace('.', ''));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var dLen = digitLength(num);
|
|
55
|
+
return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check whether the number is out of range, and give a prompt if it is out of range
|
|
59
|
+
* @param {*number} num Number of inputs
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
function checkBoundary(num) {
|
|
64
|
+
if (_boundaryCheckingState) {
|
|
65
|
+
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
|
|
66
|
+
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Exact multiplication
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
function times(num1, num2) {
|
|
76
|
+
for (var _len = arguments.length, others = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
77
|
+
others[_key - 2] = arguments[_key];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (others.length > 0) {
|
|
81
|
+
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var num1Changed = float2Fixed(num1);
|
|
85
|
+
var num2Changed = float2Fixed(num2);
|
|
86
|
+
var baseNum = digitLength(num1) + digitLength(num2);
|
|
87
|
+
var leftValue = num1Changed * num2Changed;
|
|
88
|
+
checkBoundary(leftValue);
|
|
89
|
+
return leftValue / Math.pow(10, baseNum);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Exact addition
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
function plus(num1, num2) {
|
|
97
|
+
for (var _len2 = arguments.length, others = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
98
|
+
others[_key2 - 2] = arguments[_key2];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (others.length > 0) {
|
|
102
|
+
return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1)));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
|
106
|
+
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Exact subtraction
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
function minus(num1, num2) {
|
|
114
|
+
for (var _len3 = arguments.length, others = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
|
|
115
|
+
others[_key3 - 2] = arguments[_key3];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (others.length > 0) {
|
|
119
|
+
return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1)));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
|
123
|
+
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Exact division
|
|
127
|
+
*/
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
function divide(num1, num2) {
|
|
131
|
+
for (var _len4 = arguments.length, others = new Array(_len4 > 2 ? _len4 - 2 : 0), _key4 = 2; _key4 < _len4; _key4++) {
|
|
132
|
+
others[_key4 - 2] = arguments[_key4];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (others.length > 0) {
|
|
136
|
+
return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1)));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
var num1Changed = float2Fixed(num1);
|
|
140
|
+
var num2Changed = float2Fixed(num2);
|
|
141
|
+
checkBoundary(num1Changed);
|
|
142
|
+
checkBoundary(num2Changed); // fix: Similar to 10 ** -4 is 0.00009999999999999999, strip correction
|
|
143
|
+
|
|
144
|
+
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* rounding
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
function round(num, ratio) {
|
|
152
|
+
var base = Math.pow(10, ratio);
|
|
153
|
+
return divide(Math.round(times(num, base)), base);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
var _boundaryCheckingState = true;
|
|
157
|
+
/**
|
|
158
|
+
* Whether to perform boundary check, default true
|
|
159
|
+
* @param flag Mark switch, true is on, false is off, default is true
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
function enableBoundaryChecking(flag) {
|
|
163
|
+
if (flag === void 0) {
|
|
164
|
+
flag = true;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
_boundaryCheckingState = flag;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
var _default = {
|
|
171
|
+
strip: strip,
|
|
172
|
+
plus: plus,
|
|
173
|
+
minus: minus,
|
|
174
|
+
times: times,
|
|
175
|
+
divide: divide,
|
|
176
|
+
round: round,
|
|
177
|
+
digitLength: digitLength,
|
|
178
|
+
float2Fixed: float2Fixed,
|
|
179
|
+
enableBoundaryChecking: enableBoundaryChecking
|
|
180
|
+
};
|
|
181
|
+
exports.default = _default;
|