From b43960338afc58ce8f3a0de97fcb67265429834e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 24 Oct 2011 12:33:54 -0400 Subject: [PATCH] Added object_merge function --- src/{ => core}/core.js | 0 src/module_vars.js | 2 - src/{ => modules}/DOM.js | 0 src/{ => modules}/QS.js | 0 src/{ => modules}/ajax.js | 0 src/{ => modules}/event.js | 0 src/{ => modules}/store.js | 0 src/modules/util.js | 200 +++++++++++++++++++++++++++++++++++++ src/util.js | 97 ------------------ 9 files changed, 200 insertions(+), 99 deletions(-) rename src/{ => core}/core.js (100%) delete mode 100644 src/module_vars.js rename src/{ => modules}/DOM.js (100%) rename src/{ => modules}/QS.js (100%) rename src/{ => modules}/ajax.js (100%) rename src/{ => modules}/event.js (100%) rename src/{ => modules}/store.js (100%) create mode 100644 src/modules/util.js delete mode 100644 src/util.js diff --git a/src/core.js b/src/core/core.js similarity index 100% rename from src/core.js rename to src/core/core.js diff --git a/src/module_vars.js b/src/module_vars.js deleted file mode 100644 index 3d359bf..0000000 --- a/src/module_vars.js +++ /dev/null @@ -1,2 +0,0 @@ - //Fix $_ is not defined errors - var $_ = $_ || window.$_; \ No newline at end of file diff --git a/src/DOM.js b/src/modules/DOM.js similarity index 100% rename from src/DOM.js rename to src/modules/DOM.js diff --git a/src/QS.js b/src/modules/QS.js similarity index 100% rename from src/QS.js rename to src/modules/QS.js diff --git a/src/ajax.js b/src/modules/ajax.js similarity index 100% rename from src/ajax.js rename to src/modules/ajax.js diff --git a/src/event.js b/src/modules/event.js similarity index 100% rename from src/event.js rename to src/modules/event.js diff --git a/src/store.js b/src/modules/store.js similarity index 100% rename from src/store.js rename to src/modules/store.js diff --git a/src/modules/util.js b/src/modules/util.js new file mode 100644 index 0000000..5defef9 --- /dev/null +++ b/src/modules/util.js @@ -0,0 +1,200 @@ +/** + * Util Object + * + * Various object and string manipulation functions + */ +(function(){ + + "use strict"; + + var u = { + reverse_key_sort: function(o) + { + //Define some variables + var keys = [], + num_keys = 0, + new_o = {}, + i; + + //Extract the keys + keys = this.object_keys(o); + + //Sort the keys + keys.sort(function (b, a) { + var aFloat = parseFloat(a), + bFloat = parseFloat(b), + aNumeric = aFloat + '' === a, + bNumeric = bFloat + '' === b; + + if (aNumeric && bNumeric) + { + return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0; + } + else if (aNumeric && !bNumeric) + { + return 1; + } + else if (!aNumeric && bNumeric) + { + return -1; + } + + return a > b ? 1 : a < b ? -1 : 0; + }); + + //cache object/array size + num_keys = keys.length; + + //Recreate the object/array + for(i=0; i < num_keys; i++) + { + new_o[keys[i]] = o[keys[i]]; + } + + return new_o; + }, + object_keys: function(o) + { + var keys = [], + k; + + for(k in o) + { + if(o.hasOwnProperty(k)) + { + keys.push(k); + } + } + + return keys; + }, + object_values: function(o) + { + var vals = [], + prop; + + for(prop in o) + { + vals.push(o[prop]); + } + + return vals; + }, + array_combine: function(keys, vals) + { + var new_object = {}, + num_keys, + i = 0; + + //Extract the keys or values if needed + if($_.type(keys) !== "array") + { + keys = this.object_values(keys); + } + if($_.type(vals) !== "array") + { + vals = this.object_values(vals); + } + + //cache the number of keys + num_keys = keys.length; + + if(num_keys !== vals.length) + { + console.log("Object combine requires two arrays of the same size"); + return false; + } + + //Create and return the new object + for(i = 0; i < num_keys; i++) + { + new_object[keys[i]] = vals[i]; + } + + return new_object; + }, + object_merge: function() + { + var args = Array.prototype.slice.call(arguments), + arg_len = args.length, + new_obj = {}, + arg, + iarg_len = 0, + i, + j, + x, + is_array = true; + + //Check for an array in the arguments + for(i=0; i < arg_len; i++) + { + if($_.type(args[i]) !== "array") + { + is_array = false; + break; + } + } + + //If all the arguments are javascript arrays + if(is_array) + { + new_obj = []; + //Let javascript do all the work! + for(i=0; i< arg_len; i++) + { + new_obj = new_obj.contact(args[i]); + } + + //Return early + return new_obj; + } + + //No, there's at least one object + for(i=0, x=0; i < arg_len; i++) + { + arg = args[i]; + + // If the argument is an array, add the array items as + // numeric object properties + if ($_.type(arg) == "array") + { + for (j=0, iarg_len= arg.length; j < iarg_len; j++) + { + new_obj[x++] = arg[j]; + } + } + else + { + for (j in arg) + { + if(arg.hasOwnProperty(j)) + { + // If the key is numeric, add the property with + // a numeric key + if(parseInt(j, 10) + '' === j) + { + new_obj[x++] = arg[j]; + } + else + { + new_obj[j] = arg[j]; + } + } + } + } + } + + return new_obj; + }, + str_trans: function(string, from, to) + { + + }, + str_replace: function(from, to, string) + { + } + }; + + //Add it to the $_ object + $_.ext('util', u); +}()); \ No newline at end of file diff --git a/src/util.js b/src/util.js deleted file mode 100644 index 4c78e0e..0000000 --- a/src/util.js +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Util Object - * - * Various object and string manipulation functions - */ -(function(){ - - var u = { - object_keys: function(o) - { - var keys = [], - k; - - for(k in o) - { - if(o.hasOwnProperty(k)) - { - keys.push(k); - } - } - - return keys; - }, - object_values: function(o) - { - var vals = [], - prop; - - for(prop in o) - { - vals.push(o[prop]); - } - - return vals; - }, - object_merge: function() - { - - }, - reverse_key_sort: function(o) - { - //Define some variables - var keys = [], - num_keys = 0, - new_o = {}, - i, - k, - x; - - //Extract the keys - keys = this.object_keys(o); - - //Sort the keys - keys.sort(function (b, a) { - - var aFloat = parseFloat(a), - bFloat = parseFloat(b), - aNumeric = aFloat + '' === a, - bNumeric = bFloat + '' === b; - - if (aNumeric && bNumeric) - { - return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0; - } - else if (aNumeric && !bNumeric) - { - return 1; - } - else if (!aNumeric && bNumeric) - { - return -1; - } - - return a > b ? 1 : a < b ? -1 : 0; - }); - - //cache object/array size - num_keys = keys.length; - - //Recreate the object/array - for(i=0; i < num_keys; i++) - { - k = keys[i]; - new_o[k] = o[k]; - } - - return new_o; - }, - str_trans: function(string, from, to) - { - - } - }; - - //Add it to the $_ object - $_.ext('util', u); -}()); \ No newline at end of file