Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create randomUtils.ts with updated package.json (v3) #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/randomUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Generates a random string of specified length
* @param length The desired length of the random string
* @returns A random string containing alphanumeric characters
*/
export function generateRandomString(length: number): string {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';

for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}

return result;
}

/**
* Chunks an array into smaller arrays of specified size
* @param array The input array to be chunked
* @param size The size of each chunk
* @returns An array of chunked arrays
*/
export function chunkArray<T>(array: T[], size: number): T[][] {
const chunks: T[][] = [];

for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}

return chunks;
}

/**
* Debounces a function call
* @param func The function to debounce
* @param delay The delay in milliseconds
* @returns A debounced version of the function
*/
export function debounce<T extends (...args: any[]) => any>(
func: T,
delay: number
): (...args: Parameters<T>) => void {
let timeoutId: NodeJS.Timeout;

return (...args: Parameters<T>) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func(...args), delay);
};
}

/**
* Deep clones an object
* @param obj The object to clone
* @returns A deep clone of the input object
*/
export function deepClone<T>(obj: T): T {
if (obj === null || typeof obj !== 'object') {
return obj;
}

if (Array.isArray(obj)) {
return obj.map(item => deepClone(item)) as unknown as T;
}

const clonedObj = {} as T;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clonedObj[key] = deepClone(obj[key]);
}
}

return clonedObj;
}

/**
* Formats a number as a currency string
* @param amount The number to format
* @param currency The currency code (default: 'USD')
* @param locale The locale to use for formatting (default: 'en-US')
* @returns A formatted currency string
*/
export function formatCurrency(
amount: number,
currency: string = 'USD',
locale: string = 'en-US'
): string {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(amount);
}
62 changes: 31 additions & 31 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"esnext"
],
"typeRoots": [
"node_modules/@types",
"src/types/modules"
],
"baseUrl": "./src/",
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strictNullChecks": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"outDir": "dist",
"sourceMap": true
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
"compilerOptions": {
"target": "es5",
"lib": [
"esnext"
],
"typeRoots": [
"node_modules/@types",
"src/types/modules"
],
"baseUrl": "./src/",
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strictNullChecks": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "ESNext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"outDir": "dist",
"sourceMap": true
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}