From 9cad36394cb74dc187869aae0fc74cc04cc9988f Mon Sep 17 00:00:00 2001 From: Florian Scholz Date: Wed, 10 Jul 2019 16:22:59 +0200 Subject: [PATCH] Add examples for BigInt API (#1380) * Add examples for BigInt API * Address review comments * Correct expected output comment --- .../js-examples/bigint/bigint-asintn.html | 16 +++++++++ .../js-examples/bigint/bigint-asuintn.html | 16 +++++++++ .../bigint/bigint-tolocalestring.html | 12 +++++++ .../js-examples/bigint/bigint-tostring.html | 11 ++++++ .../js-examples/bigint/bigint-valueof.html | 8 +++++ live-examples/js-examples/bigint/meta.json | 34 +++++++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 live-examples/js-examples/bigint/bigint-asintn.html create mode 100644 live-examples/js-examples/bigint/bigint-asuintn.html create mode 100644 live-examples/js-examples/bigint/bigint-tolocalestring.html create mode 100644 live-examples/js-examples/bigint/bigint-tostring.html create mode 100644 live-examples/js-examples/bigint/bigint-valueof.html create mode 100644 live-examples/js-examples/bigint/meta.json diff --git a/live-examples/js-examples/bigint/bigint-asintn.html b/live-examples/js-examples/bigint/bigint-asintn.html new file mode 100644 index 000000000..ab5bc0af2 --- /dev/null +++ b/live-examples/js-examples/bigint/bigint-asintn.html @@ -0,0 +1,16 @@ +
+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
+
+
diff --git a/live-examples/js-examples/bigint/bigint-asuintn.html b/live-examples/js-examples/bigint/bigint-asuintn.html new file mode 100644 index 000000000..ff80067f9 --- /dev/null +++ b/live-examples/js-examples/bigint/bigint-asuintn.html @@ -0,0 +1,16 @@ +
+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
+
+
diff --git a/live-examples/js-examples/bigint/bigint-tolocalestring.html b/live-examples/js-examples/bigint/bigint-tolocalestring.html new file mode 100644 index 000000000..63e0d88b2 --- /dev/null +++ b/live-examples/js-examples/bigint/bigint-tolocalestring.html @@ -0,0 +1,12 @@ +
+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 €"
+
+
diff --git a/live-examples/js-examples/bigint/bigint-tostring.html b/live-examples/js-examples/bigint/bigint-tostring.html new file mode 100644 index 000000000..5999231fa --- /dev/null +++ b/live-examples/js-examples/bigint/bigint-tostring.html @@ -0,0 +1,11 @@ +
+console.log(1024n.toString());
+// expected output: "1024"
+
+console.log(1024n.toString(2));
+// expected output: "10000000000"
+
+console.log(1024n.toString(16));
+// expected output: "400"
+
+
diff --git a/live-examples/js-examples/bigint/bigint-valueof.html b/live-examples/js-examples/bigint/bigint-valueof.html new file mode 100644 index 000000000..826e5edcf --- /dev/null +++ b/live-examples/js-examples/bigint/bigint-valueof.html @@ -0,0 +1,8 @@ +
+console.log(typeof Object(1n));
+// expected output: "object"
+
+console.log(typeof Object(1n).valueOf());
+// expected output: "bigint"
+
+
diff --git a/live-examples/js-examples/bigint/meta.json b/live-examples/js-examples/bigint/meta.json new file mode 100644 index 000000000..cdaa924c0 --- /dev/null +++ b/live-examples/js-examples/bigint/meta.json @@ -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" + } + } +}