@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
1 lines • 1.89 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["export type TGetStrWithZeroFromNumArgs = Parameters<typeof getStrWithZeroFromNum>;\r\n\r\nexport type TGetStrWithZeroFromNumReturn = ReturnType<typeof getStrWithZeroFromNum>;\r\n\r\n/**\r\n * Pads a number with leading zeros to match the desired digit count. Preserves sign for negative numbers.\r\n *\r\n * @param {number} num Source number\r\n * @param {number} [digits=2] Desired total digits for the absolute value\r\n * @returns {string} Zero-padded string\r\n * @throws {TypeError} If inputs are invalid\r\n * @example\r\n * getStrWithZeroFromNum(9, 3); // \"009\"\r\n * getStrWithZeroFromNum(-10, 5); // \"-00010\"\r\n */\r\nexport const getStrWithZeroFromNum = (num: number, digits: number = 2): string => {\r\n if (!Number.isFinite(num)) {\r\n throw new TypeError(\"getStrWithZeroFromNum: num must be a finite number\");\r\n }\r\n if (!Number.isFinite(digits) || digits < 0 || Math.floor(digits) !== digits) {\r\n throw new TypeError(\"getStrWithZeroFromNum: digits must be a non-negative integer\");\r\n }\r\n\r\n const absNum = Math.abs(num);\r\n const raw = String(absNum);\r\n const padCount = Math.max(digits - raw.length, 0);\r\n const padded = (padCount ? \"0\".repeat(padCount) : \"\") + raw;\r\n\r\n return (num < 0 ? \"-\" : \"\") + padded;\r\n};\r\n"],"names":["getStrWithZeroFromNum","num","digits","Number","isFinite","TypeError","Math","floor","absNum","abs","raw","String","padCount","max","length","padded","repeat"],"mappings":";;;;;;;;;;;AAeO,MAAMA,sBAAwBA,CAACC,IAAaC,OAAiB,KAClE,IAAKC,OAAOC,SAASH,KACnB,MAAM,IAAII,UAAU,sDAEtB,IAAKF,OAAOC,SAASF,SAAWA,OAAS,GAAKI,KAAKC,MAAML,UAAYA,OACnE,MAAM,IAAIG,UAAU,gEAGtB,MAAMG,OAASF,KAAKG,IAAIR,KACxB,MAAMS,IAAMC,OAAOH,QACnB,MAAMI,SAAWN,KAAKO,IAAIX,OAASQ,IAAII,OAAQ,GAC/C,MAAMC,QAAUH,SAAW,IAAII,OAAOJ,UAAY,IAAMF,IAExD,OAAQT,IAAM,EAAI,IAAM,IAAMc"}