diff --git a/composable.js b/composable.js index 95408fc..c99a205 100644 --- a/composable.js +++ b/composable.js @@ -81,6 +81,17 @@ document: function () { return root.document; }, + location: function () { + return root.location; + }, + + // Window location transformations + href: function () { + return root.location.href; + }, + pathname: function () { + return root.location.pathname; + }, // DOM node transformations querySelectorAll: function (selector) { @@ -96,6 +107,9 @@ innerHTML: function (node) { return node ? node.innerHTML : null; }, + outerHTML: function (node) { + return node ? node.outerHTML : null; + }, innerText: function (node) { return node ? node.innerText || node.textContent : null; }, @@ -126,6 +140,12 @@ toFloat: function (item) { return isString(item) || isNumber(item) ? parseFloat(item) : null; }, + toFixed: function (item) { + var float = isString(item) ? parseFloat(item) : item; + return function (precision) { + return isNumber(float) ? float.toFixed(precision) : null; + }; + }, round: function (item) { return isNumber(item) ? Math.round(item) : null; }, diff --git a/composable.min.js b/composable.min.js index dc85845..c63f454 100644 --- a/composable.min.js +++ b/composable.min.js @@ -1,2 +1,2 @@ -(function(){var n=typeof self==="object"&&self.self===self&&self;var r=function(n){return typeof n==="string"},t=function(n){return typeof n==="number"},e=function(n){return typeof n==="function"},i=function(n){return Array.isArray?Array.isArray(n):Object.prototype.toString.call(n)==="[object Array]"},o=function(n){return Object.prototype.toString.call(n)==="[object NodeList]"},u=function(n){return/^\/(?:\S|\s)*\/[gimy]{0,4}$/.test(n)},a=function(n){var r=n.split("/");var t=r[r.length-1];var e=n.slice(1,(t.length+1)*-1);return new RegExp(e,t)},f=function(n,r){var t,e={};for(t in n){if(n.hasOwnProperty(t)){e[t]=r(t,n[t])}}return e},l=function(n,r){var t,e={};for(t in n){if(n.hasOwnProperty(t)&&r(t,n[t])){e[t]=n[t]}}return e},c=function(n){return function(r){var t=this;if(!t.hasOwnProperty("cache")){t.cache={}}if(!t.cache.hasOwnProperty(n)){t.cache[n]={}}return function(e){if(!e){return null}if(!t.cache[n].hasOwnProperty(e)){t.cache[n][e]={}}if(!t.cache[n][e].hasOwnProperty(r)){t.cache[n][e][r]=e[n](r)}return t.cache[n][e][r]}}};var s={window:function(){return n},document:function(){return n.document},querySelectorAll:function(n){return function(r){return r?r.querySelectorAll(n):null}},querySelector:function(n){return function(r){return r?r.querySelector(n):null}},innerHTML:function(n){return n?n.innerHTML:null},innerText:function(n){return n?n.innerText||n.textContent:null},rootInnerText:function(n){if(!n)return null;var r=[];for(var t=0;tn?r[n]:null}},slice:function(n,t){var e=[parseInt(n,10)];if(typeof t!=="undefined"){e.push(parseInt(t,10))}return function(n){var t;if(r(n)){t=n?String.prototype.slice.apply(n,e):null}else if(i(n)||o(n)){t=n?Array.prototype.slice.apply(n,e):null}return t}},getProperty:function(n){return function(r){return r?r[n]:null}},getProperties:function(n){return function(r){if(!r){return null}var t=n.split("."),e;while(t.length){e=t.shift();r=r?r[e]:null}return r}}};var p={map:function(n){return function(r){return i(r)||o(r)?Array.prototype.map.call(r,n):null}}};var h=function(n){if(!(this instanceof h)){return new h(n)}return this.extract(n)};h.__objectMerge=function(){var n={};for(var r=0;rn?r[n]:null}},slice:function(n,t){var e=[parseInt(n,10)];if(typeof t!=="undefined"){e.push(parseInt(t,10))}return function(n){var t;if(r(n)){t=n?String.prototype.slice.apply(n,e):null}else if(o(n)||i(n)){t=n?Array.prototype.slice.apply(n,e):null}return t}},getProperty:function(n){return function(r){return r?r[n]:null}},getProperties:function(n){return function(r){if(!r){return null}var t=n.split("."),e;while(t.length){e=t.shift();r=r?r[e]:null}return r}}};var p={map:function(n){return function(r){return o(r)||i(r)?Array.prototype.map.call(r,n):null}}};var h=function(n){if(!(this instanceof h)){return new h(n)}return this.extract(n)};h.__objectMerge=function(){var n={};for(var r=0;rcomposable.js
        window: function () {
             return root;
         },
