UNPKG

unitformat

Version:

The RAW simple and flexible unit formatter

10 lines (8 loc) 912 B
/* Unit Formatter v1.1.5 8/16/2025 https://github.com/rawify/UnitFormat.js Copyright (c) 2025, Robert Eisele (https://raw.org/) Licensed under the MIT license. */ 'use strict';(function(n){function a(d,f,e,b){null==f&&(f="");null==e&&(e="kMGTPE");if(null==b)b=10;else if(void 0===k[b])throw Error("Not supported base "+b);if(0===d)return"0"+f;var l="";0>d&&(l="-",d=-d);for(var m="",g=d,h=0;h<e.length;h++){var c=e[h];if(c in k[b])c=d/Math.pow(b,k[b][c]),1<=c&&(c<g||1>g)&&(g=c,m=e[h]);else throw Error("Unknown unit "+c);}return l+Math.round(1E3*g)/1E3+m+f}var k={2:{E:60,P:50,T:40,G:30,M:20,k:10},10:{E:18,P:15,T:12,G:9,M:6,k:3,h:2,d:-1,c:-2,m:-3,u:-6,"\u03bc":-6, n:-9,p:-12,f:-15,a:-18}};"function"===typeof define&&define.amd?define([],function(){return a}):"object"===typeof exports?(Object.defineProperty(a,"__esModule",{value:!0}),a["default"]=a,a.UnitFormat=a,module.exports=a):n.UnitFormat=a})(this);