lemonutilities
v2.3.0
Published
Lightweight and simple JavaScript library for file management, randomization, and for CLIs.
Maintainers
Readme
LemonUtilities
LemonUtilities is a lightweight and simple JavaScript library for file management, randomization, and for CLIs (Console User Interfaces).
Setup
Installation
Go to your console, and type the following commands in order
cd "C:/path/to/your/project"npm install lemonutilitiesHow to Use
In your main JavaScript file, write the following
const {file, random, cli} = require('lemonutilities');Down below, you will find a list of functions LemonUtilitites has to offer.
Functions
File Management
getFiles(directoryPath, fileExtension)
- Description: Gets files from a folder or directory
- Parameters:
directoryPath(string): Path to checkfileExtension(string, optional): File extension to check for
- Returns (
array): Array of all file names - Example:
const images = file.getFiles('goober', 'png');
// ['bird.png', 'cat.png', 'dog.png', 'sillygoober.png']
console.log(images);getSubFolders(directoryPath)
- Description: Gets subfolders from a folder or directory
- Parameters:
directoryPath(string): Path to check
- Returns (
array): Array of all subfolder names - Example:
const subfolders = file.getSubFolders('goober');
// ['extra silly', 'very gooberish']
console.log(subfolders);moveDir(directoryPath, destinationFolderPath)
- Description: Moves a file, or an entire folder
- Parameters:
directoryPath(string): Path to filedestinationFolderPath(string): Folder to move the file to
- Example:
file.moveDir('goober/cat.png', 'goober/extra silly');renameDir(directoryPath, newFileName)
- Description: Renames a file, or a folder
- Parameters:
directoryPath(string): File to renamenewFileName(string): New file name
- Example:
file.renameDir('goober/extra silly/cat.png', 'silly car.png');
cloneFile(filePath, destinationFolderPath)
- Description: Clones a file to a specified destination
- Parameters:
filePath(string): Path to filedestinationFolderPath(string): New file path
- Example:
file.cloneFile('goober/very silly/silly car.png', 'goober of the day/silly car.png');cloneDir(directoryPath, destinationFolderPath)
- Description: Clones a folder and all contents to a specified destination
- Parameters:
directoryPath(string): Path to filedestinationFolderPath(string): New file path
- Example:
file.cloneDir('goober/very silly/silly car.png', 'goober of the day/silly car.png');calculateSize(directoryPath)
- Description: Calculates the size of a file or folder contents in bytes
- Parameters:
directoryPath(string): Path to file
- Returns (
string): Byte size - Example:
const size = file.calculateDirSize('goober/bird.png');
// bird.png is 295419 bytes. (295.419KB)
console.log(`bird.png is ${size} bytes. (${size/1000}KB)`);Randomness
num(min, max)
- Description: Returns a random number
- Parameters:
min(number): Minimum valuemax(number): Maximum value
- Returns (
num): Random number - Example:
const num = random.num(0, 10);
console.log(num);int(min, max)
- Description: Returns a random whole number
- Parameters:
min(number): Minimum valuemax(number): Maximum value
- Returns (
num): Random number - Example:
const num = random.int(0, 10);
console.log(num);item(array)
- Description: Returns a random item from an array
- Parameters:
array(array): Array to get item from
- Returns (
any): Random item - Example:
const items = ['goober', 'cat', 'dog', 'bird', true, false, 7, 8.5];
const item = random.item(items);
console.log(item);arrayShuffle(array)
- Description: Shuffles an array
- Parameters:
array(array):
- Returns (
array): Shuffled array - Example:
const items = ['goober', 'cat', 'dog', 'bird', true, false, 7, 8.5];
const shuffledItems = random.arrayShuffle(items);
console.log(shuffledItems);chance(...chances)
- Description: Generates a random chance based on the inputed values
- Parameters:
...chances(number): Chances
- Example:
const animals = ['cat', 'dog', 'bird'];
const chance = random.chance(10, 8, 3);
/*
10 in 21 chance it will return 0,
8 in 21 for 1,
and 3 in 21 for 2
*/
console.log(animals[chance]);generateToken(prefix)
- Description: Generates a random token for use in applications requiring secure identifiers
- Parameters:
prefix(string, optional): Prefix of the token
- Returns (
string): Generated token - Example:
const token = random.generateToken('LEM-');
console.log(token);Inputs
consoleInput() (async function)
- Description: Asks user for input
- Returns (
string): Response - Example:
console.log('is the cat a goober? [y/n]\n\n');
cli.consoleInput().then(response => {
if (response.toLowerCase == 'y') {
console.log('YAYAYAYAYAYY');
} else {
console.log('Aw man :(');
}
});
console.log('Gets printed after, but before the user hits enter.');OR
console.log('is the cat a goober? [y/n]\n\n');
// This function needs to be async
async function askQuestion() {
const response = await cli.consoleInput();
if (response.toLowerCase == 'y') {
console.log('YAYAYAYAYAYY');
} else {
console.log('Aw man :(');
}
console.log('Gets printed after the user hits enter.');
}
askQuestion();deleteLines(num)
- Description: Delete a specified amount of lines already printed in the console
- Parameters:
num(number): How many lines to delete
- Example:
async function deleteLinesExample() {
console.log('Cloning folder in 5 seconds...');
// See below
await cli.wait(5);
cli.deleteLines(1);
console.log('Silliest as can be :3');
}
deleteLinesExample();clear()
- Description: Clears the entire console
- Example:
console.log('clearing...');
cli.clear();wait(seconds, showCountdown) (async function)
- Description: Waits a specified amount of time in seconds before allowing further code to run
- Parameters:
seconds(number): Time to wait in secondsshowCountdown(bool, optional): Shows "Waiting... x" message
- Example:
// This function needs to be async
async function waitExample() {
console.log('Cloning folder in 5 seconds...');
await cli.wait(5, true);
file.cloneDir('goober/very silly', 'important');
}
waitExample();pause()
- Description: Stops further code from running, and displays a "hit any key to continue" message
- Example:
console.log('Done!');
cli.pause();
console.log('But there\'s more!');