Skip to content
Browse files

Object.keys wrapper for browsers without it

  • Loading branch information...
1 parent 65e8ea3 commit a891f76822a38b2fd87387e6eedb2582fe87a1d8 @substack committed May 5, 2012
Showing with 28 additions and 24 deletions.
  1. +18 −17 index.js
  2. +4 −4 test/args.js
  3. +6 −3 testling/test.sh
View
35 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 = {};
@@ -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];
});
@@ -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));
@@ -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
View
8 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('../');
@@ -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 }
);
View
9 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
@@ -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

0 comments on commit a891f76

Please sign in to comment.
Something went wrong with that request. Please try again.