Permalink
Browse files

everything seems to work with size checks

  • Loading branch information...
0 parents commit 8d7c29c98bb6fcf8f85d82cd9e3079a98208606c @substack committed Aug 21, 2011
Showing with 181 additions and 0 deletions.
  1. +67 −0 README.markdown
  2. +100 −0 index.js
  3. +14 −0 test/object.js
@@ -0,0 +1,67 @@
+garbage
+=======
+
+Generate random garbage json data.
+
+example
+=======
+
+````
+> var garbage = require('garbage')
+> garbage()
+4.136307826118192
+> garbage()
+-7.327021010101647
+> garbage()
+{ '0\t4$$c(C&s%': {},
+ '': 2.221633433726717,
+ '!&pQw5': '<~.;@,',
+ 'I$t]hky=': {},
+ '{4/li(MDYX"': [] }
+> garbage()
+false
+````
+
+methods
+=======
+
+var garbage = require('garbage');
+
+garbage(count=20)
+-----------------
+
+Generate a random type of object with at most `count` nested types.
+
+Alias: garbage.json
+
+garbage.object(count=20)
+------------------------
+
+Generate a random object with at most `count` randomly generated descendants.
+
+garbage.array(count=20)
+------------------------
+
+Generate a random array with at most `count` randomly generated descendants.
+
+garbage.string()
+----------------
+
+Generate a randomish string.
+
+garbage.boolean()
+-----------------
+
+Returns true or false.
+
+garbage.number()
+----------------
+
+Returns a random float but more likely to return floats closer to 0.
+
+install
+=======
+
+With [npm](http://npmjs.org) do:
+
+ npm install garbage
100 index.js
@@ -0,0 +1,100 @@
+var garbage = module.exports = function (count) {
+ if (!count) count = 20;
+ return generate(count)[1];
+};
+garbage.json = garbage;
+
+function generate (count) {
+ if (count === undefined) count = 20;
+
+ var types = [ 'array', 'object', 'string', 'number', 'boolean' ];
+ var ix = Math.floor(Math.random() * types.length);
+ var type = types[ix];
+ return generate[type](count);
+}
+
+garbage.size = function () {
+ var m = Math.random(), n = Math.random();
+ return Math.abs(Math.floor(5 / (1 - n * n) - 5 + m * 5));
+};
+
+generate.boolean = function () {
+ var n = Math.random();
+ return [ 1, n > 0.5 ];
+};
+
+garbage.boolean = function () {
+ return generate.boolean(count)[1];
+};
+
+generate.number = function () {
+ var n = Math.random();
+ return [ 1, Math.tan((n - 0.5) * Math.PI) ];
+};
+
+garbage.number = function () {
+ return generate.number(count)[1];
+};
+
+generate.array = function (count) {
+ var res = [];
+ if (count <= 0) return res;
+ var len = garbage.size();
+ var size = 1;
+ for (var i = 0; i < len && size < count; i++) {
+ var x = generate(--count - len);
+ res.push(x[1]);
+ size += x[0];
+ }
+ return [ size, res ];
+};
+
+garbage.array = function (count) {
+ return generate.array(count)[1];
+};
+
+generate.object = function (count) {
+ if (count === undefined) count = 20;
+
+ var res = {};
+ if (count <= 0) return res;
+ var len = garbage.size();
+ var size = 1;
+ for (var i = 0; i < len && size < count; i++) {
+ var key = generate.string()[1];
+ var x = generate(--count - len);
+ res[key] = x[1];
+ size += x[0];
+ }
+ return [ size, res ];
+};
+
+garbage.object = function (count) {
+ return generate.object(count)[1];
+};
+
+generate.string = function () {
+ var len = Math.floor(
+ Math.random() * Math.random() * 20
+ + Math.random() * 2
+ );
+ var res = [];
+ for (var i = 0; i < len; i++) {
+ var c = garbage.char();
+ res.push(c);
+ }
+ return [ 1, res.join('') ];
+};
+
+garbage.string = function () {
+ return generate.string()[1];
+};
+
+garbage.char = function () {
+ var charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ + 'abcdefghijklmnopqrstuvwxyz'
+ + '0123456789`~!@#$%^&*()-_=+[]{}|\\:;'
+ + '\'",.<>?/ \t\n'
+ ;
+ return charset.charAt(Math.floor(Math.random() * charset.length));
+};
@@ -0,0 +1,14 @@
+var test = require('tap').test;
+var traverse = require('traverse');
+var garbage = require('../');
+
+test('object', function (t) {
+ t.plan(30 * 2);
+ for (var i = 0; i < 30; i++) {
+ var obj = garbage.object(5);
+ t.equal(typeof obj, 'object');
+ t.ok(traverse.nodes(obj).length <= 5, 'too many objects');
+ }
+
+ t.end();
+});

0 comments on commit 8d7c29c

Please sign in to comment.