Skip to content

Commit 0060e90

Browse files
Fix construction from [Symbol.toPrimitive] (#102)
Fixes #101.
1 parent c40371e commit 0060e90

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/jsbi.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,9 +1819,8 @@ class JSBI extends Array {
18191819
if (obj.constructor === JSBI) return obj;
18201820
if (typeof Symbol !== 'undefined' &&
18211821
typeof Symbol.toPrimitive === 'symbol') {
1822-
const exoticToPrim = obj[Symbol.toPrimitive];
1823-
if (exoticToPrim) {
1824-
const primitive = exoticToPrim(hint);
1822+
if (obj[Symbol.toPrimitive]) {
1823+
const primitive = obj[Symbol.toPrimitive](hint);
18251824
if (typeof primitive !== 'object') return primitive;
18261825
throw new TypeError('Cannot convert object to primitive value');
18271826
}

tests/tests.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ const TESTS = [
131131
}
132132
})();
133133

134+
// https://github.com/GoogleChromeLabs/jsbi/issues/101
135+
(function() {
136+
const o = {
137+
num: 123,
138+
[Symbol.toPrimitive]: function() { return this.num; }
139+
};
140+
const result = JSBI.BigInt(o);
141+
assertTrue(JSBI.equal(result, JSBI.BigInt(123)));
142+
})();
143+
134144
function parse(string) {
135145
if (string.charCodeAt(0) === 0x2D) { // '-'
136146
return JSBI.unaryMinus(JSBI.BigInt(string.slice(1)));

0 commit comments

Comments
 (0)