Permalink
Browse files

Initial commit

  • Loading branch information...
creationix committed Jan 12, 2010
0 parents commit 987651c1f82f62f6074e63b30dcf354b0c38b8f7
Showing with 745 additions and 0 deletions.
  1. +1 −0 README.markdown
  2. +71 −0 json-ext.js
  3. +150 −0 mazemaker.js
  4. +37 −0 smtp.js
  5. +197 −0 turtle.js
  6. +289 −0 ui.menu.js
@@ -0,0 +1 @@
+This is a collection of various small projects that I don't want to lose.
@@ -0,0 +1,71 @@
+// Add ruby-like JSON ability to the standard JSON code.
+(function () {
+
+ // A recursive function that traverses the raw object looking for special
+ // serialized objects to be de-serialized. Used by `JSON.load`
+ var json_object_load = function (obj) {
+ var key;
+ if (typeof obj.json_class === 'string') {
+ return (eval(obj.json_class)).jsonCreate(obj);
+ }
+ if (typeof obj === 'object') {
+ for (key in obj) {
+ obj[key] = json_object_load(obj[key]);
+ }
+ }
+ return obj;
+ };
+
+ // dump is just an alias of stringify since the needed `toJSON` hook is
+ // already in the stringify implementation.
+ JSON.dump = JSON.stringify;
+
+ // Works like JSON.parse, but then filters the output looking for object
+ // hooks. Allows custom objects to be de-serialized.
+ JSON.load = function (text) { return json_object_load(JSON.parse(text)); };
+
+ // Adds generic object hooks to about any object constructor.
+ JSON.extend = function (class) {
+
+ // Unserializes an object from JSON
+ class.jsonCreate = function(o) {
+ var obj, data, O, prototype;
+ O = function () {};
+ obj = new O();
+ prototype = class.prototype;
+ for (key in prototype) {
+ if (prototype.hasOwnProperty(key)) {
+ O.prototype[key] = prototype[key];
+ }
+ }
+ O.name = class.name;
+ obj.constructor = class;
+ data = o.data;
+ for (key in data) {
+ if (data.hasOwnProperty(key)) {
+ obj[key] = data[key];
+ }
+ }
+ return obj;
+ };
+
+ // Serialize any object to JSON
+ class.prototype.toJSON = function() {
+ var data = {};
+ if (this.constructor.name === 'Object') {
+ return this;
+ }
+ for (key in this) {
+ if (this.hasOwnProperty(key) && typeof this[key] !== 'function') {
+ data[key] = this[key];
+ }
+ }
+ return {
+ 'json_class': this.constructor.name,
+ 'data': data
+ };
+ };
+ };
+
+
+}());
@@ -0,0 +1,150 @@
+#!/usr/local/bin/node
+
+Array.prototype.shuffle = function () {
+ var i, j, tempi, tempj;
+ i = this.length;
+ if ( i === 0 ) {
+ return false;
+ }
+ while ( --i ) {
+ j = Math.floor( Math.random() * ( i + 1 ) );
+ tempi = this[i];
+ tempj = this[j];
+ this[i] = tempj;
+ this[j] = tempi;
+ }
+};
+
+Number.prototype.times = function (fn) {
+ for (var i = 0; i < this; i += 1) {
+ fn(i);
+ }
+};
+
+var Maze;
+
+(function () {
+
+ function get_root(node) {
+ if (node.parent) {
+ return get_root(node.parent);
+ }
+ return node;
+ }
+
+ function check_wall(first, second) {
+ var first_root, second_root;
+ first_root = get_root(first);
+ second_root = get_root(second);
+ if (first_root === second_root) {
+ return true;
+ }
+ first_root.parent = second_root;
+ return false;
+ }
+
+ Maze = function (width, height) {
+ var sets = [];
+ var order = [];
+ for (var i = 0; i < width * height; i += 1) {
+ sets.push({});
+ order.push(i * 2);
+ order.push(i * 2 + 1);
+ }
+ order.shuffle();
+
+ order.forEach(function (i) {
+ var first, second;
+ if (i >= sets.length) {
+ // Right Wall
+ first = sets[i - sets.length];
+ if (i % width === width - 1) {
+ first.right = true;
+ // Skip right wall for last column of cells
+ return;
+ }
+ second = sets[i - sets.length + 1];
+ first.right = check_wall(first, second);
+ } else {
+ // Bottom Wall
+ first = sets[i];
+ if (Math.floor(i / width) === height - 1) {
+ first.bottom = true;
+ // Skip bottom wall for last row of cells
+ return;
+ }
+ second = sets[i + width];
+ first.bottom = check_wall(first, second);
+ }
+ });
+
+ this.width = width;
+ this.height = height;
+ this.sets = sets;
+ }
+
+ Maze.prototype.display = function () {
+ var x, y, map, item, grid, line;
+ grid = [];
+ map = "██";
+ line = [1];
+ for (x = 0; x < this.width; x += 1) {
+ map += "████";
+ line.push(1);
+ line.push(1);
+ }
+ grid.push(line);
+ for (y = 0; y < this.height; y += 1) {
+ map += "\n██";
+ line = [1];
+ for (x = 0; x < this.width; x += 1) {
+ item = this.sets[y * this.width + x];
+ map += item.right ? " ██" : " ";
+ line.push(0);
+ line.push(item.right ? 1 : 0);
+ }
+ grid.push(line);
+ map += "\n██";
+ line = [1];
+ for (x = 0; x < this.width; x += 1) {
+ item = this.sets[y * this.width + x];
+ var itemr = this.sets[y * this.width + x + 1];
+ var itemb = this.sets[y * this.width + x + this.width];
+ map += item.bottom ? "██" : " ";
+ line.push(item.bottom ? 1 : 0);
+ map += (item.right || item.bottom || itemr.bottom || itemb.right) ? "██" : " ";
+ line.push((item.right || item.bottom || itemr.bottom || itemb.right) ? 1 : 0);
+ }
+ grid.push(line);
+ }
+ map += "\n";
+ // sys.p(grid);
+ return map;
+ };
+
+}());
+if (__filename === process.ARGV[1]) {
+ var sys = require('sys');
+ var width, height;
+ // Check that there are two extra arguments and that they are both non-zero numbers
+ if (process.ARGV.length == 4 && (width = parseInt(process.ARGV[2])) && (height = parseInt(process.ARGV[3]))) {
+ sys.print((new Maze(width, height)).display());
+ } else {
+ sys.puts("USAGE:\n\t" + process.ARGV[1] + " width height")
+ }
+}
+
+
+// Sample output for a 5x5 maze
+// ██████████████████████
+// ██ ██ ██
+// ██ ██████ ██ ██████
+// ██ ██ ██
+// ██ ██████ ██ ██████
+// ██ ██ ██ ██
+// ██ ██████████████ ██
+// ██ ██ ██
+// ██ ██████ ██████ ██
+// ██ ██ ██ ██
+// ██████████████████████
+
37 smtp.js
@@ -0,0 +1,37 @@
+function wordwrap(str) {
+ var m = 80;
+ var b = "\r\n";
+ var c = false;
+ var i, j, l, s, r;
+ str += '';
+ if (m < 1) {
+ return str;
+ }
+ for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
+ for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
+ j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
+ }
+ }
+ return r.join("\n");
+}
+
+
+exports.sendmail = function (from, to, subject, body) {
+ var connection = process.tcp.createConnection(25);
+ connection.addListener("connect", function (socket) {
+ connection.send("helo localhost\r\n");
+ connection.send("mail from: " + from + "\r\n");
+ connection.send("rcpt to: " + to + "\r\n");
+ connection.send("data\r\n");
+ connection.send("To: " + to + "\r\n");
+ connection.send("Subject: " + subject + "\r\n");
+ connection.send("Content-Type: text/html\r\n");
+ connection.send(wordwrap(body) + "\r\n");
+ connection.send(".\r\n");
+ connection.send("quit\r\n");
+ connection.close();
+ });
+};
+
+
+
Oops, something went wrong.

0 comments on commit 987651c

Please sign in to comment.