Socket
Book a DemoInstallSign in
Socket

human-readable-id-gen

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

human-readable-id-gen

Library for generating human-readable id strings

0.0.2
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

human-readable-id-gen

Library for generating human-readable id strings.

Basic Usage

const { generateId } = require('human-readable-id-gen');

console.log(generateId());
// ex. Regina's required request

console.log(generateId());
// ex. Garrett's graceful giraffe

Custom Generators

The generateId function randomly pulls words from a default collection of nouns, adjectives, and names. To pull from a different set of words, you must use the IdGenerator class and its #generate method.

Using words in memory

Use the IdGenerator constructor with instances of WordCollection.

const { IdGenerator, WordCollection } = require('human-readable-id-gen');

let nouns = new WordCollection(['chair', 'tie', 'lamp']);
let adjectives = new WordCollection(['good', 'bad', 'ugly']);
let names = new WordCollection(['Tom', 'Dick', 'Harry']);

let idGenerator = new IdGenerator(nouns, adjectives, names);

console.log(idGenerator.generate());
// ex. Tom's ugly tie

Note that alliteration is not guaranteed if, for any word in the nouns collection, there isn't a word beginning with the same letter in both the adjectives and names collections, as above (adjectives has no words beginning with 't').

Loading from files

Use IdGenerator.load to load words from text files. These files should contain words separated by newlines.

const { IdGenerator } = require('human-readable-id-gen');

let idGenerator = IdGenerator.load(
	'path/to/nouns.txt',
	'path/to/adjectives.txt',
	'path/to/names.txt'
);

console.log(idGenerator.generate());
// ex. Leanna's logical laundry

Individual WordCollection instances can also be loaded from files:

const { IdGenerator, WordCollection } = require('human-readable-id-gen');

let nouns = WordCollection.load('path/to/nouns.txt');
let adjectives = new WordCollection(['good', 'bad', 'ugly']);
let names = new WordCollection(['Tom', 'Dick', 'Harry']);

let idGenerator = new IdGenerator(nouns, adjectives, names);

console.log(idGenerator.generate());
// ex. Harry's bad hair

Keywords

id-generator

FAQs

Package last updated on 16 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.