diff options
Diffstat (limited to 'node_modules/truncate-utf8-bytes')
| -rw-r--r-- | node_modules/truncate-utf8-bytes/.gitmodules | 3 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/.npmignore | 1 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/.travis.yml | 12 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/AUTHORS | 2 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/README.md | 27 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/browser.js | 5 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/index.js | 5 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/lib/truncate.js | 43 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/package.json | 68 | ||||
| -rw-r--r-- | node_modules/truncate-utf8-bytes/test.js | 75 |
10 files changed, 241 insertions, 0 deletions
diff --git a/node_modules/truncate-utf8-bytes/.gitmodules b/node_modules/truncate-utf8-bytes/.gitmodules new file mode 100644 index 0000000..d496220 --- /dev/null +++ b/node_modules/truncate-utf8-bytes/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/big-list-of-naughty-strings"] + path = vendor/big-list-of-naughty-strings + url = https://github.com/minimaxir/big-list-of-naughty-strings.git diff --git a/node_modules/truncate-utf8-bytes/.npmignore b/node_modules/truncate-utf8-bytes/.npmignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/node_modules/truncate-utf8-bytes/.npmignore @@ -0,0 +1 @@ +vendor/ diff --git a/node_modules/truncate-utf8-bytes/.travis.yml b/node_modules/truncate-utf8-bytes/.travis.yml new file mode 100644 index 0000000..1e25a8c --- /dev/null +++ b/node_modules/truncate-utf8-bytes/.travis.yml @@ -0,0 +1,12 @@ +language: node_js +node_js: + - "0.10" + - "0.12" + - "1" + - "2" + - "3" + - "4" + - "5" + - "node" +before_install: + - npm install -g npm diff --git a/node_modules/truncate-utf8-bytes/AUTHORS b/node_modules/truncate-utf8-bytes/AUTHORS new file mode 100644 index 0000000..6212900 --- /dev/null +++ b/node_modules/truncate-utf8-bytes/AUTHORS @@ -0,0 +1,2 @@ +Carl Xiong <[email protected]> +Parsha Pourkhomami <[email protected]> diff --git a/node_modules/truncate-utf8-bytes/README.md b/node_modules/truncate-utf8-bytes/README.md new file mode 100644 index 0000000..098819e --- /dev/null +++ b/node_modules/truncate-utf8-bytes/README.md @@ -0,0 +1,27 @@ +# truncate-utf8-bytes [](http://travis-ci.org/parshap/truncate-utf8-bytes) + +Truncate a string to the given length in bytes. Correctly handles +multi-byte characters and surrogate pairs. + +A browser implementation that doesn't use `Buffer.byteLength` is +provided to minimize build size. + +## Example + +```js +var truncate = require("truncate-utf8-bytes") +var str = "a☃" // a = 1 byte, ☃ = 3 bytes +console.log(truncate(str, 2)) +// -> "a" +``` + +## API + +### `var truncate = require("truncate-utf8-bytes")` + +*When using browserify or webpack*, this automatically resolves to an +implementation that does not use `Buffer.byteLength`. + +### `truncate(string, length)` + +Returns `string` truncated to at most `length` bytes in length. diff --git a/node_modules/truncate-utf8-bytes/browser.js b/node_modules/truncate-utf8-bytes/browser.js new file mode 100644 index 0000000..f0cfe50 --- /dev/null +++ b/node_modules/truncate-utf8-bytes/browser.js @@ -0,0 +1,5 @@ +'use strict'; + +var truncate = require("./lib/truncate"); +var getLength = require("utf8-byte-length/browser"); +module.exports = truncate.bind(null, getLength); diff --git a/node_modules/truncate-utf8-bytes/index.js b/node_modules/truncate-utf8-bytes/index.js new file mode 100644 index 0000000..39e899c --- /dev/null +++ b/node_modules/truncate-utf8-bytes/index.js @@ -0,0 +1,5 @@ +'use strict'; + +var truncate = require("./lib/truncate"); +var getLength = Buffer.byteLength.bind(Buffer); +module.exports = truncate.bind(null, getLength); diff --git a/node_modules/truncate-utf8-bytes/lib/truncate.js b/node_modules/truncate-utf8-bytes/lib/truncate.js new file mode 100644 index 0000000..3fed3b6 --- /dev/null +++ b/node_modules/truncate-utf8-bytes/lib/truncate.js @@ -0,0 +1,43 @@ +'use strict'; + +function isHighSurrogate(codePoint) { + return codePoint >= 0xd800 && codePoint <= 0xdbff; +} + +function isLowSurrogate(codePoint) { + return codePoint >= 0xdc00 && codePoint <= 0xdfff; +} + +// Truncate string by size in bytes +module.exports = function truncate(getLength, string, byteLength) { + if (typeof string !== "string") { + throw new Error("Input must be string"); + } + + var charLength = string.length; + var curByteLength = 0; + var codePoint; + var segment; + + for (var i = 0; i < charLength; i += 1) { + codePoint = string.charCodeAt(i); + segment = string[i]; + + if (isHighSurrogate(codePoint) && isLowSurrogate(string.charCodeAt(i + 1))) { + i += 1; + segment += string[i]; + } + + curByteLength += getLength(segment); + + if (curByteLength === byteLength) { + return string.slice(0, i + 1); + } + else if (curByteLength > byteLength) { + return string.slice(0, i - segment.length + 1); + } + } + + return string; +}; + diff --git a/node_modules/truncate-utf8-bytes/package.json b/node_modules/truncate-utf8-bytes/package.json new file mode 100644 index 0000000..b42e054 --- /dev/null +++ b/node_modules/truncate-utf8-bytes/package.json @@ -0,0 +1,68 @@ +{ + "_from": "truncate-utf8-bytes@^1.0.0", + "_id": "[email protected]", + "_inBundle": false, + "_integrity": "sha1-QFkjkJWS1W94pYGENLC3hInKXys=", + "_location": "/truncate-utf8-bytes", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "truncate-utf8-bytes@^1.0.0", + "name": "truncate-utf8-bytes", + "escapedName": "truncate-utf8-bytes", + "rawSpec": "^1.0.0", + "saveSpec": null, + "fetchSpec": "^1.0.0" + }, + "_requiredBy": [ + "/sanitize-filename" + ], + "_resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz", + "_shasum": "405923909592d56f78a5818434b0b78489ca5f2b", + "_spec": "truncate-utf8-bytes@^1.0.0", + "_where": "/Users/lucifer/Documents/styx/node_modules/sanitize-filename", + "author": { + "name": "Carl Xiong", + "email": "[email protected]" + }, + "browser": "browser.js", + "bugs": { + "url": "https://github.com/parshap/truncate-utf8-bytes/issues" + }, + "bundleDependencies": false, + "contributors": [ + { + "name": "Carl Xiong", + "email": "[email protected]" + }, + { + "name": "Parsha Pourkhomami", + "email": "[email protected]" + } + ], + "dependencies": { + "utf8-byte-length": "^1.0.1" + }, + "deprecated": false, + "description": "Truncate string to given length in bytes", + "devDependencies": { + "tape": "^4.2.2" + }, + "homepage": "https://github.com/parshap/truncate-utf8-bytes#readme", + "keywords": [ + "truncate", + "utf8" + ], + "license": "WTFPL", + "main": "index.js", + "name": "truncate-utf8-bytes", + "repository": { + "type": "git", + "url": "git+https://github.com/parshap/truncate-utf8-bytes.git" + }, + "scripts": { + "test": "tape test.js" + }, + "version": "1.0.2" +} 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(); + }); + }); +}); |
