aboutsummaryrefslogtreecommitdiff
path: root/node_modules/truncate-utf8-bytes/test.js
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2020-12-22 17:49:59 +0530
committerPriyansh <[email protected]>2020-12-22 17:49:59 +0530
commite93da8b04da86773247aadb1cbb1912e4f4526b2 (patch)
treeeb4ef3203a92ed3dbd2252ddb1ea23bd2d670c98 /node_modules/truncate-utf8-bytes/test.js
parenta5743c293dcb435e4b159a4df791f8955a4110ec (diff)
downloadstyx-e93da8b04da86773247aadb1cbb1912e4f4526b2.tar.xz
styx-e93da8b04da86773247aadb1cbb1912e4f4526b2.zip
Rewriting Project
Diffstat (limited to 'node_modules/truncate-utf8-bytes/test.js')
-rw-r--r--node_modules/truncate-utf8-bytes/test.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/node_modules/truncate-utf8-bytes/test.js b/node_modules/truncate-utf8-bytes/test.js
new file mode 100644
index 0000000..bde24f4
--- /dev/null
+++ b/node_modules/truncate-utf8-bytes/test.js
@@ -0,0 +1,75 @@
+"use strict";
+
+var test = require("tape");
+var truncate = require("./");
+var browserTruncate = require("./browser");
+
+function isHighSurrogate(codePoint) {
+ return codePoint >= 0xd800 && codePoint <= 0xdbff;
+}
+
+function repeat(string, times) {
+ return new Array(times + 1).join(string);
+}
+
+function assertLengths(t, string, charLength, byteLength) {
+ t.equal(string.length, charLength);
+ t.equal(Buffer.byteLength(string), byteLength);
+}
+
+// Test writing files to the fs
+//
+
+try {
+ var blns = require("./vendor/big-list-of-naughty-strings/blns.json");
+}
+catch (err) {
+ console.error("Error: Cannot load file './vendor/big-list-of-naughty-strings/blns.json'");
+ console.error();
+ console.error("Make sure you've initialized git submodules by running");
+ console.error();
+ console.error(" git submodule update --init");
+ console.error();
+ process.exit(1);
+}
+
+// Run tests against both implementations
+[truncate, browserTruncate].forEach(function(truncate) {
+ test("strings", function(t) {
+ assertLengths(t, truncate("a☃", 2), 1, 1);
+ assertLengths(t, truncate(repeat("a", 250) + '\uD800\uDC00', 255), 252, 254);
+ assertLengths(t, truncate(repeat("a", 251) + '\uD800\uDC00', 255), 253, 255);
+ assertLengths(t, truncate(repeat("a", 252) + '\uD800\uDC00', 255), 252, 252);
+ assertLengths(t, truncate(repeat("a", 253) + '\uD800\uDC00', 255), 253, 253);
+ assertLengths(t, truncate(repeat("a", 254) + '\uD800\uDC00', 255), 254, 254);
+ assertLengths(t, truncate(repeat("a", 255) + '\uD800\uDC00', 255), 255, 255);
+ t.end();
+ });
+
+ // Truncate various strings
+ [].concat(
+ [
+ repeat("a", 300),
+ repeat("a", 252) + '\uD800\uDC00',
+ repeat("a", 251) + '\uD800\uDC00',
+ repeat("a", 253) + '\uD800\uDC00',
+ ],
+ blns
+ ).forEach(function(str) {
+ test(JSON.stringify(str), function(t) {
+ var i = 0;
+ t.equals(truncate(str, 0), "");
+ // Truncate string one byte at a time
+ while (true) {
+ var truncated = truncate(str, i);
+ t.ok(Buffer.byteLength(truncated) <= i);
+ t.ok( ! isHighSurrogate(truncated[truncated.length - 1]));
+ if (truncated === str) {
+ break;
+ }
+ i += 1;
+ }
+ t.end();
+ });
+ });
+});