sikits
Version:
A powerful and comprehensive utility library for JavaScript and TypeScript with 100+ functions for strings, numbers, arrays, and objects
251 lines (250 loc) โข 7.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rangeRandomEmojis = exports.rangeRandomHexColors = exports.rangeRandomColors = exports.rangeRandomStrings = exports.rangeRandom = exports.rangePrimes = exports.rangeFibonacci = exports.rangeBusinessDays = exports.rangeWeekends = exports.rangeWeekdays = exports.rangeYears = exports.rangeMonths = exports.rangeDates = exports.rangeChars = exports.rangeLength = exports.rangeReverse = exports.range = void 0;
/**
* Creates a range of numbers
*/
const range = (start, end, step = 1) => {
const result = [];
for (let i = start; i <= end; i += step) {
result.push(i);
}
return result;
};
exports.range = range;
/**
* Creates a range of numbers in reverse order
*/
const rangeReverse = (start, end, step = 1) => {
const result = [];
for (let i = start; i >= end; i -= step) {
result.push(i);
}
return result;
};
exports.rangeReverse = rangeReverse;
/**
* Creates a range of numbers with specified length
*/
const rangeLength = (start, length, step = 1) => {
const result = [];
for (let i = 0; i < length; i++) {
result.push(start + i * step);
}
return result;
};
exports.rangeLength = rangeLength;
/**
* Creates a range of characters
*/
const rangeChars = (start, end) => {
const startCode = start.charCodeAt(0);
const endCode = end.charCodeAt(0);
const result = [];
for (let i = startCode; i <= endCode; i++) {
result.push(String.fromCharCode(i));
}
return result;
};
exports.rangeChars = rangeChars;
/**
* Creates a range of dates
*/
const rangeDates = (start, end, stepDays = 1) => {
const result = [];
const current = new Date(start);
while (current <= end) {
result.push(new Date(current));
current.setDate(current.getDate() + stepDays);
}
return result;
};
exports.rangeDates = rangeDates;
/**
* Creates a range of months
*/
const rangeMonths = (start, end) => {
const result = [];
const current = new Date(start);
while (current <= end) {
result.push(new Date(current));
current.setMonth(current.getMonth() + 1);
}
return result;
};
exports.rangeMonths = rangeMonths;
/**
* Creates a range of years
*/
const rangeYears = (start, end) => {
const result = [];
const current = new Date(start);
while (current <= end) {
result.push(new Date(current));
current.setFullYear(current.getFullYear() + 1);
}
return result;
};
exports.rangeYears = rangeYears;
/**
* Creates a range of weekdays
*/
const rangeWeekdays = (start, end) => {
const result = [];
const current = new Date(start);
while (current <= end) {
const dayOfWeek = current.getDay();
if (dayOfWeek !== 0 && dayOfWeek !== 6) { // Skip weekends
result.push(new Date(current));
}
current.setDate(current.getDate() + 1);
}
return result;
};
exports.rangeWeekdays = rangeWeekdays;
/**
* Creates a range of weekends
*/
const rangeWeekends = (start, end) => {
const result = [];
const current = new Date(start);
while (current <= end) {
const dayOfWeek = current.getDay();
if (dayOfWeek === 0 || dayOfWeek === 6) { // Only weekends
result.push(new Date(current));
}
current.setDate(current.getDate() + 1);
}
return result;
};
exports.rangeWeekends = rangeWeekends;
/**
* Creates a range of business days (excluding weekends and holidays)
*/
const rangeBusinessDays = (start, end, holidays = []) => {
const result = [];
const current = new Date(start);
while (current <= end) {
const dayOfWeek = current.getDay();
const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
const isHoliday = holidays.some(holiday => holiday.getFullYear() === current.getFullYear() &&
holiday.getMonth() === current.getMonth() &&
holiday.getDate() === current.getDate());
if (!isWeekend && !isHoliday) {
result.push(new Date(current));
}
current.setDate(current.getDate() + 1);
}
return result;
};
exports.rangeBusinessDays = rangeBusinessDays;
/**
* Creates a range of Fibonacci numbers
*/
const rangeFibonacci = (count) => {
const result = [];
let a = 0, b = 1;
for (let i = 0; i < count; i++) {
result.push(a);
[a, b] = [b, a + b];
}
return result;
};
exports.rangeFibonacci = rangeFibonacci;
/**
* Creates a range of prime numbers
*/
const rangePrimes = (count) => {
const result = [];
let num = 2;
while (result.length < count) {
if (isPrime(num)) {
result.push(num);
}
num++;
}
return result;
};
exports.rangePrimes = rangePrimes;
/**
* Helper function to check if a number is prime
*/
const isPrime = (num) => {
if (num < 2)
return false;
if (num === 2)
return true;
if (num % 2 === 0)
return false;
for (let i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i === 0)
return false;
}
return true;
};
/**
* Creates a range of random numbers
*/
const rangeRandom = (count, min = 0, max = 100) => {
const result = [];
for (let i = 0; i < count; i++) {
result.push(Math.floor(Math.random() * (max - min + 1)) + min);
}
return result;
};
exports.rangeRandom = rangeRandom;
/**
* Creates a range of random strings
*/
const rangeRandomStrings = (count, length = 8) => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const result = [];
for (let i = 0; i < count; i++) {
let str = '';
for (let j = 0; j < length; j++) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
}
result.push(str);
}
return result;
};
exports.rangeRandomStrings = rangeRandomStrings;
/**
* Creates a range of random colors
*/
const rangeRandomColors = (count) => {
const result = [];
for (let i = 0; i < count; i++) {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
result.push(`rgb(${r}, ${g}, ${b})`);
}
return result;
};
exports.rangeRandomColors = rangeRandomColors;
/**
* Creates a range of random hex colors
*/
const rangeRandomHexColors = (count) => {
const result = [];
for (let i = 0; i < count; i++) {
const color = '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
result.push(color);
}
return result;
};
exports.rangeRandomHexColors = rangeRandomHexColors;
/**
* Creates a range of random emojis
*/
const rangeRandomEmojis = (count) => {
const emojis = ['๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐คฃ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฅฐ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐คช', '๐คจ', '๐ง', '๐ค', '๐', '๐คฉ', '๐ฅณ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', 'โน๏ธ', '๐ฃ', '๐', '๐ซ', '๐ฉ', '๐ฅบ', '๐ข', '๐ญ', '๐ค', '๐ ', '๐ก', '๐คฌ', '๐คฏ', '๐ณ', '๐ฅต', '๐ฅถ', '๐ฑ', '๐จ', '๐ฐ', '๐ฅ', '๐', '๐ค', '๐ค', '๐คญ', '๐คซ', '๐คฅ', '๐ถ', '๐', '๐', '๐ฏ', '๐ฆ', '๐ง', '๐ฎ', '๐ฒ', '๐ฅฑ', '๐ด', '๐คค', '๐ช', '๐ต', '๐ค', '๐ฅด', '๐คข', '๐คฎ', '๐คง', '๐ท', '๐ค', '๐ค', '๐ค', '๐ค '];
const result = [];
for (let i = 0; i < count; i++) {
const randomIndex = Math.floor(Math.random() * emojis.length);
result.push(emojis[randomIndex]);
}
return result;
};
exports.rangeRandomEmojis = rangeRandomEmojis;