From 67bf078e728412931de2f6efd48b2493ffbef6e0 Mon Sep 17 00:00:00 2001 From: Jonathan Ostrander Date: Thu, 11 Aug 2016 11:37:41 -0400 Subject: [PATCH 1/3] adds toFixed transformation --- component.json | 2 +- composable.js | 8 +++++++- composable.min.js | 2 +- composable.min.map | 2 +- docs/composable.html | 8 +++++++- package.json | 2 +- test/spec.js | 11 +++++++++++ 7 files changed, 29 insertions(+), 6 deletions(-) diff --git a/component.json b/component.json index a414d5c..855a985 100644 --- a/component.json +++ b/component.json @@ -10,6 +10,6 @@ "repo": "vail130/composable-js", "main": "composable.js", "scripts": ["composable.js"], - "version": "0.4.4", + "version": "0.4.5", "license": "MIT" } diff --git a/composable.js b/composable.js index 867bb56..2391fcf 100644 --- a/composable.js +++ b/composable.js @@ -126,6 +126,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; }, @@ -373,7 +379,7 @@ return args; }; - Composable.VERSION = '0.4.4'; + Composable.VERSION = '0.4.5'; // Make the object globally accessible root.Composable = Composable; diff --git a/composable.min.js b/composable.min.js index 1fe4f9e..0d6352c 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(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;rcomposable.js 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; }, @@ -601,7 +607,7 @@

composable.js

return args; }; - Composable.VERSION = '0.4.4'; + Composable.VERSION = '0.4.5'; diff --git a/package.json b/package.json index 7ba97e4..aa068de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "composable", - "version": "0.4.4", + "version": "0.4.5", "description": "A JS library to select, transform and format data, declaratively.", "main": "composable.js", "repository": { diff --git a/test/spec.js b/test/spec.js index ec7e6eb..95d8815 100644 --- a/test/spec.js +++ b/test/spec.js @@ -203,6 +203,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); From f5d0bcfe3957bd58da40e4cfc19f0b4ef4119e0d Mon Sep 17 00:00:00 2001 From: PhilWinchester Date: Mon, 18 Sep 2017 15:14:00 -0400 Subject: [PATCH 2/3] adding transformation support to get window location/href --- composable.js | 11 ++++ composable.min.js | 2 +- composable.min.map | 2 +- docs/composable.html | 117 ++++++++++++++++++++++++++----------------- test/spec.js | 12 +++++ 5 files changed, 95 insertions(+), 49 deletions(-) diff --git a/composable.js b/composable.js index 5f6bc9e..e9ef4e5 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) { diff --git a/composable.min.js b/composable.min.js index 62cf9ce..ddc2b2b 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,18 @@

    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) { + innerText: function (node) { return node ? node.innerText || node.textContent : null; }, - rootInnerText: function (node) { + rootInnerText: function (node) { if (!node) return null; var textNodes = []; @@ -216,10 +239,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 +251,11 @@

    composable.js

  • -
  • +
  • - +

    Number transformations

    @@ -241,19 +264,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;
             },
    -        toFixed: 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) {
    +        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;
                 };
    @@ -262,11 +285,11 @@ 

    composable.js

  • -
  • +
  • - +

    String transformations

    @@ -278,7 +301,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)) { @@ -286,13 +309,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)); @@ -301,18 +324,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; @@ -322,11 +345,11 @@

    composable.js

  • -
  • +
  • - +

    Array transformations

    @@ -338,7 +361,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)); @@ -352,11 +375,11 @@

    composable.js

  • -
  • +
  • - +

    Slice needs to work on NodeList instances

    @@ -371,11 +394,11 @@

    composable.js

  • -
  • +
  • - +

    Object transformations

    @@ -386,7 +409,7 @@

    composable.js

    return object ? object[property] : null; }; }, - getProperties: function (propertyString) { + getProperties: function (propertyString) { return function (object) { if (!object) { return null; @@ -403,7 +426,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; }; @@ -413,11 +436,11 @@

    composable.js

  • -
  • +
  • - +

    Allow usage as a function without new operator

    @@ -447,11 +470,11 @@

    composable.js

  • -
  • +
  • - +

    Put transformations on Composable object for global referencing without new operator

    @@ -462,11 +485,11 @@

    composable.js

  • -
  • +
  • - +

    Extract and format data from the DOM based on config

    @@ -491,11 +514,11 @@

    composable.js

  • -
  • +
  • - +

    Apply each transformation to the item, in series

    @@ -521,11 +544,11 @@

    composable.js

  • -
  • +
  • - +

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

    @@ -568,7 +591,7 @@

    composable.js

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

    composable.js

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

    composable.js

  • -
  • +
  • - +

    Make the object globally accessible

    @@ -628,11 +651,11 @@

    composable.js

  • -
  • +
  • - +

    Define it for AMD

    diff --git a/test/spec.js b/test/spec.js index b519d8d..928b708 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); }); From 69ea700fdc67d956142932034ec901d3b3b71181 Mon Sep 17 00:00:00 2001 From: PhilWinchester Date: Fri, 22 Sep 2017 15:40:51 -0400 Subject: [PATCH 3/3] adding outerHTML query --- composable.js | 3 +++ composable.min.js | 2 +- composable.min.map | 2 +- docs/composable.html | 3 +++ test/spec.js | 4 ++++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/composable.js b/composable.js index e9ef4e5..c99a205 100644 --- a/composable.js +++ b/composable.js @@ -107,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; }, diff --git a/composable.min.js b/composable.min.js index ddc2b2b..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"},o=function(n){return Array.isArray?Array.isArray(n):Object.prototype.toString.call(n)==="[object Array]"},i=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},location:function(){return n.location},href:function(){return n.location.href},pathname:function(){return n.location.pathname},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(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;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 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; }, diff --git a/test/spec.js b/test/spec.js index 928b708..1c7e394 100644 --- a/test/spec.js +++ b/test/spec.js @@ -248,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'))