mirror of
https://github.com/20kaushik02/spotify-manager-web.git
synced 2026-01-25 16:14:06 +00:00
typescript! yay! (pain.)
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* Returns a string with zero padding of the number
|
||||
*
|
||||
* @param {number} num Input number (positive integer only, for now)
|
||||
* @param {number} places Number of zeroes to pad
|
||||
* @param {"before" | "after"} position Position of zeroes
|
||||
* @returns {string} Zero-padded string
|
||||
*/
|
||||
export const zeroPaddedString = (num, places, position) => {
|
||||
if (num < 0) throw new Error("negative number");
|
||||
if (places < 0) throw new Error("invalid number of zeroes");
|
||||
if (position !== "before" && position !== "after") throw new Error("invalid position (before or after only)");
|
||||
|
||||
const zeroes = "0".repeat(places);
|
||||
return position === "before" ? '' + zeroes + num : '' + num + zeroes;
|
||||
}
|
||||
13
src/utils/numFormatter.ts
Normal file
13
src/utils/numFormatter.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export const zeroPaddedString = (
|
||||
num: number,
|
||||
places: number,
|
||||
position: "before" | "after"
|
||||
): string => {
|
||||
if (num < 0) throw new Error("negative number");
|
||||
if (places < 0) throw new Error("invalid number of zeroes");
|
||||
if (position !== "before" && position !== "after")
|
||||
throw new Error("invalid position (before or after only)");
|
||||
|
||||
const zeroes = "0".repeat(places);
|
||||
return position === "before" ? "" + zeroes + num : "" + num + zeroes;
|
||||
};
|
||||
Reference in New Issue
Block a user