├── .gitignore ├── .npmignore ├── LICENSE.txt ├── Makefile ├── README.md ├── build ├── caller-of.amd.js ├── caller-of.js └── caller-of.node.js ├── index.html ├── package.json ├── src └── caller-of.js ├── template ├── amd.after ├── amd.before ├── copyright ├── node.after └── node.before └── test ├── Function.prototype.js └── caller-of.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/* 2 | test/* 3 | template/* 4 | node_modules/* 5 | build/caller-of.amd.js 6 | build/caller-of.js 7 | Makefile 8 | index.html -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) Andrea Giammarchi 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean test dependencies 2 | 3 | # repository name 4 | REPO = repo_name 5 | 6 | # default build task 7 | build: 8 | mkdir -p build 9 | cat src/caller-of.js >build/no-copy.caller-of.max.js 10 | node node_modules/uglify-js/bin/uglifyjs --verbose build/no-copy.caller-of.max.js >build/no-copy.caller-of.js 11 | cat template/copyright build/no-copy.caller-of.js >build/caller-of.js 12 | # node.js var EventTarget = require('caller-of'); 13 | cat template/node.before src/caller-of.js template/node.after >build/no-copy.caller-of.max.node.js 14 | node node_modules/uglify-js/bin/uglifyjs --verbose build/no-copy.caller-of.max.node.js >build/no-copy.caller-of.node.js 15 | cat template/copyright build/no-copy.caller-of.node.js >build/caller-of.node.js 16 | # AMD define('caller-of', callerOf) 17 | cat template/amd.before src/caller-of.js template/amd.after >build/no-copy.caller-of.max.amd.js 18 | node node_modules/uglify-js/bin/uglifyjs --verbose build/no-copy.caller-of.max.amd.js >build/no-copy.caller-of.amd.js 19 | cat template/copyright build/no-copy.caller-of.amd.js >build/caller-of.amd.js 20 | rm build/no-copy.*.js 21 | make test 22 | 23 | # clean/remove build folder 24 | clean: 25 | rm -rf build 26 | 27 | # tests, as usual and of course 28 | test: 29 | node node_modules/wru/node/program.js test/caller-of.js 30 | 31 | # launch polpetta (ctrl+click to open the page) 32 | web: 33 | node node_modules/polpetta/build/polpetta ./ 34 | 35 | # copy some stuff over gh-pages repository 36 | pages: 37 | mkdir -p ~/tmp/$(REPO) 38 | cp -rf src ~/tmp/$(REPO) 39 | cp -rf test ~/tmp/$(REPO) 40 | cp index.html ~/tmp/$(REPO) 41 | git checkout gh-pages 42 | mkdir -p test 43 | rm -rf test 44 | cp -rf ~/tmp/$(REPO) test 45 | git add . 46 | git commit -m 'automatic test generator' 47 | git push 48 | git checkout master 49 | rm -r ~/tmp/$(REPO) 50 | 51 | # modules used in this repo 52 | dependencies: 53 | rm -rf node_modules 54 | mkdir node_modules 55 | npm install wru 56 | npm install polpetta 57 | npm install uglify-js@1 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | caller-of 2 | ========= 3 | 4 | The tiniest yet most powerful JS utility ever :D 5 | 6 | ```JavaScript 7 | /** WTFPL Style License */ 8 | function callerOf(c) {return c.call.bind(c)} 9 | ``` 10 | 11 | ### What Does Above Code Do 12 | Here is what we usually do 13 | ```JavaScript 14 | object.hasOwnProperty(key) 15 | ``` 16 | Here is what `callerOf` create 17 | ```JavaScript 18 | hasOwnProperty(object, key) 19 | ``` 20 | so we can borrow any method at any time and reuse it in a minifier friendly way. 21 | 22 | ```JavaScript 23 | var bind = callerOf(Function.bind); 24 | 25 | // easy log 26 | var log = bind(console.log, console); 27 | log('it works'); 28 | 29 | // common borrowed methods 30 | var 31 | has = callerOf({}.hasOwnProperty), 32 | whoIs = callerOf({}.toString), 33 | forEach = callerOf([].forEach), 34 | slice = callerOf([].slice); 35 | 36 | has({a:1}, "a"); // true 37 | has({a:1}, "toString"); // false 38 | 39 | whoIs([]); // "[object Array]" 40 | whoIs(false); // "[object Boolean]" 41 | 42 | slice(document.querySelectorAll("*")); // array 43 | (function (obj){ 44 | var args = slice(arguments, 1); 45 | }()); 46 | 47 | forEach(document.getElementsByTagName("body"), log); 48 | ``` 49 | 50 | ### Compatibility 51 | Every JavaScript engine I know, included IE. 52 | 53 | For *node.js* simply `npm install caller-of` then `var callerOf = require('caller-of')` 54 | 55 | ### What If No Function.prototype.bind 56 | You can use this tiny yet working polyfill ^_^ 57 | ```JavaScript 58 | // 139 bytes gzipped 59 | /*! (C) WebReflection, Mit Style License */ 60 | (function (P) { 61 | 'use strict'; 62 | if (!P.bind) { 63 | P.bind = function (s) { 64 | var 65 | c = this, 66 | l = [].slice, 67 | a = l.call(arguments, 1); 68 | return function bind() { 69 | return c.apply(s, a.concat(l.call(arguments))); 70 | }; 71 | }; 72 | } 73 | }(Function.prototype)); 74 | ``` -------------------------------------------------------------------------------- /build/caller-of.amd.js: -------------------------------------------------------------------------------- 1 | /*!(C)WebReflection Mit Style License*/ 2 | define("caller-of",function(){function e(e){"use strict";return e.call.bind(e)}return}); -------------------------------------------------------------------------------- /build/caller-of.js: -------------------------------------------------------------------------------- 1 | /*!(C)WebReflection Mit Style License*/ 2 | // (C) WebReflection, Mit Style License 3 | function callerOf(e){"use strict";return e.call.bind(e)}; -------------------------------------------------------------------------------- /build/caller-of.node.js: -------------------------------------------------------------------------------- 1 | /*!(C)WebReflection Mit Style License*/ 2 | module.exports=function(t){"use strict";return t.call.bind(t)}; -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | wru test 5 | 6 | 10 | 41 | 42 | 43 | 75 | 76 | 77 |
78 | 84 | 85 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.1", 3 | "name": "caller-of", 4 | "description": "The tiniest yet most powerful JS utility ever :D", 5 | "homepage": "https://github.com/WebReflection/caller-of", 6 | "main": "./build/caller-of.node.js", 7 | "keywords": ["development", "web", "server", "terminal", "prototyping", "node"], 8 | "author": { 9 | "name": "Andrea Giammarchi", 10 | "web": "http://webreflection.blogspot.com/" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/WebReflection/caller-of.git" 15 | } 16 | } -------------------------------------------------------------------------------- /src/caller-of.js: -------------------------------------------------------------------------------- 1 | function callerOf(c) {'use strict';return c.call.bind(c)} -------------------------------------------------------------------------------- /template/amd.after: -------------------------------------------------------------------------------- 1 | ;}); -------------------------------------------------------------------------------- /template/amd.before: -------------------------------------------------------------------------------- 1 | define('caller-of',function(){return -------------------------------------------------------------------------------- /template/copyright: -------------------------------------------------------------------------------- 1 | /*!(C)WebReflection Mit Style License*/ 2 | -------------------------------------------------------------------------------- /template/node.after: -------------------------------------------------------------------------------- 1 | ; -------------------------------------------------------------------------------- /template/node.before: -------------------------------------------------------------------------------- 1 | module.exports = -------------------------------------------------------------------------------- /test/Function.prototype.js: -------------------------------------------------------------------------------- 1 | /*! (C) WebReflection, Mit Style License */ 2 | (function (P, l) { 3 | 'use strict'; 4 | if (!P.bind) { 5 | P.bind = function (s) { 6 | var 7 | c = this, 8 | a = l.call(arguments, 1); 9 | return function bind() { 10 | return c.apply(s, a.concat(l.call(arguments))); 11 | }; 12 | }; 13 | } 14 | }(Function.prototype, [].slice)); -------------------------------------------------------------------------------- /test/caller-of.js: -------------------------------------------------------------------------------- 1 | //remove: 2 | var callerOf = require('../build/caller-of.node.js'); 3 | //:remove 4 | 5 | wru.test([ 6 | { 7 | name: "100% code coverage", 8 | test: function () { 9 | var 10 | slice = callerOf([].slice), 11 | fakeObject = {length:2,"0":"a","1":"b",}; 12 | wru.assert("it slices", slice(fakeObject).join(",") === "a,b"); 13 | wru.assert("it slices(1)", slice(fakeObject, 1).join(",") === "b"); 14 | } 15 | } 16 | ]); --------------------------------------------------------------------------------