UNPKG

@technobuddha/library

Version:
25 lines (24 loc) 960 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.padNumber = void 0; var isNaN_1 = __importDefault(require("lodash/isNaN")); /** * Add leading zeros to a number to ensure a string of a minimum length * * @param input The number to pad * @param length The minimum length of the resulting string * @returns number as a string with leading zeros as needed */ function padNumber(input, length) { if (length === void 0) { length = 2; } if (isNaN_1.default(input) || input === Infinity || input === -Infinity) return input.toString().padStart(length, ' '); else if (input < 0) return "-" + Math.abs(input).toString().padStart(length - 1, '0'); return input.toString().padStart(length, '0'); } exports.padNumber = padNumber; exports.default = padNumber;