Skip to content

Commit

Permalink
Object.keys wrapper for browsers without it
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed May 5, 2012
1 parent 65e8ea3 commit a891f76
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
35 changes: 18 additions & 17 deletions index.js
@@ -1,12 +1,12 @@
var traverse = require('traverse');
var EventEmitter = require('events').EventEmitter;

var stream = function () {};
try {
stream = require('stream');
} catch (e) {};

var json = typeof JSON === 'object' ? JSON : require('jsonify');
var Object_keys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
};

var exports = module.exports = function (wrapper) {
var self = {};
Expand Down Expand Up @@ -132,11 +132,11 @@ var Session = exports.Session = function (id, wrapper) {
}

// copy since assignment discards the previous refs
Object.keys(self.remote).forEach(function (key) {
Object_keys(self.remote).forEach(function (key) {
delete self.remote[key];
});

Object.keys(methods).forEach(function (key) {
Object_keys(methods).forEach(function (key) {
self.remote[key] = methods[key];
});

Expand Down Expand Up @@ -196,7 +196,7 @@ var Scrubber = exports.Scrubber = function (store) {
// return a callback of its own.
self.unscrub = function (msg, f) {
var args = msg.arguments || [];
Object.keys(msg.callbacks || {}).forEach(function (strId) {
Object_keys(msg.callbacks || {}).forEach(function (strId) {
var id = parseInt(strId,10);
var path = msg.callbacks[id];
args = setAt(args, path, f(id));
Expand Down Expand Up @@ -316,22 +316,23 @@ var parseArgs = exports.parseArgs = function (argv) {
params.block = arg;
}
else if (typeof arg === 'object') {
if (arg.__proto__ === Object.prototype) {
if (arg && typeof arg.listen === 'function') {
// servers can .listen()
params.server = arg;
}
else if (arg && typeof arg.write === 'function') {
// streams can .write()
params.stream = arg;
}
else {
// merge vanilla objects into params
Object.keys(arg).forEach(function (key) {
Object_keys(arg).forEach(function (key) {
params[key] = key === 'port'
? parseInt(arg[key], 10)
: arg[key]
;
});
}
else if (stream.Stream && arg instanceof stream.Stream) {
params.stream = arg;
}
else {
// and non-Stream, non-vanilla objects are probably servers
params.server = arg;
}
}
else if (typeof arg === 'undefined') {
// ignore
Expand Down
8 changes: 4 additions & 4 deletions test/args.js
@@ -1,6 +1,6 @@
var test;
try { test = require('tap').test }
catch (e) { test = require('testling') }
var test = typeof window === 'undefined'
? require('tap').test : require('testling')
;

var protocol = require('../');

Expand All @@ -26,7 +26,7 @@ test('args', function (t) {
);

t.deepEqual(
protocol.parseArgs(argv('meow.cats.com', { port : '1234', })),
protocol.parseArgs(argv('meow.cats.com', { port : '1234' })),
{ host : 'meow.cats.com', port : 1234 }
);

Expand Down
9 changes: 6 additions & 3 deletions testling/test.sh
@@ -1,7 +1,9 @@
#!/bin/bash

echo -n "username: "
read user
if test -z "$user"; then
echo -n "username: "
read user
fi

browsers=""
if test -n "$2"; then
Expand All @@ -10,7 +12,8 @@ fi

function testFile {
tar -cf- index.js "$1" node_modules/traverse/index.js \
| curl -u "$user" -sSNT- "http://testling.com/?$browsers&main=$1"
| curl -u "$user" -sSNT- \
"http://testling.com/?$browsers&main=$1"
}

if test -f "$1"; then
Expand Down

0 comments on commit a891f76

Please sign in to comment.