npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lemonutilities

v2.2.1

Published

Lightweight and simple JavaScript library for file management, randomization, and for CLIs.

Downloads

252

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 lemonutilities

How 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

read(filePath)

  • Description: returns file contents from a path.
  • Parameters:
    • filePath (string): Path to file
  • Returns (string): File contents
  • Example:
const message = file.read('message.txt');

// Hello, World!
console.log(message);

write(filePath, content)

  • Description: writes data to a file.
  • Parameters:
    • filePath (string): Path to file
    • content (string): Content to write to file
  • Example:
const message = 'Goodbye, World :(';

file.write('message.txt', message);

checkPathAccessible(directoryPath)

  • Description: Check if a path is accessible by your script
  • Parameters:
    • directoryPath (string): Path to check
  • Returns (bool): true/false
  • Example:
if (file.checkPathExists('goober/cat.png')) {
    console.log('cat.png exists!');
} else {
    console.log('cat.png does not exist :(');
}

checkPathExists(directoryPath)

  • Description: Check if a path exists
  • Parameters:
    • directoryPath (string): Path to check
  • Returns (bool): true/false
  • Example:
if (file.checkPathExists('goober/cat.png')) {
    console.log('cat.png exists!');
} else {
    console.log('cat.png does not exist :(');
}

getFiles(directoryPath, fileExtension)

  • Description: Gets files from a folder or directory
  • Parameters:
    • directoryPath (string): Path to check
    • fileExtension (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 file
    • destinationFolderPath (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 rename
    • newFileName (string): New file name
  • Example:
file.renameDir('goober/extra silly/cat.png', 'silly car.png');

deleteDir(directoryPath) (DANGEROUS)

  • Description: Deletes a file, or an entire folder
  • Parameters:
    • directoryPath (string): File to delete
  • Example:
file.deleteDir('message.txt');

createFile(filePath)

  • Description: Creates a file
  • Parameters:
    • filePath (string): Path and file name
  • Example:
file.createFile('message.txt');

createDir(directoryPath)

  • Description: Creates a directory
  • Parameters:
    • directoryPath (string): Directory name
  • Example:
file.createDir('goober/extra silly/VERY silly');

cloneFile(filePath, destinationFolderPath)

  • Description: Clones a file to a specified destination
  • Parameters:
    • filePath (string): Path to file
    • destinationFolderPath (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 file
    • destinationFolderPath (string): New file path
  • Example:
file.cloneDir('goober/very silly/silly car.png', 'goober of the day/silly car.png');

calculateDirSize(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

getNum(min, max)

  • Description: Returns a random number
  • Parameters:
    • min (number): Minimum value
    • max (number): Maximum value
  • Returns (num): Random number
  • Example:
const num = random.getNum(0, 10);

console.log(num);

getInt(min, max)

  • Description: Returns a random whole number
  • Parameters:
    • min (number): Minimum value
    • max (number): Maximum value
  • Returns (num): Random number
  • Example:
const num = random.getInt(0, 10);

console.log(num);

getItem(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.getItem(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);

getChance(...chances)

  • Description: Generates a random chance based on the inputed values
  • Parameters:
    • ...chances (number): Chances
  • Example:
const animals = ['cat', 'dog', 'bird'];
const chance = random.getChance(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();

editLastLine(newLine)

  • Description: Edits the last line printed to the console
  • Parameters:
    • newLine (string): New console text
  • Example:
console.log('Not silly :(');
setTimeout(() => {
    cli.editLastLine('Silliest as can be :3');
}, 2000);

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 seconds
    • showCountdown (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!');