Skip to content

Instantly share code, notes, and snippets.

@rikuba
Last active December 21, 2015 09:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikuba/6285554 to your computer and use it in GitHub Desktop.
Save rikuba/6285554 to your computer and use it in GitHub Desktop.
get function name
Function.prototype.getName =
(function () { return (function foobar() { }).name === 'foobar'; })()
? (function () { return function getName() { return this.name; }; })()
: (function () {
var functionKeywordPattern = (function () {
var parts = [];
var keyword = 'function';
for (var i = 0; i < 8; ++i) {
var ch = keyword.charAt(i);
var code = keyword.charCodeAt(i);
parts[i] = '(?:' + ch + '|\\\\u00' + ignoreCase(code.toString(16)) + ')';
}
return parts.join('');
function ignoreCase(str) {
var result = [];
for (var i = 0, I = str.length; i < I; ++i) {
var ch = str.charAt(i);
var code = str.charCodeAt(i);
result[i] =
code >= 65 && code <= 90 ? '[' + ch + ch.toLowerCase() + ']' :
code >= 97 && code <= 122 ? '[' + ch.toUpperCase() + ch + ']' :
ch;
}
return result.join('');
}
})();
var toString = Function.prototype.toString;
var re_skip = new RegExp('^' + functionKeywordPattern + '(?://.*|/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/|\\s+)+');
var re_name = /^[^(/\s]*/;
var unescapeString =
typeof JSON !== 'undefined'
? function (str) { return JSON.parse('"' + str + '"'); }
: function (str) { return ('global', eval)('"' + str + '"'); };
function getName() {
if (typeof this !== 'function') {
throw new TypeError('Function.prototype.getName is not generic');
}
if (typeof this.name === 'string') {
return this.name;
}
var text = toString.call(this).replace(re_skip, '');
var name = re_name.exec(text)[0];
if (name.indexOf('\\') >= 0) {
name = unescapeString(name);
}
this.name = name;
return name;
}
return getName;
})();
t
(function () {
if (function foobar() { }.name === 'foobar') { return; }
var getName = (function () {
var functionKeywordPattern = (function () {
var parts = [];
var keyword = 'function';
for (var i = 0; i < 8; ++i) {
var ch = keyword.charAt(i);
var code = keyword.charCodeAt(i);
parts[i] = '(?:' + ch + '|\\\\u00' + ignoreCase(code.toString(16)) + ')';
}
return parts.join('');
function ignoreCase(str) {
var result = [];
for (var i = 0, I = str.length; i < I; ++i) {
var ch = str.charAt(i);
var code = str.charCodeAt(i);
result[i] =
code >= 65 && code <= 90 ? '[' + ch + ch.toLowerCase() + ']' :
code >= 97 && code <= 122 ? '[' + ch.toUpperCase() + ch + ']' :
ch;
}
return result.join('');
}
})();
var toString = Function.prototype.toString;
var re_skip = new RegExp('^' + functionKeywordPattern + '(?://.*|/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/|\\s+)+');
var re_name = /^[^(/\s]*/;
var unescapeString =
typeof JSON !== 'undefined'
? function (str) { return JSON.parse('"' + str + '"'); }
: function (str) { return ('global', eval)('"' + str + '"'); };
function getName() {
if (typeof this !== 'function') {
throw new TypeError('Function.prototype.getName is not generic');
}
var text = toString.call(this).replace(re_skip, '');
var name = re_name.exec(text)[0];
if (name.indexOf('\\') >= 0) {
name = unescapeString(name);
}
Object.defineProperty(this, 'name', {
value: name,
configurable: false,
enumerable: false,
writable: false
});
return name;
}
return getName;
})();
Object.defineProperty(Function.prototype, 'name', {
get: getName,
configurable: true,
enumerable: false
});
})();
@rikuba
Copy link
Author

rikuba commented Aug 20, 2013

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment