├── package.json
├── Nonsense.html
├── README.md
└── Nonsense.js
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "Nonsense",
3 | "version": "0.1.3",
4 | "author": "Josh Faul ",
5 | "main": "./Nonsense",
6 | "description": "Create repeatable random information",
7 | "repository": "git://github.com/jocafa/Nonsense",
8 | "main": "Nonsense.js"
9 | }
10 |
--------------------------------------------------------------------------------
/Nonsense.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
11 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Nonsense
2 | ========
3 |
4 | Generate repeatable random data in JS
5 |
6 | Inspired by [Faker.js](http://github.com/Marak/Faker.js)
7 | Uses slightly modified [Alea PRNG](http://baagoe.org/en/wiki/Alea)
8 |
9 | ## Getting started
10 |
11 | For the server, install Nonsense via [npm][npm].
12 | ```shell
13 | npm install Nonsense
14 | ```
15 |
16 | For the browser, download [Nonsense.js][nonsense.js], and include it as a script tag.
17 |
18 | ```html
19 |
20 |
24 | ```
25 |
26 | [npm]: https://npmjs.org/
27 | [nonsense.js]: https://raw.github.com/jocafa/Nonsense/master/Nonsense.js
28 |
29 | ## Usage
30 |
31 | ### Instantiation
32 | To create a new Nonsense instance, do `var ns = new Nonsense();`. You can pass any number of arbitrary arguments to the `Nonsense()` constructor to be used as seed data. If you don't pass anything, it will just use the default.
33 |
34 | ### Seeding
35 | If you want to reset the seed of an instance you already have, call `ns.sow()` and pass in the seed data you want to use. The constructor calls `sow()` internally on instantiation.
36 |
37 | ### Numbers
38 | - `integer()` - returns a random integer between 0 and 2^32
39 | - `frac()` - returns a random real number between 0 and 1
40 | - `real()` - returns a random real number between 0 and 2^32
41 | - `integerInRange(min, max)` - returns a random integer between min and max
42 | - `realInRange(min, max)` - returns a random real number between min and max
43 | - `normal()` - returns a random real number between -1 and 1
44 |
45 | ### Utilities
46 | - `uuid()` - returns a valid v4 UUID hex string
47 | - `pick(array)` - returns a random member of `array`
48 | - `weightedPick(array)` - returns a random member of `array`, favoring the earlier entries
49 | - `timestamp(min, max)` - returns a random timestamp between min and max, or between the beginning of 2000 and the end of 2020 if min and max aren't specified
50 |
51 | ### Language
52 | - `word()` - returns a random word of lipsum
53 | - `words(n)` - returns `n` random words of lipsum, 3 if not specified
54 | - `sentence()` - returns a random lipsum sentence
55 | - `sentences(n)` - returns `n` random lipsum sentences, 3 if not specified
56 |
57 | ### Miscellaneous
58 | - `firstName()` - returns a random common first name
59 | - `lastName()` - returns a random common last name
60 | - `name()` - returns a random first and last name
61 | - `jobTitle()` - returns a random job title
62 | - `buzzPhrase()` - returns a random web 2.0 business plan...
63 |
64 | License
65 | -------
66 |
67 | Do whatever you want with this code. The consequenses of your actions are your own responsibility.
68 |
--------------------------------------------------------------------------------
/Nonsense.js:
--------------------------------------------------------------------------------
1 | (function() {
2 | // random number generator from http://baagoe.org/en/wiki/Better_random_numbers_for_javascript
3 | var DATA;
4 |
5 | function hash (data) {
6 | var h, i, n;
7 |
8 | n = 0xefc8249d;
9 |
10 | data = data.toString();
11 |
12 | for (i = 0; i < data.length; i++) {
13 | n += data.charCodeAt(i);
14 | h = 0.02519603282416938 * n;
15 | n = h >>> 0;
16 | h -= n;
17 | h *= n;
18 | n = h >>> 0;
19 | h -= n;
20 | n += h * 0x100000000; // 2^32
21 | }
22 |
23 | return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
24 | }
25 |
26 | // private random helper
27 | function rnd () {
28 | var t = 2091639 * this.s0 + this.c * 2.3283064365386963e-10; // 2^-32
29 |
30 | this.c = t | 0;
31 | this.s0 = this.s1;
32 | this.s1 = this.s2;
33 | this.s2 = t - this.c;
34 | return this.s2;
35 | }
36 |
37 | function Nonsense () {
38 | this.sow.apply(this, arguments);
39 | }
40 |
41 | Nonsense.prototype.sow = function () {
42 | var i, seeds, seed;
43 |
44 | this.s0 = hash(' ');
45 | this.s1 = hash(this.s0);
46 | this.s2 = hash(this.s1);
47 | this.c = 1;
48 |
49 | seeds = Array.prototype.slice.call(arguments);
50 |
51 | for (i = 0; seed = seeds[i++];) {
52 | this.s0 -= hash(seed);
53 | this.s0 += ~~(this.s0 < 0);
54 |
55 | this.s1 -= hash(seed);
56 | this.s1 += ~~(this.s1 < 0);
57 |
58 | this.s2 -= hash(seed);
59 | this.s2 += ~~(this.s2 < 0);
60 | }
61 | };
62 |
63 |
64 | Nonsense.prototype.uint32 = function () {
65 | return rnd.apply(this) * 0x100000000; // 2^32
66 | };
67 |
68 | Nonsense.prototype.fract32 = function () {
69 | return rnd.apply(this) + (rnd.apply(this) * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
70 | };
71 |
72 | Nonsense.prototype.bool = function () {
73 | return this.uint32() & 0x1 ? true : false;
74 | };
75 |
76 | Nonsense.prototype.integer = function() {
77 | return this.uint32();
78 | };
79 |
80 | Nonsense.prototype.frac = function() {
81 | return this.fract32();
82 | };
83 |
84 | Nonsense.prototype.real = function() {
85 | return this.uint32() + this.fract32();
86 | };
87 |
88 | Nonsense.prototype.integerInRange = function(min, max) {
89 | return Math.floor(this.realInRange(min, max));
90 | };
91 |
92 | Nonsense.prototype.realInRange = function(min, max) {
93 | min = min || 0;
94 | max = max || 0;
95 | return this.frac() * (max - min) + min;
96 | };
97 |
98 | Nonsense.prototype.normal = function() {
99 | return 1 - 2 * this.frac();
100 | };
101 |
102 | Nonsense.prototype.uuid = function() {
103 | // from https://gist.github.com/1308368
104 | var a, b;
105 | for (
106 | b=a='';
107 | a++<36;
108 | b+=~a%5|a*3&4?(a^15?8^this.frac()*(a^20?16:4):4).toString(16):'-'
109 | );
110 | return b;
111 | };
112 |
113 | Nonsense.prototype.pick = function(ary) {
114 | return ary[this.integerInRange(0, ary.length)];
115 | };
116 |
117 | Nonsense.prototype.weightedPick = function(ary) {
118 | return ary[~~(Math.pow(this.frac(), 2) * ary.length)];
119 | };
120 |
121 | // Language ----------------------------------------------------------------
122 | Nonsense.prototype.word = function() {
123 | return this.pick(DATA.lipsum);
124 | };
125 |
126 | Nonsense.prototype.words = function(num) {
127 | num = num || 3;
128 | var ret = [];
129 | for (var i = 0; i < num; i++) {
130 | ret.push(this.pick(DATA.lipsum));
131 | }
132 | return ret.join(' ');
133 | };
134 |
135 | Nonsense.prototype.sentence = function() {
136 | var ret;
137 | ret = this.words(this.integerInRange(2, 16)).replace(/[a-z]/, function(m) {
138 | return m.toUpperCase();
139 | });
140 | return ret + '.';
141 | };
142 |
143 | Nonsense.prototype.sentences = function(num) {
144 | num = num || 3;
145 | var ret = [];
146 | for (var i = 0; i < num; i++) {
147 | ret.push(this.sentence());
148 | }
149 | return ret.join(' ');
150 | };
151 |
152 | // Time --------------------------------------------------------------------
153 | Nonsense.prototype.timestamp = function(a, b) {
154 | return this.realInRange(a || 946684800000, b || 1577862000000);
155 | };
156 |
157 | // People ------------------------------------------------------------------
158 | Nonsense.prototype.gender = function () {
159 | return this.bool() ? 'male' : 'female';
160 | };
161 |
162 | Nonsense.prototype.firstName = function(gender) {
163 | gender = gender || this.gender();
164 | return "" + (this.pick(DATA.names.first[gender]));
165 | };
166 |
167 | Nonsense.prototype.lastName = function() {
168 | return "" + (this.pick(DATA.names.last));
169 | };
170 |
171 | Nonsense.prototype.name = function(gender) {
172 | return "" + (this.firstName(gender)) + " " + (this.lastName());
173 | };
174 |
175 | Nonsense.prototype.jobTitle = function() {
176 | return "" + (this.pick(DATA.departments)) + " " + (this.pick(DATA.positions));
177 | };
178 |
179 | Nonsense.prototype.buzzPhrase = function() {
180 | return "" + (this.pick(DATA.buzz.verbs)) + " " + (this.pick(DATA.buzz.adjectives)) + " " + (this.pick(DATA.buzz.nouns));
181 | };
182 |
183 | // Dataset -----------------------------------------------------------------
184 | DATA = {
185 | lipsum: [
186 | "lorem", "ipsum", "dolor", "sit", "amet", "consectetur",
187 | "adipiscing", "elit", "nunc", "sagittis", "tortor", "ac", "mi",
188 | "pretium", "sed", "convallis", "massa", "pulvinar", "curabitur",
189 | "non", "turpis", "velit", "vitae", "rutrum", "odio", "aliquam",
190 | "sapien", "orci", "tempor", "sed", "elementum", "sit", "amet",
191 | "tincidunt", "sed", "risus", "etiam", "nec", "lacus", "id", "ante",
192 | "hendrerit", "malesuada", "donec", "porttitor", "magna", "eget",
193 | "libero", "pharetra", "sollicitudin", "aliquam", "mattis", "mattis",
194 | "massa", "et", "porta", "morbi", "vitae", "magna", "augue",
195 | "vestibulum", "at", "lectus", "sed", "tellus", "facilisis",
196 | "tincidunt", "suspendisse", "eros", "magna", "consequat", "at",
197 | "sollicitudin", "ac", "vestibulum", "vel", "dolor", "in", "egestas",
198 | "lacus", "quis", "lacus", "placerat", "et", "molestie", "ipsum",
199 | "scelerisque", "nullam", "sit", "amet", "tortor", "dui", "aenean",
200 | "pulvinar", "odio", "nec", "placerat", "fringilla", "neque", "dolor"
201 | ],
202 | names: {
203 | first: {
204 | // Top names from 1913-2012
205 | // Data from http://www.ssa.gov/OACT/babynames/decades/century.html
206 | male: [
207 | "James", "John", "Robert", "Michael", "William", "David", "Richard",
208 | "Joseph", "Charles", "Thomas", "Christopher", "Daniel", "Matthew",
209 | "Donald", "Anthony", "Paul", "Mark", "George", "Steven", "Kenneth",
210 | "Andrew", "Edward", "Brian", "Joshua", "Kevin", "Ronald", "Timothy",
211 | "Jason", "Jeffrey", "Gary", "Ryan", "Nicholas", "Eric", "Stephen",
212 | "Jacob", "Larry", "Frank", "Jonathan", "Scott", "Justin", "Raymond",
213 | "Brandon", "Gregory", "Samuel", "Patrick", "Benjamin", "Jack",
214 | "Dennis", "Jerry", "Alexander", "Tyler", "Douglas", "Henry", "Peter",
215 | "Walter", "Aaron", "Jose", "Adam", "Harold", "Zachary", "Nathan",
216 | "Carl", "Kyle", "Arthur", "Gerald", "Lawrence", "Roger", "Albert",
217 | "Keith", "Jeremy", "Terry", "Joe", "Sean", "Willie", "Jesse",
218 | "Ralph", "Billy", "Austin", "Bruce", "Christian", "Roy", "Bryan",
219 | "Eugene", "Louis", "Harry", "Wayne", "Ethan", "Jordan", "Russell",
220 | "Alan", "Philip", "Randy", "Juan", "Howard", "Vincent", "Bobby",
221 | "Dylan", "Johnny", "Phillip", "Craig"
222 | ],
223 |
224 | female: [
225 | "Mary", "Patricia", "Elizabeth", "Jennifer", "Linda", "Barbara",
226 | "Susan", "Margaret", "Jessica", "Dorothy", "Sarah", "Karen", "Nancy",
227 | "Betty", "Lisa", "Sandra", "Helen", "Donna", "Ashley", "Kimberly",
228 | "Carol", "Michelle", "Amanda", "Emily", "Melissa", "Laura", "Deborah",
229 | "Stephanie", "Rebecca", "Sharon", "Cynthia", "Ruth", "Kathleen",
230 | "Anna", "Shirley", "Amy", "Angela", "Virginia", "Brenda", "Pamela",
231 | "Catherine", "Katherine", "Nicole", "Christine", "Janet", "Debra",
232 | "Carolyn", "Samantha", "Rachel", "Heather", "Maria", "Diane",
233 | "Frances", "Joyce", "Julie", "Martha", "Joan", "Evelyn", "Kelly",
234 | "Christina", "Emma", "Lauren", "Alice", "Judith", "Marie", "Doris",
235 | "Ann", "Jean", "Victoria", "Cheryl", "Megan", "Kathryn", "Andrea",
236 | "Jacqueline", "Gloria", "Teresa", "Janice", "Sara", "Rose", "Julia",
237 | "Hannah", "Theresa", "Judy", "Mildred", "Grace", "Beverly", "Denise",
238 | "Marilyn", "Amber", "Danielle", "Brittany", "Diana", "Jane", "Lori",
239 | "Olivia", "Tiffany", "Kathy", "Tammy", "Crystal", "Madison"
240 | ]
241 | },
242 |
243 | last: [
244 | "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis",
245 | "Miller", "Wilson", "Moore", "Taylor", "Anderson", "Thomas",
246 | "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia",
247 | "Martinez", "Robinson", "Clark", "Rodriguez", "Lewis", "Lee",
248 | "Walker", "Hall", "Allen", "Young", "Hernandez", "King",
249 | "Wright", "Lopez", "Hill", "Scott", "Green", "Adams", "Baker",
250 | "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Roberts",
251 | "Turner", "Phillips", "Campbell", "Parker", "Evans", "Edwards",
252 | "Collins", "Stewart", "Sanchez", "Morris", "Rogers", "Reed",
253 | "Cook", "Morgan", "Bell", "Murphy", "Bailey", "Rivera",
254 | "Cooper", "Richardson", "Cox", "Howard", "Ward", "Torres",
255 | "Peterson", "Gray", "Ramirez", "James", "Watson", "Brooks",
256 | "Kelly", "Sanders", "Price", "Bennett", "Wood", "Barnes",
257 | "Ross", "Henderson", "Coleman", "Jenkins", "Perry", "Powell",
258 | "Long", "Patterson", "Hughes", "Flores", "Washington", "Butler",
259 | "Simmons", "Foster", "Gonzales", "Bryant", "Alexander",
260 | "Russell", "Griffin", "Diaz", "Hayes"
261 | ]
262 | },
263 |
264 | departments: ['HR', 'IT', 'Marketing', 'Engineering', 'Sales'],
265 |
266 | positions: ['Director', 'Manager', 'Team Lead', 'Team Member'],
267 |
268 | internet: {
269 | tlds: ['.com', '.net', '.org', '.edu', '.co.uk']
270 | },
271 |
272 | buzz: {
273 | nouns: [
274 | "action-items", "applications", "architectures", "bandwidth",
275 | "channels", "communities", "content", "convergence",
276 | "deliverables", "e-business", "e-commerce", "e-markets",
277 | "e-services", "e-tailers", "experiences", "eyeballs",
278 | "functionalities", "infomediaries", "infrastructures",
279 | "initiatives", "interfaces", "markets", "methodologies",
280 | "metrics", "mindshare", "models", "networks", "niches",
281 | "paradigms", "partnerships", "platforms", "portals",
282 | "relationships", "ROI", "schemas", "solutions", "supply-chains",
283 | "synergies", "systems", "technologies", "users", "vortals",
284 | "web services", "web-readiness"
285 | ],
286 | adjectives: [
287 | "24/365", "24/7", "B2B", "B2C", "back-end", "best-of-breed",
288 | "bleeding-edge", "bricks-and-clicks", "clicks-and-mortar",
289 | "collaborative", "compelling", "cross-media", "cross-platform",
290 | "customized", "cutting-edge", "distributed", "dot-com",
291 | "dynamic", "e-business", "efficient", "end-to-end",
292 | "enterprise", "extensible", "frictionless", "front-end",
293 | "global", "granular", "holistic", "impactful", "innovative",
294 | "integrated", "interactive", "intuitive", "killer",
295 | "leading-edge", "magnetic", "mission-critical", "multiplatform",
296 | "next-generation", "one-to-one", "open-source",
297 | "out-of-the-box", "plug-and-play", "proactive", "real-time",
298 | "revolutionary", "rich", "robust", "scalable", "seamless",
299 | "sexy", "sticky", "strategic", "synergistic", "transparent",
300 | "turn-key", "ubiquitous", "user-centric", "value-added",
301 | "vertical", "viral", "virtual", "visionary", "web-enabled",
302 | "wireless", "world-class"
303 | ],
304 | verbs: [
305 | "aggregate", "architect", "benchmark", "brand", "cultivate",
306 | "deliver", "deploy", "disintermediate", "drive", "e-enable",
307 | "embrace", "empower", "enable", "engage", "engineer", "enhance",
308 | "envisioneer", "evolve", "expedite", "exploit", "extend",
309 | "facilitate", "generate", "grow", "harness", "implement",
310 | "incentivize", "incubate", "innovate", "integrate", "iterate",
311 | "leverage", "matrix", "maximize", "mesh", "monetize", "morph",
312 | "optimize", "orchestrate", "productize", "recontextualize",
313 | "redefine", "reintermediate", "reinvent", "repurpose",
314 | "revolutionize", "scale", "seize", "strategize", "streamline",
315 | "syndicate", "synergize", "synthesize", "target", "transform",
316 | "transition", "unleash", "utilize", "visualize", "whiteboard"
317 | ]
318 | }
319 | };
320 |
321 | if (typeof module !== 'undefined') {
322 | module.exports = Nonsense;
323 | } else if (typeof define == 'function') {
324 | define(function () {
325 | return Nonsense;
326 | });
327 | } else {
328 | this.Nonsense = Nonsense;
329 | }
330 | }).call(this);
331 |
--------------------------------------------------------------------------------