Skip to content

Commit

Permalink
Cleanup and document proto.
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Apr 6, 2010
1 parent 57d0c66 commit 2e819ff
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 50 deletions.
51 changes: 51 additions & 0 deletions README.markdown
@@ -0,0 +1,51 @@
# Proto

This simple js library adds three functions to Object.prototype and one to Function.prototype that are commonly used. Since this only modifies global objects and doesn't export any structures, you don't need the return value when calling `require('proto')`.

## Object.prototype

All functions added to `Object.prototype` are usable from any object in JavaScript.

### Object.prototype.forEach

This is the most useful of the additions. It allows you to forEach over an `Object` instance's local properties and values just like you can already do with `Array` instances.

require('proto');
{name: "Tim", age: 28}.forEach(function (value, key) {
sys.puts(key + " = " + JSON.stringify(value));
});

### Object.prototype.map

This works like forEach, except returns an `Array` instance with the returned values of the function calls.

require('proto');
var pairs = {name: "Tim", age: 28}.map(function (value, key) {
return key + " = " + value;
});
// pairs is ["name = Tim", "age = 28"]

### Object.prototype.mixin

Does a shallow copy from another object into this one.

require('proto');
GLOBAL.mixin(require('sys'));
puts("Hello World");

## Function.prototype

Functions added to `Function.prototype` are available to any JavaScript function.

### Function.prototype.curry

Partially applies a function and returns a new function that accepts the remaining arguments.

require('proto');
var fs = require('fs');
var sys = require('sys');
// Create a curried version of the readFile function
var loader = fs.readFile.curry("myfile.txt");
// Then finish the application of the function
loader(sys.debug);

72 changes: 22 additions & 50 deletions lib/proto.js
@@ -1,3 +1,25 @@
/*
Copyright (c) 2010 Tim Caswell <tim@creationix.com>
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.
*/

var proto = Object.prototype; var proto = Object.prototype;
var func_proto = Function.prototype; var func_proto = Function.prototype;


Expand Down Expand Up @@ -54,53 +76,3 @@ if (typeof func_proto.curry !== 'function') {
}}); }});
} }


if (typeof func_proto.single !== 'function') {
Object.defineProperty(func_proto, "single", {value: function () {
var fn = this;
var first = arr_proto.slice.call(arguments);
return function () {
return fn.apply(this, first.concat([this]));
};
}});
}

// Inspired by http://github.com/willconant/flow-js
Function.step = function step() {
var steps = Array.prototype.slice.call(arguments),
counter, results;
function next() {
if (steps.length <= 0) { return; }
var fn = steps.shift();
counter = 0;
results = [];
fn.apply(next, arguments);
}
next.parallel = function () {
var i = counter;
counter++;
return function () {
counter--;
results[i] = arguments;
if (counter <= 0) {
next(results);
}
};
};
next([]);
};

// var fs = require('fs');
// var sys = require('sys');
//
// Function.step(
// fs.readdir.single(__dirname),
//
// function (err, paths) {
// var parallel = this.parallel;
// paths.forEach(function (path) {
// fs.readFile(path, parallel());
// });
// },
// sys.p
// );
//

0 comments on commit 2e819ff

Please sign in to comment.