Created
August 16, 2022 10:28
-
-
Save andreasvirkus/109b0cf343486939b7d7897aaa81c0fc to your computer and use it in GitHub Desktop.
Revisions
-
andreasvirkus created this gist
Aug 16, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ const roundingStops = [5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000] export const roundToScale = (count, scale = roundingStops) => { if (count === 0) return 0 const nearestIdx = scale.findIndex((s) => s > count) let nearest = scale[nearestIdx] if (nearestIdx !== 0 && nearest / 2 > count) nearest = scale[nearestIdx - 1] || scale[0] return nearest || count }