From d1f1af40ae4ad5763c26660f17abdeda8e68e82a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 31 Oct 2011 11:56:59 -0400 Subject: [PATCH] Updated selector function to add context, privitized reverse_key_sort function in util module --- .gitignore | 1 + README.md | 2 +- kis-all.js | 120 ++++++++++++++++++++---------------------- kis-min.js | 44 ++++++++-------- src/core/core.js | 19 ++++--- src/modules/util.js | 101 +++++++++++++++++------------------ tests/qunit/qunit.css | 2 +- tests/qunit/qunit.js | 4 +- tests/tests.js | 6 +-- 9 files changed, 145 insertions(+), 154 deletions(-) diff --git a/.gitignore b/.gitignore index e1e512b..e5065e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ kis-custom.js +kis-custom-min.js combine-build.php test.html \ No newline at end of file diff --git a/README.md b/README.md index 3f4ad63..1f46442 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ functions: ### Util: Array and string manipulation functions ### -** functions: ** +**functions:** * object_keys: Gets the name of the properties of an object diff --git a/kis-all.js b/kis-all.js index 7580e77..de3d90d 100644 --- a/kis-all.js +++ b/kis-all.js @@ -21,23 +21,25 @@ * * Simple DOM selector function */ - $ = function (a) + $ = function (a, context) { - var x; + var x, c; + if (typeof a !== "string" || typeof a === "undefined"){ return a;} + //Check for a context of a specific element, otherwise, just run on the document + c = (typeof context === 'object' && context.nodeType === 1) + ? context + : document; + //Pick the quickest method for each kind of selector if (a.match(/^#([\w\-]+$)/)) { return document.getElementById(a.split('#')[1]); } - else if (a.match(/^([\w\-]+)$/)) - { - x = document.getElementsByTagName(a); - } else { - x = document.querySelectorAll(a); + x = c.querySelectorAll(a); } //Return the single object if applicable @@ -54,6 +56,7 @@ //Have documentElement be default selector, just in case if(typeof s === "undefined") { + //Defines a "global" selector for that instance sel = (typeof $_.el !== "undefined") ? $_.el : document.documentElement; @@ -157,7 +160,7 @@ } return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); - } + }; //Set global variables $_ = window.$_ = window.$_ || $_; @@ -799,52 +802,53 @@ if (typeof document !== "undefined" && !("classList" in document.createElement(" "use strict"; - var u = { - reverse_key_sort: function(o) - { - //Define some variables - var keys = [], - num_keys = 0, - new_o = {}, - i; + var + reverse_key_sort = function(o) + { + //Define some variables + var keys = [], + num_keys = 0, + new_o = {}, + i; + + //Extract the keys + keys = u.object_keys(o); - //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++) + //Sort the keys + keys.sort(function (b, a) { + var aFloat = parseFloat(a), + bFloat = parseFloat(b), + aNumeric = aFloat + '' === a, + bNumeric = bFloat + '' === b; + + if (aNumeric && bNumeric) { - new_o[keys[i]] = o[keys[i]]; + return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0; + } + else if (aNumeric && !bNumeric) + { + return 1; + } + else if (!aNumeric && bNumeric) + { + return -1; } - return new_o; - }, + 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; + }, + u = { object_keys: function(o) { var keys = [], @@ -1003,7 +1007,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement(" { // Sort the keys in descending order for better // replacement functionality - from = this.reverse_key_sort(from); + from = reverse_key_sort(from); for(f in from) { @@ -1035,19 +1039,11 @@ if (typeof document !== "undefined" && !("classList" in document.createElement(" stry = str.charAt(i+1); for(j=0; j < from_len; j++) { - /*if(from_len !== to_len) + if(strx == from.charAt(j)) { - //Double check matches when the strings are different lengths - + match = true; + break; } - else - {*/ - if(strx == from.charAt(j)) - { - match = true; - break; - } - //} } } else diff --git a/kis-min.js b/kis-min.js index 8bd55c1..20882b4 100644 --- a/kis-min.js +++ b/kis-min.js @@ -1,22 +1,22 @@ -(function(){if(document.querySelectorAll){var c,d,e,b;d=function(a){if(typeof a!=="string"||typeof a==="undefined")return a;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);else a=a.match(/^([\w\-]+)$/)?document.getElementsByTagName(a):document.querySelectorAll(a);return a.length===1?a[0]:a};c=function(a){b=typeof a==="undefined"?typeof c.el!=="undefined"?c.el:document.documentElement:typeof a!=="object"?d(a):a;c.prototype.el=b;var a=e(c),f;for(f in a)if(typeof a[f]==="object")a[f].el= -b;a.el=b;return a};e=function(a){var b;if(typeof a!=="undefined"){if(typeof Object.create!=="undefined")return Object.create(a);b=typeof a;if(!(b!=="object"&&b!=="function"))return b=function(){},b.prototype=a,new b}};c.ext=function(a,f){f.el=b;c[a]=f};c.ext("each",function(a){if(typeof b.length!=="undefined"&&b!==window){var f=b.length;if(f!==0)for(var c,d=0;d1&&typeof a==="undefined")console.log(f),console.log("Must be a singular element");else if(f.length>1&&typeof a!== -"undefined")$_.each(function(f){return c(f,b,a)});else return c(f,b,a)},text:function(b){var a,f,c;c=this.el;f=typeof c.innerText!=="undefined"?"innerText":typeof c.textContent!=="undefined"?"textContent":"innerHTML";a=c[f];return typeof b!=="undefined"?c[f]=b:a},css:function(b,a){if(typeof a==="undefined")return e(this.el,b);$_.each(function(f){e(f,b,a)})}})})(); -(function(){$_.ext("store",{get:function(c){return JSON.parse(localStorage.getItem(c))},set:function(c,d){typeof d!=="string"&&(d=JSON.stringify(d));localStorage.setItem(c,d)},remove:function(c){localStorage.removeItem(c)},getAll:function(){var c,d,e;d=localStorage.length;e={};for(c=0;c1?c[1]:"";else if(c===false||c===void 0)c=window.location.search.substring(1);else return false;d=c.split("&");e=d.length;for(c=0;cd?1:ca?1:b1&&typeof b==="undefined")console.log(d),console.log("Must be a singular element");else if(d.length>1&&typeof b!== +"undefined")$_.each(function(d){return c(d,a,b)});else return c(d,a,b)},text:function(a){var b,d,f;f=this.el;d=typeof f.innerText!=="undefined"?"innerText":typeof f.textContent!=="undefined"?"textContent":"innerHTML";b=f[d];return typeof a!=="undefined"?f[d]=a:b},css:function(a,b){if(typeof b==="undefined")return g(this.el,a);$_.each(function(d){g(d,a,b)})}})})(); +(function(){$_.ext("store",{get:function(c){return JSON.parse(localStorage.getItem(c))},set:function(c,e){typeof e!=="string"&&(e=JSON.stringify(e));localStorage.setItem(c,e)},remove:function(c){localStorage.removeItem(c)},getAll:function(){var c,e,g;e=localStorage.length;g={};for(c=0;c1?c[1]:"";else if(c===false||c===void 0)c=window.location.search.substring(1);else return false;e=c.split("&");g=e.length;for(c=0;cf?1:ba?1:d 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++) + //Sort the keys + keys.sort(function (b, a) { + var aFloat = parseFloat(a), + bFloat = parseFloat(b), + aNumeric = aFloat + '' === a, + bNumeric = bFloat + '' === b; + + if (aNumeric && bNumeric) { - new_o[keys[i]] = o[keys[i]]; + return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0; + } + else if (aNumeric && !bNumeric) + { + return 1; + } + else if (!aNumeric && bNumeric) + { + return -1; } - return new_o; - }, + 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; + }, + u = { object_keys: function(o) { var keys = [], @@ -212,7 +213,7 @@ { // Sort the keys in descending order for better // replacement functionality - from = this.reverse_key_sort(from); + from = reverse_key_sort(from); for(f in from) { @@ -244,19 +245,11 @@ stry = str.charAt(i+1); for(j=0; j < from_len; j++) { - /*if(from_len !== to_len) + if(strx == from.charAt(j)) { - //Double check matches when the strings are different lengths - + match = true; + break; } - else - {*/ - if(strx == from.charAt(j)) - { - match = true; - break; - } - //} } } else diff --git a/tests/qunit/qunit.css b/tests/qunit/qunit.css index c85f36a..e114ea0 100755 --- a/tests/qunit/qunit.css +++ b/tests/qunit/qunit.css @@ -1,5 +1,5 @@ /** - * QUnit - A JavaScript Unit Testing Framework + * QUnit 1.2.0pre - A JavaScript Unit Testing Framework * * http://docs.jquery.com/QUnit * diff --git a/tests/qunit/qunit.js b/tests/qunit/qunit.js index b17f168..d630c43 100755 --- a/tests/qunit/qunit.js +++ b/tests/qunit/qunit.js @@ -6,8 +6,6 @@ * Copyright (c) 2011 John Resig, Jörn Zaefferer * Dual licensed under the MIT (MIT-LICENSE.txt) * or GPL (GPL-LICENSE.txt) licenses. - * Pulled Live from Git Fri Oct 21 15:55:01 UTC 2011 - * Last Commit: ee156923cdb01820e35e6bb579d5cf6bf55736d4 */ (function(window) { @@ -1586,4 +1584,4 @@ QUnit.diff = (function() { }; })(); -})(this); \ No newline at end of file +})(this); diff --git a/tests/tests.js b/tests/tests.js index 117b351..d462720 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,5 +1,5 @@ (function(){ - //"use strict"; + "use strict"; //Selector test function function $(a) @@ -253,7 +253,7 @@ }); - test("Reverse Key Sort", function(){ + /*test("Reverse Key Sort", function(){ expect(2); var test_o = { @@ -275,7 +275,7 @@ deepEqual($_.util.reverse_key_sort(test_o), test_sorted, "Object sort"); deepEqual($_.util.object_values($_.util.reverse_key_sort(test_array)), test_array_sorted, "Array Sort"); - }); + });*/ test("Object Merge", function(){ expect(2);