Welcome to AddZero
AddZero is a powerful library designed to simplify string manipulation by adding leading zeros to numbers and other essential tasks. With its wide range of API functions, AddZero proves to be a versatile tool for developers.
API Examples
1. addZero Function
Add leading zeros to a single number.
const { addZero } = require('addzero');
console.log(addZero(5)); // Output: 05
console.log(addZero(123, 5)); // Output: 00123
2. addZeroToArray Function
Add leading zeros to an array of numbers.
const { addZeroToArray } = require('addzero');
console.log(addZeroToArray([1, 23, 456], 4)); // Output: ['0001', '0023', '0456']
3. addZeroToString Function
Add leading zeros to a string representation of a number.
const { addZeroToString } = require('addzero');
console.log(addZeroToString('9', 3)); // Output: 009
4. addZeroInRange Function
Add leading zeros to each number in a specified range.
const { addZeroInRange } = require('addzero');
console.log(addZeroInRange(1, 10, 3)); // Output: ['001', '002', '003', '004', '005', '006', '007', '008', '009', '010']
5. addZeroToObjectKeys Function
Add leading zeros to the numerical keys of an object.
const { addZeroToObjectKeys } = require('addzero');
const obj = { 1: 'one', 2: 'two' };
console.log(addZeroToObjectKeys(obj, 3)); // Output: { '001': 'one', '002': 'two' }
App Example Using AddZero APIs
Now, let’s build a simple Node.js application that uses the AddZero library to format numerical data for display.
const express = require('express');
const { addZero, addZeroToArray, addZeroInRange } = require('addzero');
const app = express();
app.get('/format-number', (req, res) => {
const number = parseInt(req.query.number || '0', 10);
res.send(`Formatted Number: ${addZero(number, 4)}`);
});
app.get('/format-array', (req, res) => {
const numbers = [5, 23, 345];
res.send(`Formatted Array: ${addZeroToArray(numbers, 5)}`);
});
app.get('/format-range', (req, res) => {
const start = parseInt(req.query.start || '1', 10);
const end = parseInt(req.query.end || '10', 10);
res.send(`Formatted Range: ${addZeroInRange(start, end, 3).join(', ')}`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
With these examples, you can see how easy it is to integrate and use the AddZero library in your applications to ensure your numerical data is always formatted as needed.
Hash: b5db10a01b697fd3e436e2c8f83348d0dab07e6715e516260f65ede71853ed06