Skip to content

Instantly share code, notes, and snippets.

@nowhereman
Last active June 11, 2016 21:27
Show Gist options
  • Save nowhereman/1267298 to your computer and use it in GitHub Desktop.
Save nowhereman/1267298 to your computer and use it in GitHub Desktop.
Object#tap in JavaScript
<!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="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) {
//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 />");
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>
</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 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment