This document provides detailed information about the ascii-cats JavaScript API.
npm install ascii-cats// CommonJS
const asciiCats = require('ascii-cats');
// ES Modules (if using a transpiler)
import asciiCats from 'ascii-cats';The main function that returns ASCII cat art.
catName(String, optional): The name of the specific cat to retrieve. If omitted, returns a random cat.
- (String): ASCII art of the requested cat, with lines joined by newline characters.
// Get a random cat
const randomCat = asciiCats();
console.log(randomCat);
// Get a specific cat
const nyanCat = asciiCats('nyan');
console.log(nyanCat);A property that contains an array of all available cat names.
- (Array): An array of strings representing all available cat names.
// Get all available cat names
const allCatNames = asciiCats.catNames;
console.log(allCatNames);
// ['grumpy', 'approaching', 'tubby', ...]The library does not throw errors for invalid cat names. If a non-existent cat name is provided, the function will return undefined.
// Request a non-existent cat
const nonExistentCat = asciiCats('not-a-real-cat');
console.log(nonExistentCat); // undefinedThe library loads all cat data when it's first required. This means:
- There's a small upfront cost when the module is first loaded
- Subsequent calls are very fast as they're just accessing in-memory data
- The memory footprint is proportional to the number and size of cats in the JSON file
While primarily designed for Node.js, the library can work in browser environments if bundled with tools like Webpack, Browserify, or Parcel. No browser-specific APIs are used.
While the library doesn't include TypeScript definitions natively, you can define them yourself:
declare module 'ascii-cats' {
function asciiCats(catName?: string): string | undefined;
namespace asciiCats {
const catNames: string[];
}
export = asciiCats;
}const asciiCats = require('ascii-cats');
// Display a random cat
console.log(asciiCats());
// Display a specific cat
console.log(asciiCats('nyan'));const asciiCats = require('ascii-cats');
console.log('Available cats:');
asciiCats.catNames.forEach(name => {
console.log(`- ${name}`);
});const asciiCats = require('ascii-cats');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('Available cats:');
asciiCats.catNames.forEach((name, index) => {
console.log(`${index + 1}. ${name}`);
});
rl.question('Select a cat by number: ', (answer) => {
const index = parseInt(answer, 10) - 1;
if (index >= 0 && index < asciiCats.catNames.length) {
const catName = asciiCats.catNames[index];
console.log(`\nYou selected: ${catName}\n`);
console.log(asciiCats(catName));
} else {
console.log('Invalid selection, showing a random cat instead:');
console.log(asciiCats());
}
rl.close();
});