Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from a-type/gfycat
Browse files Browse the repository at this point in the history
Gfycat animals, more options
  • Loading branch information
a-type committed Jun 23, 2015
2 parents 946f34e + ee4ced7 commit b858650
Show file tree
Hide file tree
Showing 8 changed files with 10,822 additions and 644 deletions.
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# adjective-adjective-animal
Suitably random and reasonably unique (and fairly adorable) human readable ids
### Suitably random and reasonably unique (and fairly adorable) human readable ids

> Fun fact: most if not all of the adjectives are Scribblenauts-compatible. Use that information at your own discretion.
# Usage
> Now with GfyCat lists!
## Usage
The library export is a function. Call the function with the number of adjectives you want before the animal. Default is 2.

The function returns a `Promise` for the adjective-animal string. This is mainly because generating cyrptographically strong random data is not guaranteed to be very quick.
The function returns a Promise for the adjective-animal string. This is mainly because generating cyrptographically strong random data is not guaranteed to be very quick.

``` javascript
var generate = require("adjective-adjective-animal");

generate().then(console.log);
// "supercurious-senior-woodlouse"

var generate = require("adjective-adjective-animal");
generate(5).then(console.log);
// "unquiet-calm-omniscient-ornate-industrious-deer"

generate().then(console.log);
// "supercurious-senior-woodlouse"
// valid formats : upper (spaces), lower (spaces), sentence (spaces), title (spaces),
// camel, pascal, snake, param, dot, path, constant, swap, ucFirst, lcFirst
generate("pascal").then(console.log);
// "OddPortentBullfrog"

generate(5).then(console.log);
// "unquiet-calm-omniscient-ornate-industrious-deer"
generate({ adjectives : 3, format : "dot"}).then(console.log);
// "undead.energetic.mortified.albatross"
```

```
# About
## About
There's nothing too special about this package—there are many like it—but I made this one because I thought it would be fun and I wanted mine to be cryptographically strong. Although the space is probably too small to guarantee any sort of uniqueness reliably, at least the randomness is not predictable. It uses node's core crypto library to choose each word.

There's nothing too special about this package—there are many like it—but I made this one because I thought it would be fun and I wanted mine to be cryptographically strong. Although the space is probably too small to guarantee any sort of uniqueness reliably, at least the randomness is not predictable. It uses node's core `crypto` library to choose each word.
## Thanks
Thanks to [@ChrissiQ](https://github.com/ChrissiQ) for pointing me in the right direction to use GfyCat lists instead of my own.
4 changes: 2 additions & 2 deletions lib/cryptoRand.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
return randomBytes(2) // 0 - 65535
.then(function (bytes) {
var num = bytes.readUInt16LE(0);
num = num % 16384; // smallest power of 2 > num adjectives (8997)
num = num % 16384; // smallest power of 2 > num adjectives (8980)

if (num < loadLists.NUM_ADJECTIVES && lists.adjectives[num]) {
return lists.adjectives[num];
Expand All @@ -28,7 +28,7 @@ module.exports = {
return randomBytes(2) // 0 - 65535
.then(function (bytes) {
var num = bytes.readUInt16LE(0);
num = num % 1024; // smallest power of 2 > num animals (591)
num = num % 2048; // smallest power of 2 > num animals (1750)

if (num < loadLists.NUM_ANIMALS && lists.animals[num]) {
return lists.animals[num];
Expand Down
27 changes: 20 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"use strict";

var Bluebird = require("bluebird");
var randWord = require("./cryptoRand");
var Bluebird = require("bluebird");
var changeCase = require("change-case");
var randWord = require("./cryptoRand");

module.exports = function (numAdjectives) {
if (!numAdjectives) {
numAdjectives = 2; // default
module.exports = Bluebird.method(function (param) {
var numAdjectives = 2; // default
var format = "param";

if (param) {
if (typeof param === "number") {
numAdjectives = param;
}
else if (typeof param === "string") {
format = param;
}
else if (typeof param === "object") {
numAdjectives = param.adjectives || numAdjectives;
format = param.format || format;
}
}

var promises = [];
Expand All @@ -16,6 +29,6 @@ module.exports = function (numAdjectives) {

return Bluebird.all(promises)
.then(function (parts) {
return parts.join("-");
return changeCase[format](parts.join(" "));
});
};
});
4 changes: 2 additions & 2 deletions lib/loadLists.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ module.exports = function () {
return { adjectives : adjectives, animals : animals };
};

module.exports.NUM_ADJECTIVES = 8981;
module.exports.NUM_ANIMALS = 590;
module.exports.NUM_ADJECTIVES = 8980;
module.exports.NUM_ANIMALS = 1750;
9,006 changes: 8,980 additions & 26 deletions lists/adjectives.json

Large diffs are not rendered by default.

Loading

0 comments on commit b858650

Please sign in to comment.