Permalink
Browse files

shims for Array.isArray, forEach, Object.keys for non-es5 browsers

  • Loading branch information...
substack committed Nov 19, 2012
1 parent 7071d4b commit d50a19cb77c1f9b9635ff8e819b8aa2cc72c5dc0
Showing with 26 additions and 11 deletions.
  1. +19 −8 lib/request.js
  2. +6 −2 lib/response.js
  3. +1 −1 package.json
View
@@ -17,16 +17,18 @@ var Request = module.exports = function (xhr, params) {
);
if (params.headers) {
- Object.keys(params.headers).forEach(function (key) {
+ var keys = objectKeys(params.headers);
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
if (!self.isSafeRequestHeader(key)) return;
var value = params.headers[key];
- if (Array.isArray(value)) {
- value.forEach(function (v) {
- xhr.setRequestHeader(key, v);
- });
+ if (isArray(value)) {
+ for (var j = 0; j < value.length; j++) {
+ xhr.setRequestHeader(key, value[j]);
+ }
}
else xhr.setRequestHeader(key, value)
- });
+ }
}
var res = new Response;
@@ -46,8 +48,7 @@ var Request = module.exports = function (xhr, params) {
Request.prototype = new Stream;
Request.prototype.setHeader = function (key, value) {
- if ((Array.isArray && Array.isArray(value))
- || value instanceof Array) {
+ if (isArray(value)) {
for (var i = 0; i < value.length; i++) {
this.xhr.setRequestHeader(key, value[i]);
}
@@ -101,3 +102,13 @@ Request.prototype.isSafeRequestHeader = function (headerName) {
if (!headerName) return false;
return (Request.unsafeHeaders.indexOf(headerName.toLowerCase()) === -1)
};
+
+var objectKeys = Object.keys || function (obj) {
+ var keys = [];
+ for (var key in obj) keys.push(key);
+ return keys;
+};
+
+var isArray = Array.isArray || function (xs) {
+ return Object.prototype.toString.call(xs) === '[object Array]';
+};
View
@@ -24,8 +24,8 @@ function parseHeaders (res) {
var key = m[1].toLowerCase(), value = m[2];
if (headers[key] !== undefined) {
- if ((Array.isArray && Array.isArray(headers[key]))
- || headers[key] instanceof Array) {
+
+ if (isArray(headers[key])) {
headers[key].push(value);
}
else {
@@ -113,3 +113,7 @@ Response.prototype._emitData = function (res) {
this.offset = respBody.length;
}
};
+
+var isArray = Array.isArray || function (xs) {
+ return Object.prototype.toString.call(xs) === '[object Array]';
+};
View
@@ -1,6 +1,6 @@
{
"name" : "http-browserify",
- "version" : "0.1.4",
+ "version" : "0.1.5",
"description" : "http module compatability for browserify",
"main" : "index.js",
"browserify" : "index.js",

0 comments on commit d50a19c

Please sign in to comment.