Skip to content

Instantly share code, notes, and snippets.

@nowhereman
Last active June 11, 2016 21:27
Show Gist options
  • Select an option

  • Save nowhereman/1267298 to your computer and use it in GitHub Desktop.

Select an option

Save nowhereman/1267298 to your computer and use it in GitHub Desktop.

Revisions

  1. nowhereman revised this gist Jun 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Object_tap.html
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <meta charset="utf-8" />
    <title>Object#tap in JavaScript</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript" src="http://sugarjs.com/javascripts/sugar-0.9.5.min.js""></script>
    <script type="text/javascript" src="http://sugarjs.com/javascripts/sugar-0.9.5.min.js"></script>
    <script type="text/javascript">
    jQuery.noConflict();
    (function($) {
  2. nowhereman revised this gist Oct 7, 2011. 1 changed file with 20 additions and 12 deletions.
    32 changes: 20 additions & 12 deletions Object_tap.html
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,12 @@
    <meta charset="utf-8" />
    <title>Object#tap in JavaScript</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://raw.github.com/andrewplummer/Sugar/master/release/sugar-0.9.5.min.js"></script>
    <script type="text/javascript" src="http://sugarjs.com/javascripts/sugar-0.9.5.min.js""></script>
    <script type="text/javascript">
    jQuery.noConflict();
    (function($) {
    $(function(){

    // Object#tap method
    if(!Object.prototype.tap) { // if this method does not exist..
    Object.prototype.tap = function(name) {
    @@ -18,34 +18,42 @@
    name.apply(this);
    } else {
    var args = Array.prototype.slice.call(arguments);
    this[name].apply(this, args.slice(1));
    this[name].apply(this, args.slice(1));
    }
    return this;
    };
    }

    // Fails
    //var withoutTap = [1, 2, 3, 4, 5].pop().map(function (n) { return n * 2 });
    //console.debug(withoutTap);//debug

    // Pass
    //var tapTest = [1, 2, 3, 4, 5].tap.pop.map { |n| n * 2 };
    var withTap = [1, 2, 3, 4, 5].tap(function() { this.pop() }).map(function (n) { return n * 2 });
    var withTap = [1, 2, 3, 4, 5].tap(function() { this.pop(); }).map(function (n) { return n * 2 });
    //console.debug(withTap);//debug
    $("#test-panel").append("Test1: " + withTap.toString() + "<br />");

    var withTap2 = [1, 2, 3, 4, 5].tap("pop").map(function (n) { return n * 2 });
    //console.debug(withTap2);//debug
    $("#test-panel").append("Test2: " + withTap2.toString() + "<br />");
    var withTap3 = [1, 2].tap(function() { this.push(3, 4) }).map(function (n) { return n * 2 });

    var withTap3 = [1, 2].tap(function() { this.push(3, 4); }).map(function (n) { return n * 2 });
    //console.debug(withTap3);//debug
    $("#test-panel").append("Test3: " + withTap3.toString() + "<br />");

    var withTap4 = [1, 2].tap("push", 3, 4).map(function (n) { return n * 2 });
    //console.debug(withTap4);//debug
    $("#test-panel").append("Test4: " + withTap4.toString() + "<br />");


    var withTap5 = [1, 2, 3].tap(function() { if(this.none(4)) this.add(4); }).map(function (n) { return n * 2 });
    //console.debug(withTap5);//debug
    $("#test-panel").append("Test5: " + withTap5.toString() + "<br />");

    var withTap6 = [1, 2, 3, 4].tap(function() { if(this.last() === 5) this.pop(); }).map(function (n) { return n * 2 });
    //console.debug(withTap6);//debug
    $("#test-panel").append("Test6: " + withTap6.toString() + "<br />");

    });
    })(jQuery);
    </script>
    @@ -57,8 +65,8 @@ <h2>Object#tap in JavaScript</h2>
    Based on a <a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html">Ruby blog post</a>, <br />
    a <a href="https://gist.github.com/1025583">JavaScript Gist</a>,<br />
    And a <a href="http://d.hatena.ne.jp/javascripter/20081107/1226046467">Japanese Blog post</a>.<br />
    Tested on IE6, IE7, Firefox 7, Chrome 14, Safari 5 and Opera 11.51.
    Tested on IE6, IE7, Firefox 7, Chrome 14, Safari 5 and Opera 11.5.
    </p>
    <div id="test-panel">Expected: 2,4,6,8<br /></div>
    </body>
    </html>
    </html>
  3. nowhereman revised this gist Oct 7, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Object_tap.html
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@
    jQuery.noConflict();
    (function($) {
    $(function(){

    // Object#tap method
    if(!Object.prototype.tap) { // if this method does not exist..
    Object.prototype.tap = function(name) {
    //console.debug(typeof(name));//debug
    @@ -55,7 +57,7 @@ <h2>Object#tap in JavaScript</h2>
    Based on a <a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html">Ruby blog post</a>, <br />
    a <a href="https://gist.github.com/1025583">JavaScript Gist</a>,<br />
    And a <a href="http://d.hatena.ne.jp/javascripter/20081107/1226046467">Japanese Blog post</a>.<br />
    Tested on IE7, Firefox 7 and Chrome 14.
    Tested on IE6, IE7, Firefox 7, Chrome 14, Safari 5 and Opera 11.51.
    </p>
    <div id="test-panel">Expected: 2,4,6,8<br /></div>
    </body>
  4. nowhereman created this gist Oct 6, 2011.
    62 changes: 62 additions & 0 deletions Object_tap.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>Object#tap in JavaScript</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://raw.github.com/andrewplummer/Sugar/master/release/sugar-0.9.5.min.js"></script>
    <script type="text/javascript">
    jQuery.noConflict();
    (function($) {
    $(function(){
    if(!Object.prototype.tap) { // if this method does not exist..
    Object.prototype.tap = function(name) {
    //console.debug(typeof(name));//debug
    if(typeof(name) == "function") {
    name.apply(this);
    } else {
    var args = Array.prototype.slice.call(arguments);
    this[name].apply(this, args.slice(1));
    }
    return this;
    };
    }

    // Fails
    //var withoutTap = [1, 2, 3, 4, 5].pop().map(function (n) { return n * 2 });
    //console.debug(withoutTap);//debug

    // Pass
    //var tapTest = [1, 2, 3, 4, 5].tap.pop.map { |n| n * 2 };
    var withTap = [1, 2, 3, 4, 5].tap(function() { this.pop() }).map(function (n) { return n * 2 });
    //console.debug(withTap);//debug
    $("#test-panel").append("Test1: " + withTap.toString() + "<br />");

    var withTap2 = [1, 2, 3, 4, 5].tap("pop").map(function (n) { return n * 2 });
    //console.debug(withTap2);//debug
    $("#test-panel").append("Test2: " + withTap2.toString() + "<br />");

    var withTap3 = [1, 2].tap(function() { this.push(3, 4) }).map(function (n) { return n * 2 });
    //console.debug(withTap3);//debug
    $("#test-panel").append("Test3: " + withTap3.toString() + "<br />");

    var withTap4 = [1, 2].tap("push", 3, 4).map(function (n) { return n * 2 });
    //console.debug(withTap4);//debug
    $("#test-panel").append("Test4: " + withTap4.toString() + "<br />");

    });
    })(jQuery);
    </script>
    </head>
    <body>
    <h2>Object#tap in JavaScript</h2>
    <p>
    With the help of <a href="http://sugarjs.com">Sugar JavaScript lib</a>.<br />
    Based on a <a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html">Ruby blog post</a>, <br />
    a <a href="https://gist.github.com/1025583">JavaScript Gist</a>,<br />
    And a <a href="http://d.hatena.ne.jp/javascripter/20081107/1226046467">Japanese Blog post</a>.<br />
    Tested on IE7, Firefox 7 and Chrome 14.
    </p>
    <div id="test-panel">Expected: 2,4,6,8<br /></div>
    </body>
    </html>