Skip to content

Commit

Permalink
teach closure about Symbol.toPrimitive
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed Jan 9, 2019
1 parent ca0db9e commit 64dc015
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 2 additions & 0 deletions js/gulp/closure-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ function externsHeader() {
/** @type {symbol} */
Symbol.iterator;
/** @type {symbol} */
Symbol.toPrimitive;
/** @type {symbol} */
Symbol.asyncIterator;
`);
}
Expand Down
7 changes: 3 additions & 4 deletions js/src/util/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type IntArray = Int8Array | Int16Array | Int32Array;
type UintArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray;

const BigNumNMixin = {
toJSON(this: BN<BigNumArray>, ) { return `"${this[Symbol.toPrimitive]('string')}"`; },
valueOf(this: BN<BigNumArray>, ) { return this[Symbol.toPrimitive]('number') as number; },
toString(this: BN<BigNumArray>, ) { return this[Symbol.toPrimitive]('string') as string; },
toJSON(this: BN<BigNumArray>, ) { return `"${bignumToString(this)}"`; },
valueOf(this: BN<BigNumArray>, ) { return bignumToNumber(this); },
toString(this: BN<BigNumArray>, ) { return bignumToString(this); },
[Symbol.toPrimitive]<T extends BN<BigNumArray>>(this: T, hint: 'string' | 'number' | 'default') {
if (hint === 'number') { return bignumToNumber(this); }
/** @suppress {missingRequire} */
Expand Down Expand Up @@ -95,7 +95,6 @@ export interface BN<T extends BigNumArray> extends TypedArrayLike<T> {
* so it's compatible with JSON.stringify().
*/
toJSON(): string;
[Symbol.iterator](): IterableIterator<BN<T>>;
[Symbol.toPrimitive](hint: any): number | string | bigint;
}

Expand Down

0 comments on commit 64dc015

Please sign in to comment.