-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] add
getSymbolDescription
and getInferredName
helpers
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
var getInferredName; | ||
try { | ||
// eslint-disable-next-line no-new-func | ||
getInferredName = Function('s', 'return { [s]() {} }[s].name;'); | ||
} catch (e) {} | ||
|
||
var inferred = function () {}; | ||
module.exports = getInferredName && inferred.name === 'inferred' ? getInferredName : null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
var GetIntrinsic = require('../GetIntrinsic'); | ||
|
||
var callBound = require('./callBound'); | ||
|
||
var $SyntaxError = GetIntrinsic('%SyntaxError%'); | ||
var symToStr = callBound('Symbol.prototype.toString', true); | ||
|
||
var getInferredName = require('./getInferredName'); | ||
|
||
module.exports = function getSymbolDescription(symbol) { | ||
if (!symToStr) { | ||
throw new $SyntaxError('Symbols are not supported in this environment'); | ||
} | ||
var str = symToStr(symbol); // will throw if not a symbol | ||
|
||
if (getInferredName) { | ||
var name = getInferredName(symbol); | ||
if (name === '') { return; } | ||
// eslint-disable-next-line consistent-return | ||
return name.slice(1, -1); // name.slice('['.length, -']'.length); | ||
} | ||
|
||
var desc = str.slice(7, -1); // str.slice('Symbol('.length, -')'.length); | ||
if (desc) { | ||
// eslint-disable-next-line consistent-return | ||
return desc; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
var test = require('tape'); | ||
var debug = require('object-inspect'); | ||
var forEach = require('foreach'); | ||
|
||
var v = require('./values'); | ||
var getSymbolDescription = require('../../helpers/getSymbolDescription'); | ||
|
||
test('getSymbolDescription', function (t) { | ||
t.test('no symbols', { skip: v.hasSymbols }, function (st) { | ||
st['throws']( | ||
getSymbolDescription, | ||
SyntaxError, | ||
'requires Symbol support' | ||
); | ||
|
||
st.end(); | ||
}); | ||
|
||
forEach(v.nonSymbolPrimitives.concat(v.objects), function (nonSymbol) { | ||
t['throws']( | ||
function () { getSymbolDescription(nonSymbol); }, | ||
TypeError, | ||
debug(nonSymbol) + ' is not a Symbol' | ||
); | ||
}); | ||
|
||
t.test('with symbols', { skip: !v.hasSymbols }, function (st) { | ||
forEach( | ||
[ | ||
[Symbol(), undefined], | ||
[Symbol(undefined), undefined], | ||
[Symbol(null), 'null'], | ||
[Symbol(''), ''], | ||
[Symbol.iterator, 'Symbol.iterator'], | ||
[Symbol('foo'), 'foo'] | ||
], | ||
function (pair) { | ||
var sym = pair[0]; | ||
var desc = pair[1]; | ||
st.equal(getSymbolDescription(sym), desc, debug(sym) + ' yields ' + debug(desc)); | ||
} | ||
); | ||
|
||
st.end(); | ||
}); | ||
|
||
t.end(); | ||
}); |