From 14f00e2cb303be20419b0c12d53565f0138bbacd Mon Sep 17 00:00:00 2001 From: Burak Sonmez Date: Wed, 1 Apr 2020 07:36:55 +0300 Subject: [PATCH] init --- .gitignore | 2 ++ LICENSE | 21 +++++++++++++++++++++ README.md | 0 index.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7636cae --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +yarn-error.log diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0b1557e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Burak Sonmez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js new file mode 100644 index 0000000..5fd43ba --- /dev/null +++ b/index.js @@ -0,0 +1,44 @@ +const Abbreviated = { + length(value) { + let len = 0; + while (value > 1) { + len += 1; + value /= 10; + } + return len; + }, + + get(value) { + const len = this.length(value); + + if (len <= 3) return value; + + if (len > 3 && len <= 6) { + return `${value.toString().substr(0, len - 3)}K`; + } + + if (len > 6 && len <= 9) { + return `${value.toString().substr(0, len - 6)}M`; + } + + if (len > 9 && len <= 12) { + return `${value.toString().substr(0, len - 9)}B`; + } + + if (len > 12 && len <= 15) { + return `${value.toString().substr(0, len - 12)}T`; + } + + if (len > 15 && len <= 18) { + return `${value.toString().substr(0, len - 15)}q`; + } + + if (len > 18 && len <= 21) { + return `${value.toString().substr(0, len - 18)}Q`; + } + + return value; + }, +}; + +module.exports = { Abbreviated };