Skip to content

Commit

Permalink
[New] add functionsHaveConfigurableNames function on main export
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 23, 2019
1 parent 836819c commit ce73f75
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"extends": "@ljharb",

"rules": {
}
"func-name-matching": [2, "always"],
"id-length": 1,
},
}
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

module.exports = function functionsHaveNames() {
var functionsHaveNames = function functionsHaveNames() {
return function f() {}.name === 'f';
};

var gOPD = Object.getOwnPropertyDescriptor;

functionsHaveNames.functionsHaveConfigurableNames = function functionsHaveConfigurableNames() {
return functionsHaveNames() && gOPD && !!gOPD(function () {}, 'name').configurable;
};

module.exports = functionsHaveNames;
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,32 @@ test('named functions', function (t) {
function f() {} // eslint-disable-line func-style
var g = function h() {};

t.equal(typeof hasNames, 'function', 'is a function');
t.equal(hasNames(), f.name === 'f' && g.name === 'h', 'functions have names or not as expected');

t.end();
});

test('functionsHaveConfigurableNames', function (t) {
t.equal(typeof hasNames.functionsHaveConfigurableNames, 'function', 'is a function');

if (hasNames()) {
var fn = function f() {};
if (Object.defineProperty) {
Object.defineProperty(fn, 'name', { configurable: true, value: 'foo' });
if (fn.name === 'f') {
t.equal(hasNames.functionsHaveConfigurableNames(), false, 'function names are not configurable');
} else if (fn.name === 'foo') {
t.equal(hasNames.functionsHaveConfigurableNames(), true, 'function names are not configurable');
} else {
t.fail('functions have names, but something surprising has happened. Please report this!');
}
} else {
t.equal(hasNames.functionsHaveConfigurableNames(), false, 'function names are not configurable');
}
} else {
t.equal(hasNames.functionsHaveConfigurableNames(), false, 'functions do not have names');
}

t.end();
});

0 comments on commit ce73f75

Please sign in to comment.