Skip to content

Commit

Permalink
Add examples for BigInt API (#1380)
Browse files Browse the repository at this point in the history
* Add examples for BigInt API

* Address review comments

* Correct expected output comment
  • Loading branch information
Elchi3 authored and wbamberg committed Jul 10, 2019
1 parent 6fe4232 commit 9cad363
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
16 changes: 16 additions & 0 deletions live-examples/js-examples/bigint/bigint-asintn.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre>
<code id="static-js">const max = 2n ** (64n - 1n) - 1n;

function check64bit(number) {
(number > max) ?
console.log("Number doesn't fit in signed 64-bit integer!") :
console.log(BigInt.asIntN(64, number));
}

check64bit(2n ** 64n);
// expected output: "Number doesn't fit in signed 64-bit integer!"

check64bit(2n ** 32n);
// expected output: 4294967296n
</code>
</pre>
16 changes: 16 additions & 0 deletions live-examples/js-examples/bigint/bigint-asuintn.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre>
<code id="static-js">const max = 2n ** 64n - 1n;

function check64bit(number) {
(number > max) ?
console.log("Number doesn't fit in unsigned 64-bit integer!") :
console.log(BigInt.asUintN(64, number));
}

check64bit(2n ** 64n);
// expected output: "Number doesn't fit in unsigned 64-bit integer!"

check64bit(2n ** 32n);
// expected output: 4294967296n
</code>
</pre>
12 changes: 12 additions & 0 deletions live-examples/js-examples/bigint/bigint-tolocalestring.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<pre>
<code id="static-js">const bigint = 123456789123456789n;

// German uses period for thousands
console.log(bigint.toLocaleString('de-DE'));
// expected output: "123.456.789.123.456.789"

// request a currency format
console.log(bigint.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
// expected output: "123.456.789.123.456.789,00 €"
</code>
</pre>
11 changes: 11 additions & 0 deletions live-examples/js-examples/bigint/bigint-tostring.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<pre>
<code id="static-js">console.log(1024n.toString());
// expected output: "1024"

console.log(1024n.toString(2));
// expected output: "10000000000"

console.log(1024n.toString(16));
// expected output: "400"
</code>
</pre>
8 changes: 8 additions & 0 deletions live-examples/js-examples/bigint/bigint-valueof.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<pre>
<code id="static-js">console.log(typeof Object(1n));
// expected output: "object"

console.log(typeof Object(1n).valueOf());
// expected output: "bigint"
</code>
</pre>
34 changes: 34 additions & 0 deletions live-examples/js-examples/bigint/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"pages": {
"bigIntasIntN": {
"exampleCode": "./live-examples/js-examples/bigint/bigint-asintn.html",
"fileName": "bigint-asintn.html",
"title": "JavaScript Demo: BigInt.asIntN()",
"type": "js"
},
"bigIntasUintN": {
"exampleCode": "./live-examples/js-examples/bigint/bigint-asuintn.html",
"fileName": "bigint-asuintn.html",
"title": "JavaScript Demo: BigInt.asUintN()",
"type": "js"
},
"bigIntToLocaleString": {
"exampleCode": "./live-examples/js-examples/bigint/bigint-tolocalestring.html",
"fileName": "bigint-tolocalestring.html",
"title": "JavaScript Demo: BigInt.toLocaleString()",
"type": "js"
},
"bigIntToString": {
"exampleCode": "./live-examples/js-examples/bigint/bigint-tostring.html",
"fileName": "bigint-tostring.html",
"title": "JavaScript Demo: BigInt.toString()",
"type": "js"
},
"bigIntValueof": {
"exampleCode": "./live-examples/js-examples/bigint/bigint-valueof.html",
"fileName": "bigint-valueof.html",
"title": "JavaScript Demo: BigInt.valueOf()",
"type": "js"
}
}
}

0 comments on commit 9cad363

Please sign in to comment.