From c962253c33078036df2b98b8b1a3786432da06c2 Mon Sep 17 00:00:00 2001 From: sezgi Date: Tue, 14 Jul 2020 01:29:27 -0700 Subject: [PATCH] docs: prettify github count (close #5356) (#5357) --- docs/_static/utils.js | 25 +++++++++++++++++++++++++ docs/_theme/djangodocs/layout.html | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 docs/_static/utils.js diff --git a/docs/_static/utils.js b/docs/_static/utils.js new file mode 100644 index 00000000000..d861e31c17d --- /dev/null +++ b/docs/_static/utils.js @@ -0,0 +1,25 @@ +const hasuraUtils = { + formatNumber: function(number) { + if (typeof number !== "number") return number; + + const SIsymbol = ["", "k", "M", "G", "T", "P", "E"]; + const absNumber = Math.abs(number); + const sign = Math.sign(number); + + // what tier? (determines SI symbol) + const tier = Math.log10(absNumber) / 3 | 0; + + // if zero, we don't need a suffix + if (tier === 0) return sign * absNumber; + + // get suffix and determine scale + const suffix = SIsymbol[tier]; + const scale = Math.pow(10, tier * 3); + + // scale the number + const scaled = absNumber / scale; + + // format number and add suffix + return sign * scaled.toFixed(1) + suffix; + } +} diff --git a/docs/_theme/djangodocs/layout.html b/docs/_theme/djangodocs/layout.html index 5483b75640f..90868f0a777 100644 --- a/docs/_theme/djangodocs/layout.html +++ b/docs/_theme/djangodocs/layout.html @@ -6,6 +6,8 @@ {% set css_files = css_files + ['_static/djangosite.css', '_static/hasura-custom.css'] %} +{% set script_files = script_files + ['_static/utils.js'] %} + {%- if is_landing_page %} {% set css_files = css_files + ['_static/landing.css'] %} {%- endif %} @@ -751,7 +753,7 @@ }) .then(data => { const githubStar = data[0].star_count - $(".gitHubCount").html(githubStar); + $(".gitHubCount").html(hasuraUtils.formatNumber(githubStar)); $(".gitHubBtn").removeClass("hide"); }) .catch(e => {