diff --git a/README.md b/README.md index 0a31585..1bae268 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,14 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera }): * ext: For extending the library, adds this.el to the object or function supplied - Use: $_.ext("name", functionOrObject) + Use: $_.ext("name", functionOrObject); Example: $_.ext("zip", function(){ //function }); Adds 'zip' function to $_. +* type: For getting the type of a variable + Use: $_.type(var); + ### Ajax: simple, jQuery-like ajax functions ### diff --git a/combine.php b/combine.php index 2edeef8..90b80ea 100755 --- a/combine.php +++ b/combine.php @@ -77,5 +77,16 @@ $new_file .= "\n}());"; //Output the full file file_put_contents("kis-custom.js", $new_file); +//Get a much-minified version from Google's closure compiler +$ch = curl_init('http://closure-compiler.appspot.com/compile'); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); +curl_setopt($ch, CURLOPT_POST, 1); +curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($new_file)); +$output = curl_exec($ch); +curl_close($ch); + +file_put_contents("kis-min.js", $output); + + //Display the output on-screen too echo '
'.htmlspecialchars($new_file).'
'; \ No newline at end of file diff --git a/kis-all.js b/kis-all.js index bd25080..eb19921 100644 --- a/kis-all.js +++ b/kis-all.js @@ -52,7 +52,7 @@ $_ = function(s) { //Have documentElement be default selector, just in case - if(typeof s == "undefined") + if(typeof s === "undefined") { sel = (typeof $_.el !== "undefined") ? $_.el @@ -60,7 +60,7 @@ } else { - sel = $(s); + sel = (typeof s !== "object") ? $(s) : s; } // Make a copy before adding properties @@ -115,8 +115,8 @@ //Function to add to $_ object, and get sel $_.ext = function(name, obj) { - $_[name] = obj; obj.el = sel; + $_[name] = obj; }; //Selector iteration @@ -143,6 +143,18 @@ callback(sel); } }); + + //Type retriever + $_.type = function(obj) + { + if((function() {return obj && (obj !== this)}).call(obj)) + { + //fallback on 'typeof' for truthy primitive values + return (typeof obj).toLowerCase(); + } + + return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); + } //Set global variables $_ = window.$_ = window.$_ || $_; @@ -341,10 +353,9 @@ * */ (function (){ - var d, tag_reg, id_reg, class_reg; + var d, tag_reg, class_reg; tag_reg = /^([\w\-]+)$/; - id_reg = /#([\w\-]+$)/; class_reg = /\.([\w\-]+)$/; @@ -436,7 +447,7 @@ outerHeight: "offsetHeight", outerWidth: "offsetWidth", top: "posTop" - } + }; //If you don't define a value, try returning the existing value @@ -472,18 +483,13 @@ var i, len = curr_sel.length, matches = []; - - if(typeof filter !== "string") - { - return filter; - } //Filter by tag if(filter.match(tag_reg)) { for(i=0;i1&&typeof b==="undefined")console.log(a),console.log("Must be a singular element");else if(a.length>1&&typeof b!== +"undefined")e.each(function(a){return c(a,d,b)});else return c(a,d,b)},text:function(d){var b,a,c;c=this.el;a=typeof c.innerText!=="undefined"?"innerText":typeof c.textContent!=="undefined"?"textContent":"innerHTML";b=c[a];return typeof d!=="undefined"?c[a]=d:b},css:function(d,b){if(typeof b==="undefined")return f(this.el,d);e.each(function(a){f(a,d,b)})},children:function(d){if(typeof d==="undefined")return e(this.el.children);var b=typeof this.el.children!=="undefined"?this.el.children:this.el; +if(e.type(d)!=="string")return e(d);else if(d.match(/#([\w\-]+$)/))return e(e.$(d));else{var c,f=b.length,g=[];if(d.match(a))for(c=0;c1?c[1]:"";else if(c===!1||c===void 0)c=window.location.search.substring(1);else return!1;g=c.split("&");f=g.length;for(c=0;c + + + + + Kis-js test app + + +

Kis-js Test Suite

+

+
+

+
    +
    + test markup, will be hidden + +
    + + + + + + \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index fcc00ea..4ec0668 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,5 +1,5 @@ (function(){ - "use strict"; + //"use strict"; //Selector test function function $(a) @@ -22,6 +22,16 @@ strictEqual(typeof $_(), "object"); }); + test("Type Checking", function(){ + equal($_.type(5), "number", "Number type"); + equal($_.type("abc"), "string", "String type"); + equal($_.type({}), "object", "Object type"); + equal($_.type([0,1,2]), "array", "Array type"); + equal($_.type(/x/), "regexp", "Regex type"); + equal($_.type(function(){}), "function", "Function type"); + equal($_.type(true), "boolean", "Boolean type"); + }); + test("Unique Selectors", function(){ var x = $_("ol"); var y = $_("aside"); @@ -116,7 +126,7 @@ test("Show/Hide", function(){ expect(3); - var $test = $_("#classChild .child"); + var $test = $_("#classChild .nephew"); var ele = $test.el; $test.dom.hide(); @@ -132,14 +142,15 @@ }); test("Text", function(){ - expect(2); + expect(3); var $test = $_("article#r14"); var ele = $test.el; var text = (typeof ele.innerText !== "undefined") ? ele.innerText : ele.textContent; equal($test.el, $("article#r14"), "Selector property is correct"); - equal($test.dom.text(), text, "Getting test"); + equal($test.dom.text(), text, "Getting text"); + equal($test.dom.text(""), "", "Setting text"); }); test("Attr", function(){ @@ -168,10 +179,13 @@ test("Children", function(){ var $test = $_("section"); - var ele = $test.el; + var ele = $("section"); + var ele2 = $_("section aside").el; - equal($test.dom.children().el, ele.children, "Returns children without parameters"); - equal($test.dom.children('#r14').el, document.getElementById('r14'), "Finds id"); + equal($_("section").dom.children().el, ele.children, "Returns children without parameters"); + equal($_("section").dom.children('#r14').el, document.getElementById('r14'), "Finds id"); + equal($_("section").dom.children("aside").el, ele2, "Finds children by tag name"); + equal($_("section aside").dom.children(".child").el, $_("#classChild .child").el, "Finds children by class"); }); }()); \ No newline at end of file