Permalink
Browse files

shims for browser compat

  • Loading branch information...
1 parent 1e458cf commit 4f781dbb616b41c9d2db9d70d2f97afd053b822c @juliangruber juliangruber committed Apr 21, 2013
Showing with 175 additions and 64 deletions.
  1. +75 −27 index.js
  2. +100 −37 test/browser/qs.js
View
102 index.js
@@ -6,6 +6,56 @@
var toString = Object.prototype.toString;
/**
+ * Array#indexOf shim.
+ */
+
+function indexOf(arr, el) {
+ if (arr.indexOf) return arr.indexOf(el);
+ for (var i = 0; i < arr.length; i++) {
+ if (arr[i] === el) return i;
+ }
+ return -1;
+}
+
+/**
+ * Array.isArray shim.
+ */
+
+var isArray = Array.isArray || function (arr) {
+ return toString.call(arr) == '[object Array]';
+}
+
+/**
+ * Object.keys shim.
+ */
+
+var objectKeys = Object.keys || function (obj) {
+ var ret = [];
+ for (var key in obj) ret.push(key);
+ return ret;
+}
+
+/**
+ * Array#forEach shim.
+ */
+
+var forEach = function (arr, fn) {
+ if (arr.forEach) return arr.forEach(fn);
+ for (var i = 0; i < arr.length; i++) fn(arr[i]);
+}
+
+/**
+ * Array#reduce shim.
+ */
+
+var reduce = function (arr, fn, initial) {
+ if (arr.reduce) return arr.reduce(fn, initial);
+ var res = initial;
+ for (var i = 0; i < arr.length; i++) res = fn(res, arr[i]);
+ return res;
+}
+
+/**
* Cache non-integer test regexp.
*/
@@ -23,7 +73,7 @@ function parse(parts, parent, key, val) {
var part = parts.shift();
// end
if (!part) {
- if (Array.isArray(parent[key])) {
+ if (isArray(parent[key])) {
parent[key].push(val);
} else if ('object' == typeof parent[key]) {
parent[key] = val;
@@ -36,21 +86,21 @@ function parse(parts, parent, key, val) {
} else {
var obj = parent[key] = parent[key] || [];
if (']' == part) {
- if (Array.isArray(obj)) {
+ if (isArray(obj)) {
if ('' != val) obj.push(val);
} else if ('object' == typeof obj) {
- obj[Object.keys(obj).length] = val;
+ obj[objectKeys(obj).length] = val;
} else {
obj = parent[key] = [parent[key], val];
}
// prop
- } else if (~part.indexOf(']')) {
+ } else if (~indexOf(part, ']')) {
part = part.substr(0, part.length - 1);
- if (!isint.test(part) && Array.isArray(obj)) obj = promote(parent, key);
+ if (!isint.test(part) && isArray(obj)) obj = promote(parent, key);
parse(parts, obj, part, val);
// key
} else {
- if (!isint.test(part) && Array.isArray(obj)) obj = promote(parent, key);
+ if (!isint.test(part) && isArray(obj)) obj = promote(parent, key);
parse(parts, obj, part, val);
}
}
@@ -61,14 +111,14 @@ function parse(parts, parent, key, val) {
*/
function merge(parent, key, val){
- if (~key.indexOf(']')) {
+ if (~indexOf(key, ']')) {
var parts = key.split('[')
, len = parts.length
, last = len - 1;
parse(parts, parent, 'base', val);
// optimize
} else {
- if (!isint.test(key) && Array.isArray(parent.base)) {
+ if (!isint.test(key) && isArray(parent.base)) {
var t = {};
for (var k in parent.base) t[k] = parent.base[k];
parent.base = t;
@@ -85,7 +135,7 @@ function merge(parent, key, val){
function parseObject(obj){
var ret = { base: {} };
- Object.keys(obj).forEach(function(name){
+ forEach(objectKeys(obj), function(name){
merge(ret, name, obj[name]);
});
return ret.base;
@@ -96,21 +146,19 @@ function parseObject(obj){
*/
function parseString(str){
- return String(str)
- .split('&')
- .reduce(function(ret, pair){
- var eql = pair.indexOf('=')
- , brace = lastBraceInKey(pair)
- , key = pair.substr(0, brace || eql)
- , val = pair.substr(brace || eql, pair.length)
- , val = val.substr(val.indexOf('=') + 1, val.length);
-
- // ?foo
- if ('' == key) key = pair, val = '';
- if ('' == key) return ret;
-
- return merge(ret, decode(key), decode(val));
- }, { base: {} }).base;
+ return reduce(String(str).split('&'), function(ret, pair){
+ var eql = indexOf(pair, '=')
+ , brace = lastBraceInKey(pair)
+ , key = pair.substr(0, brace || eql)
+ , val = pair.substr(brace || eql, pair.length)
+ , val = val.substr(indexOf(val, '=') + 1, val.length);
+
+ // ?foo
+ if ('' == key) key = pair, val = '';
+ if ('' == key) return ret;
+
+ return merge(ret, decode(key), decode(val));
+ }, { base: {} }).base;
}
/**
@@ -137,7 +185,7 @@ exports.parse = function(str){
*/
var stringify = exports.stringify = function(obj, prefix) {
- if (Array.isArray(obj)) {
+ if (isArray(obj)) {
return stringifyArray(obj, prefix);
} else if ('[object Object]' == toString.call(obj)) {
return stringifyObject(obj, prefix);
@@ -191,7 +239,7 @@ function stringifyArray(arr, prefix) {
function stringifyObject(obj, prefix) {
var ret = []
- , keys = Object.keys(obj)
+ , keys = objectKeys(obj)
, key;
for (var i = 0, len = keys.length; i < len; ++i) {
@@ -223,7 +271,7 @@ function set(obj, key, val) {
var v = obj[key];
if (undefined === v) {
obj[key] = val;
- } else if (Array.isArray(v)) {
+ } else if (isArray(v)) {
v.push(val);
} else {
obj[key] = [v, val];
Oops, something went wrong.

0 comments on commit 4f781db

Please sign in to comment.