diff options
author | Joas Schilling <coding@schilljs.com> | 2021-01-15 15:03:16 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-01-15 15:03:16 +0100 |
commit | 170506590c4029ba0fb10c69be5e315dfaf5faf8 (patch) | |
tree | 11d1a23a1640518df5b7dec9f6d9a149760d8f6f /src/mixins/readableNumber.js | |
parent | 528d5f444bc3f61641711c18e611e17d5d4c18d0 (diff) |
Make the dial-in info better readable
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'src/mixins/readableNumber.js')
-rw-r--r-- | src/mixins/readableNumber.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/mixins/readableNumber.js b/src/mixins/readableNumber.js new file mode 100644 index 000000000..944f703cf --- /dev/null +++ b/src/mixins/readableNumber.js @@ -0,0 +1,57 @@ +/** + * @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 + * @returns {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 + * @returns {string[]} + */ + stringChop(str, size) { + str = String(str) + size = ~~size + return size > 0 ? str.match(new RegExp('.{1,' + size + '}', 'g')) : [str] + }, + }, +} + +export default readableNumber |