Browse files

first commit, version 0.0.1

  • Loading branch information...
0 parents commit e443d31a00019c114f2883a307c209716032f579 @linus committed Sep 14, 2011
Showing with 154 additions and 0 deletions.
  1. +59 −0 README.md
  2. +80 −0 index.js
  3. +15 −0 package.json
59 README.md
@@ -0,0 +1,59 @@
+# Greg: Create unique, memorable ids in Node.js
+
+`greg` is a simple module to let you generate (probably) unique, memorable ids in Node.js applications.
+
+It creates sentences on the form count + adjective + noun + verb + adverb, as described in [this Aptana blog post](http://asana.com/2011/09/6-sad-squid-snuggle-softly/ "6 sad squids snuggle softly") by Greg Slovacek, whom this module is named after.
+
+Disclaimer: I wrote this in a fit of insomnia late at night after reading the above blog post. Accordingly, it's probably chock-full of bugs and there are too few words. Pull requests are welcome.
+
+## Version
+0.0.1
+
+## Requirements
+- [Node](http://github/ry/node)
+
+## Installation
+
+Via npm:
+
+ npm install greg
+
+## Usage
+
+ var greg = require("greg"),
+ sentence = greg.sentence();
+
+ console.log("The sentence \"%s\" corresponds to the id %d", sentence, greg.parse(sentence));
+
+## Credits
+
+Linus G Thiel <linus@hanssonlarsson.se>
+
+## Thank you
+
+Greg Slovacek
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010 Hansson & Larsson <info@hanssonlarsson.se>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 index.js
@@ -0,0 +1,80 @@
+// Generate a Greg sentence
+exports.sentence = function sentence() {
+ function random(max) {
+ return Math.floor(Math.random() * max);
+ }
+
+ function randomItem(array) {
+ return array[random(array.length)];
+ }
+
+ var count = random(33) + 2,
+ adjective = randomItem(exports.adjectives),
+ noun = randomItem(exports.nouns),
+ verb = randomItem(exports.verbs),
+ adverb = randomItem(exports.adverbs);
+
+ return [count, adjective, noun, verb, adverb].join(" ");
+};
+
+// Parse a Greg sentence and return it's corresponding id
+exports.parse = function parse(sentence) {
+ var words = sentence.split(" "),
+ adjectiveFactor = 32,
+ nounFactor = adjectiveFactor * exports.adjectives.length,
+ verbFactor = nounFactor * exports.nouns.length,
+ adverbFactor = verbFactor * exports.verbs.length,
+ count = parseInt(words[0], 10),
+ adjective = exports.adjectives.indexOf(words[1]),
+ noun = exports.nouns.indexOf(words[2])
+ verb = exports.verbs.indexOf(words[3])
+ adverb = exports.adverbs.indexOf(words[4]);
+
+ return count +
+ adjective * adjectiveFactor +
+ noun * nounFactor +
+ verb * verbFactor +
+ adverb * adverbFactor;
+};
+
+// English adjectives
+exports.adjectives = [
+ "cute", "dapper", "large", "small", "long", "short", "thick", "narrow",
+ "deep", "flat", "whole", "low", "high", "near", "far", "fast",
+ "quick", "slow", "early", "late", "bright", "dark", "cloudy", "warm",
+ "cool", "cold", "windy", "noisy", "loud", "quiet", "dry", "clear",
+ "hard", "soft", "heavy", "light", "strong", "weak", "tidy", "clean",
+ "dirty", "empty", "full", "close", "thirsty", "hungry", "fat", "old",
+ "fresh", "dead", "healthy", "sweet", "sour", "bitter", "salty", "good",
+ "bad", "great", "important", "useful", "expensive", "cheap", "free", "difficult",
+ "strong", "weak", "able", "free", "rich", "afraid", "brave", "fine",
+ "sad", "proud", "comfortable", "happy", "clever", "interesting", "famous", "exciting",
+ "funny", "kind", "polite", "fair", "share", "busy", "free", "lazy",
+ "lucky", "careful", "safe", "dangerous"
+];
+
+// English plural nouns (all animals)
+exports.nouns = [
+ "rabbits", "badgers", "foxes", "chickens", "bats", "deer", "snakes", "hares",
+ "hedgehogs", "platypuses", "moles", "mice", "otters", "rats", "squirrels", "stoats",
+ "weasels", "crows", "doves", "ducks", "geese", "hawks", "herons", "kingfishers",
+ "owls", "peafowl", "pheasants", "pigeons", "robins", "rooks", "sparrows", "starlings",
+ "swans", "ants", "bees", "butterflies", "dragonflies", "flies", "moths", "spiders",
+ "pikes", "salmons", "trouts", "frogs", "newts", "toads", "crabs", "lobsters",
+ "clams", "cockles", "mussles", "oysters", "snails", "cattle", "dogs", "donkeys",
+ "goats", "horses", "pigs", "sheep", "ferrets", "gerbils", "guinea pigs", "parrots",
+ "greg"
+];
+
+// English verbs, past tense
+exports.verbs = [
+ "sang", "played", "knitted", "floundered", "danced", "played", "listened", "ran",
+ "talked", "cuddled", "sat", "kissed", "hugged", "whimpered", "hid", "fought",
+ "whispered", "cried", "snuggled", "walked", "drove", "loitered", "whimpered", "felt",
+ "jumped", "hopped", "went", "married", "engaged",
+];
+
+// English adverbs
+exports.adverbs = [
+ "jovially", "merrily", "cordially", "easily"
+];
15 package.json
@@ -0,0 +1,15 @@
+{
+ "name": "greg",
+ "description": "Unique, memorable ids for your Node app",
+ "version": "0.0.1",
+ "author": "Linus G Thiel <linus@hanssonlarsson.se>",
+ "contributors": [
+ ],
+ "dependencies": {},
+ "keywords": ["word", "id", "generator", "error code"],
+ "engines": { "node": ">= 0.2.0" },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/linus/greg.git"
+ }
+}

0 comments on commit e443d31

Please sign in to comment.