blob: 78057cecf7bd95faafd9a383d49190e1b394e0c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/**
* @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
const readableNumber = {
methods: {
/**
* Splits a number into chunks of 3 digits (single digit as last is merged into the previous chunk):
* Samples:
* 9432670 => 943 2670
* 94326702 => 943 267 02
* 943267028 => 943 267 028
* 9432670284 => 943 267 0284
*
* @param {string} number The number to make readable
* @return {string}
*/
readableNumber(number) {
const chunks = this.stringChop(number, 3)
const lastChunk = chunks.pop()
if (lastChunk.length === 1) {
return chunks.join(' ') + lastChunk
}
return chunks.join(' ') + ' ' + lastChunk
},
/**
* Copied from https://www.w3resource.com/javascript-exercises/javascript-string-exercise-17.php
*
* @param {string} str The string to chop
* @param {number} size Size of the chunks
* @return {string[]}
*/
stringChop(str, size) {
str = String(str)
size = ~~size
return size > 0 ? str.match(new RegExp('.{1,' + size + '}', 'g')) : [str]
},
},
}
export default readableNumber
|