-        document: function () {
+        document: function () {
             return root.document;
+        },
+        location: function () {
+          return root.location;
         },
@@ -185,6 +188,26 @@

composable.js

+

Window location transformations

+ + + +
        href: function () {
+          return root.location.href;
+        },
+        pathname: function () {
+          return root.location.pathname;
+        },
+ + + + +
  • +
    + +
    + +

    DOM node transformations

    @@ -194,18 +217,21 @@

    composable.js

    return node ? node.querySelectorAll(selector) : null; }; }, - querySelector: function (selector) { + querySelector: function (selector) { return function (node) { return node ? node.querySelector(selector) : null; }; }, - innerHTML: function (node) { + innerHTML: function (node) { return node ? node.innerHTML : null; }, - innerText: function (node) { + outerHTML: function (node) { + return node ? node.outerHTML : null; + }, + innerText: function (node) { return node ? node.innerText || node.textContent : null; }, - rootInnerText: function (node) { + rootInnerText: function (node) { if (!node) return null; var textNodes = []; @@ -216,10 +242,10 @@

    composable.js

    } return Composable.T.trim(textNodes.join(' ')); }, - value: function (node) { + value: function (node) { return node ? node.value : null; }, - getAttribute: function (attr) { + getAttribute: function (attr) { return function (node) { return node && node.getAttribute ? node.getAttribute(attr) : null; }; @@ -228,11 +254,11 @@

    composable.js

  • -
  • +
  • - +

    Number transformations

    @@ -241,13 +267,19 @@

    composable.js

            toInt: function (item) {
                 return isString(item) || isNumber(item) ? parseInt(item, 10) : null;
             },
    -        toFloat: function (item) {
    +        toFloat: function (item) {
                 return isString(item) || isNumber(item) ? parseFloat(item) : null;
             },
    -        round: function (item) {
    +        toFixed: function (item) {
    +            var float = isString(item) ? parseFloat(item) : item;
    +            return function (precision) {
    +                return isNumber(float) ? float.toFixed(precision) : null;
    +            };
    +        },
    +        round: function (item) {
                 return isNumber(item) ? Math.round(item) : null;
             },
    -        multiplyBy: function (factor) {
    +        multiplyBy: function (factor) {
                 return function (number) {
                     return isNumber(number) ? factor * number : null;
                 };
    @@ -256,11 +288,11 @@ 

    composable.js

  • -
  • +
  • - +

    String transformations

    @@ -272,7 +304,7 @@

    composable.js

    var firstChild = div.firstChild; return firstChild ? firstChild.nodeValue + String() : ''; }, - reverse: function (item) { + reverse: function (item) { if (isString(item)) { return item.split('').reverse().join(''); } else if (isArray(item)) { @@ -280,13 +312,13 @@

    composable.js

    } return null; }, - toString: function (item) { + toString: function (item) { return item ? item + String() : null; }, - trim: function (text) { + trim: function (text) { return isString(text) ? text.trim() : null; }, - split: function (delimeter, limit) { + split: function (delimeter, limit) { var args = [delimeter]; if (typeof limit !== 'undefined') { args.push(parseInt(limit, 10)); @@ -295,18 +327,18 @@

    composable.js

    return isString(text) ? text.split.apply(text, args) : null; }; }, - join: function (delimeter) { + join: function (delimeter) { return function (array) { return isArray(array) ? array.join(delimeter) : null; }; }, - replace: function (pattern, replacement) { + replace: function (pattern, replacement) { pattern = isString(pattern) && stringIsRegExp(pattern) ? toRegExp(pattern) : pattern; return function (text) { return isString(text) ? text.replace(pattern, replacement) : null; }; }, - match: function (pattern) { + match: function (pattern) { pattern = isString(pattern) && stringIsRegExp(pattern) ? toRegExp(pattern) : pattern; return function (text) { return isString(text) ? text.match(pattern) : null; @@ -316,11 +348,11 @@

    composable.js

  • -
  • +
  • - +

    Array transformations

    @@ -332,7 +364,7 @@

    composable.js

    return isArray(array) && array.length > index ? array[index] : null; }; }, - slice: function (start, stop) { + slice: function (start, stop) { var args = [parseInt(start, 10)]; if (typeof stop !== 'undefined') { args.push(parseInt(stop, 10)); @@ -346,11 +378,11 @@

    composable.js

  • -
  • +
  • - +

    Slice needs to work on NodeList instances

    @@ -365,11 +397,11 @@

    composable.js

  • -
  • +
  • - +

    Object transformations

    @@ -380,7 +412,7 @@

    composable.js

    return object ? object[property] : null; }; }, - getProperties: function (propertyString) { + getProperties: function (propertyString) { return function (object) { if (!object) { return null; @@ -397,7 +429,7 @@

    composable.js

    }; var arrayTransformations = { - map: function (method) { + map: function (method) { return function (array) { return isArray(array) || isNodeList(array) ? Array.prototype.map.call(array, method) : null; }; @@ -407,11 +439,11 @@

    composable.js

  • -
  • +
  • - +

    Allow usage as a function without new operator

    @@ -441,11 +473,11 @@

    composable.js

  • -
  • +
  • - +

    Put transformations on Composable object for global referencing without new operator

    @@ -456,11 +488,11 @@

    composable.js

  • -
  • +
  • - +

    Extract and format data from the DOM based on config

    @@ -485,11 +517,11 @@

    composable.js

  • -
  • +
  • - +

    Apply each transformation to the item, in series

    @@ -515,11 +547,11 @@

    composable.js

  • -
  • +
  • - +

    either command string is empty or no sub command given in recursion

    @@ -562,7 +594,7 @@

    composable.js

    } } - return {command: command, args: args}; + return {command: command, args: args}; }; Composable.prototype.getTransformationForStringTransformation = function (transformationInput) { @@ -580,7 +612,7 @@

    composable.js

    var args = subCommand ? [subCommand] : Composable.extractArgs(commandArgString); - return {command: command, args: args}; + return {command: command, args: args}; }; Composable.extractArgs = function (string) { @@ -607,11 +639,11 @@

    composable.js

  • -
  • +
  • - +

    Make the object globally accessible

    @@ -622,11 +654,11 @@

    composable.js

  • -
  • +
  • - +

    Define it for AMD

    diff --git a/test/spec.js b/test/spec.js index d1114da..1c7e394 100644 --- a/test/spec.js +++ b/test/spec.js @@ -194,6 +194,18 @@ describe('Composable', function () { expect(Composable.T.document()).toEqual(document); }); + it('should get location', function () { + expect(Composable.T.location()).toEqual(window.location); + }); + + it('should get href', function () { + expect(Composable.T.href()).toEqual(window.location.href); + }); + + it('should get pathname', function () { + expect(Composable.T.pathname()).toEqual(window.location.pathname); + }); + it('should run toFloat', function () { expect(Composable.T.toFloat('12.34')).toEqual(12.34); }); @@ -203,6 +215,17 @@ describe('Composable', function () { expect(Composable.T.toInt('12')).toEqual(12); }); + describe('toFixed', function () { + it('should default to 0', function () { + expect(Composable.T.toFixed(1.234)()).toEqual('1'); + }); + + it('should return fixed string at given precision', function () { + expect(Composable.T.toFixed(4.123)(5)).toEqual('4.12300'); + expect(Composable.T.toFixed('4.12345')(3)).toEqual('4.123'); + }); + }); + it('should run round', function () { expect(Composable.T.round(12.34)).toEqual(12); expect(Composable.T.round(12.54)).toEqual(13); @@ -225,6 +248,10 @@ describe('Composable', function () { expect(Composable.T.innerText(document.querySelector('.test-inner-text'))).toEqual('Root Text\nSub Text\n'); }); + it('should run outerHTML', function () { + expect(Composable.T.outerHTML(document.querySelector('.test-match'))).toEqual('
    match
    '); + }); + it('should run rootInnerText', function () { expect(Composable.T.rootInnerText( document.querySelector('.test-root-inner-text'))