Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial implementation ripped out of bouncy with a string test

  • Loading branch information...
commit 86fbf92db670a281bd02f9877f37849b1040decd 0 parents
@substack authored
Showing with 76 additions and 0 deletions.
  1. +15 −0 index.js
  2. +36 −0 package.json
  3. +25 −0 test/chunks.js
15 index.js
@@ -0,0 +1,15 @@
+module.exports = function (msg) {
+ var chunks = [];
+ for (
+ var i = 0, j = Math.floor(Math.random() * (msg.length + 1));
+ j <= msg.length;
+ j += Math.floor(Math.random() * (msg.length - j + 1))
+ ) {
+ var c = msg.slice(i, j);
+ chunks.push(c);
+ i = j;
+ if (j === msg.length) break;
+ }
+
+ return chunks;
+}
36 package.json
@@ -0,0 +1,36 @@
+{
+ "name" : "chunky",
+ "description" : "Break up messages into randomly-sized chunks",
+ "version" : "0.0.0",
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/node-chunky.git"
+ },
+ "main" : "index.js",
+ "keywords" : [
+ "chunk",
+ "random",
+ "test"
+ ],
+ "directories" : {
+ "lib" : ".",
+ "example" : "example",
+ "test" : "test"
+ },
+ "scripts" : {
+ "test" : "tap test/*.js"
+ },
+ "dependencies" : {},
+ "devDependencies" : {
+ "tap" : "0.0.x"
+ },
+ "engines" : {
+ "node" : ">=0.4.0"
+ },
+ "license" : "MIT",
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ }
+}
25 test/chunks.js
@@ -0,0 +1,25 @@
+var chunky = require('../');
+var test = require('tap').test;
+
+test('string chunks', function (t) {
+ t.plan(100 + 4);
+
+ var dist = {};
+ for (var i = 0; i < 100; i++) {
+ var msg = [
+ 'This is a message from the future:',
+ ' Disregard computers. Obtain molybdenum.'
+ ].join('\r\n');
+
+ var chunks = chunky(msg);
+ t.equal(chunks.join(''), msg);
+ dist[chunks.length] = (dist[chunks.length] || 0) + 1;
+ }
+
+ t.ok(dist[5] >= 5);
+ t.ok(dist[6] >= 5);
+
+ t.ok(dist[5] > (dist[2] || 0));
+ t.ok(dist[6] > (dist[10] || 0));
+ t.end();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.