utf8-buffer
Advanced tools
Comparing version 0.2.0 to 1.0.0
@@ -1,1 +0,1 @@ | ||
Rafael da Silva Rocha <rocha.rafaelsilva@gmail.com> | ||
Rafael da Silva Rocha <rocha.rafaelsilva@gmail.com> |
# CHANGELOG | ||
## 1.0.0 - 2019-12-31 | ||
- New package structure: | ||
* dist file is "./dist/utf8-buffer.js", a UMD served as "main" | ||
* ES6 source is "./index.js", served as "module" | ||
## 0.2.0 (2018-08-05) | ||
@@ -14,2 +19,2 @@ - unpack(buffer, start, end) API change: end now is non-inclusive | ||
- *end* is the buffer index to end reading. | ||
- works in IE8+. | ||
- works in IE8+. |
@@ -1,150 +0,7 @@ | ||
/* | ||
* Copyright (c) 2018 Rafael da Silva Rocha. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
/** | ||
* @fileoverview Functions to serialize and deserialize UTF-8 strings. | ||
* @see https://github.com/rochars/utf8-buffer | ||
* @see https://encoding.spec.whatwg.org/#the-encoding | ||
* @see https://encoding.spec.whatwg.org/#utf-8-encoder | ||
*/ | ||
/** @module utf8-buffer */ | ||
/** | ||
* Read a string of UTF-8 characters from a byte buffer. | ||
* Invalid characters are replaced with 'REPLACEMENT CHARACTER' (U+FFFD). | ||
* @see https://encoding.spec.whatwg.org/#the-encoding | ||
* @see https://stackoverflow.com/a/34926911 | ||
* @param {!Uint8Array|!Array<number>} buffer A byte buffer. | ||
* @param {number=} start The buffer index to start reading. | ||
* @param {?number=} end The buffer index to stop reading. | ||
* If end is null will read until the end of the buffer. | ||
* @return {string} | ||
*/ | ||
function unpack(buffer, start=0, end=buffer.length) { | ||
/** @type {string} */ | ||
let str = ""; | ||
for(let index = start; index < end;) { | ||
/** @type {number} */ | ||
let lowerBoundary = 0x80; | ||
/** @type {number} */ | ||
let upperBoundary = 0xBF; | ||
/** @type {boolean} */ | ||
let replace = false; | ||
/** @type {number} */ | ||
let charCode = buffer[index++]; | ||
if (charCode >= 0x00 && charCode <= 0x7F) { | ||
str += String.fromCharCode(charCode); | ||
} else { | ||
/** @type {number} */ | ||
let count = 0; | ||
if (charCode >= 0xC2 && charCode <= 0xDF) { | ||
count = 1; | ||
} else if (charCode >= 0xE0 && charCode <= 0xEF ) { | ||
count = 2; | ||
if (buffer[index] === 0xE0) { | ||
lowerBoundary = 0xA0; | ||
} | ||
if (buffer[index] === 0xED) { | ||
upperBoundary = 0x9F; | ||
} | ||
} else if (charCode >= 0xF0 && charCode <= 0xF4 ) { | ||
count = 3; | ||
if (buffer[index] === 0xF0) { | ||
lowerBoundary = 0x90; | ||
} | ||
if (buffer[index] === 0xF4) { | ||
upperBoundary = 0x8F; | ||
} | ||
} else { | ||
replace = true; | ||
} | ||
charCode = charCode & (1 << (8 - count - 1)) - 1; | ||
for (let i = 0; i < count; i++) { | ||
if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) { | ||
replace = true; | ||
} | ||
charCode = (charCode << 6) | (buffer[index] & 0x3f); | ||
index++; | ||
} | ||
if (replace) { | ||
str += String.fromCharCode(0xFFFD); | ||
} | ||
else if (charCode <= 0xffff) { | ||
str += String.fromCharCode(charCode); | ||
} else { | ||
charCode -= 0x10000; | ||
str += String.fromCharCode( | ||
((charCode >> 10) & 0x3ff) + 0xd800, | ||
(charCode & 0x3ff) + 0xdc00); | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
/** | ||
* Write a string of UTF-8 characters to a byte buffer. | ||
* @see https://encoding.spec.whatwg.org/#utf-8-encoder | ||
* @param {string} str The string to pack. | ||
* @param {!Uint8Array|!Array<number>} buffer The buffer to pack the string to. | ||
* @param {number=} index The buffer index to start writing. | ||
* @return {number} The next index to write in the buffer. | ||
*/ | ||
function pack(str, buffer, index=0) { | ||
for (let i = 0, len = str.length; i < len; i++) { | ||
/** @type {number} */ | ||
let codePoint = str.codePointAt(i); | ||
if (codePoint < 128) { | ||
buffer[index] = codePoint; | ||
index++; | ||
} else { | ||
/** @type {number} */ | ||
let count = 0; | ||
/** @type {number} */ | ||
let offset = 0; | ||
if (codePoint <= 0x07FF) { | ||
count = 1; | ||
offset = 0xC0; | ||
} else if(codePoint <= 0xFFFF) { | ||
count = 2; | ||
offset = 0xE0; | ||
} else if(codePoint <= 0x10FFFF) { | ||
count = 3; | ||
offset = 0xF0; | ||
i++; | ||
} | ||
buffer[index] = (codePoint >> (6 * count)) + offset; | ||
index++; | ||
while (count > 0) { | ||
buffer[index] = 0x80 | (codePoint >> (6 * (count - 1)) & 0x3F); | ||
index++; | ||
count--; | ||
} | ||
} | ||
} | ||
return index; | ||
} | ||
export { unpack, pack }; | ||
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.checkStringArgs=function(a,b,c){if(null==a)throw new TypeError("The 'this' value for String.prototype."+c+" must not be null or undefined");if(b instanceof RegExp)throw new TypeError("First argument to String.prototype."+c+" must not be a regular expression");return a+""};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1; | ||
$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,c){a!=Array.prototype&&a!=Object.prototype&&(a[b]=c.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this); | ||
$jscomp.polyfill=function(a,b,c,d){if(b){c=$jscomp.global;a=a.split(".");for(d=0;d<a.length-1;d++){var h=a[d];h in c||(c[h]={});c=c[h]}a=a[a.length-1];d=c[a];b=b(d);b!=d&&null!=b&&$jscomp.defineProperty(c,a,{configurable:!0,writable:!0,value:b})}}; | ||
$jscomp.polyfill("String.prototype.codePointAt",function(a){return a?a:function(b){var c=$jscomp.checkStringArgs(this,null,"codePointAt"),a=c.length;b=Number(b)||0;if(0<=b&&b<a){b|=0;var h=c.charCodeAt(b);if(55296>h||56319<h||b+1===a)return h;b=c.charCodeAt(b+1);return 56320>b||57343<b?h:1024*(h-55296)+b+9216}}},"es6","es3"); | ||
(function(a,b){"object"===typeof exports&&"undefined"!==typeof module?b(exports):"function"===typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.UTF8Buffer={}))})(this,function(a){a.pack=function(b,a,d){d=void 0===d?0:d;for(var c=0,k=b.length;c<k;c++){var f=b.codePointAt(c);if(128>f)a[d]=f,d++;else{var g=0,e=0;2047>=f?(g=1,e=192):65535>=f?(g=2,e=224):1114111>=f&&(g=3,e=240,c++);a[d]=(f>>6*g)+e;for(d++;0<g;)a[d]=128|f>>6*(g-1)&63,d++,g--}}return d};a.unpack=function(b,a,d){d=void 0===d? | ||
b.length:d;var c="";for(a=void 0===a?0:a;a<d;){var k=128,f=191,g=!1,e=b[a++];if(0<=e&&127>=e)c+=String.fromCharCode(e);else{var l=0;194<=e&&223>=e?l=1:224<=e&&239>=e?(l=2,224===b[a]&&(k=160),237===b[a]&&(f=159)):240<=e&&244>=e?(l=3,240===b[a]&&(k=144),244===b[a]&&(f=143)):g=!0;e&=(1<<8-l-1)-1;for(var m=0;m<l;m++){if(b[a]<k||b[a]>f)g=!0;e=e<<6|b[a]&63;a++}g?c+=String.fromCharCode(65533):65535>=e?c+=String.fromCharCode(e):(e-=65536,c+=String.fromCharCode((e>>10&1023)+55296,(e&1023)+56320))}}return c}; | ||
Object.defineProperty(a,"__esModule",{value:!0})}); |
/* | ||
* Copyright (c) 2018 Rafael da Silva Rocha. | ||
* Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
* | ||
@@ -26,3 +26,3 @@ * Permission is hereby granted, free of charge, to any person obtaining | ||
/** | ||
* @fileoverview Externs for utf8-buffer 0.1.0 | ||
* @fileoverview Externs for utf8-buffer 1.0.0 | ||
* @see https://github.com/rochars/utf8-buffer | ||
@@ -29,0 +29,0 @@ * @externs |
{ | ||
"name": "utf8-buffer", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "ES6 module to encode and decode UTF-8 strings.", | ||
@@ -8,5 +8,5 @@ "homepage": "https://github.com/rochars/utf8-buffer", | ||
"license": "MIT", | ||
"module": "./main.js", | ||
"main": "./dist/utf8-buffer.umd.js", | ||
"es2015": "./dist/utf8-buffer.js", | ||
"main": "./dist/utf8-buffer.js", | ||
"module": "./index.js", | ||
"types": "./index.d.ts", | ||
"engines": { | ||
@@ -41,3 +41,4 @@ "node": ">=8" | ||
"externs", | ||
"main.js", | ||
"index.js", | ||
"index.d.ts", | ||
"LICENSE", | ||
@@ -49,10 +50,9 @@ "AUTHORS.md", | ||
"scripts": { | ||
"lint": "jshint main.js externs", | ||
"lint": "jshint index.js externs", | ||
"test": "nyc ./node_modules/mocha/bin/_mocha test/src --recursive -R dot", | ||
"test-umd": "node ./node_modules/mocha/bin/_mocha test/src --umd --recursive -R dot", | ||
"test-amd": "node ./node_modules/mocha/bin/_mocha test/src --amd --recursive -R dot", | ||
"test-esm": "node ./node_modules/mocha/bin/_mocha test/src --esm --recursive -R dot", | ||
"test-dist": "npm run test-umd && npm run test-esm", | ||
"test-tsc": "tsc ./test/dist/TypeScript/main.ts && node -r esm ./test/dist/TypeScript/main.js", | ||
"test-dist": "npm run test-umd && npm run test-tsc", | ||
"pack": "npm run test && rollup -c && npm run test-dist", | ||
"doc": "./node_modules/.bin/jsdoc main.js -d docs -r README.md -t node_modules/docdash", | ||
"doc": "./node_modules/.bin/jsdoc -c .jsdocrc index.js -d docs -r README.md -t node_modules/docdash", | ||
"build": "npm run lint && npm run pack && npm run doc", | ||
@@ -62,14 +62,15 @@ "coverage": "nyc report --reporter=lcov > coverage.lcov && codecov" | ||
"devDependencies": { | ||
"codecov": "^3.0.2", | ||
"docdash": "^0.4.0", | ||
"esm": "^3.0.51", | ||
"google-closure-compiler": "^20180610.0.2", | ||
"jsdoc": "^3.5.5", | ||
"jshint": "^2.9.5", | ||
"mocha": "^5.2.0", | ||
"@ampproject/rollup-plugin-closure-compiler": "^0.13.0", | ||
"codecov": "^3.6.1", | ||
"docdash": "^1.1.1", | ||
"esm": "^3.2.25", | ||
"jsdoc": "^3.6.3", | ||
"jshint": "^2.10.3", | ||
"mocha": "^6.2.2", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"nyc": "^12.0.2", | ||
"rollup": "^0.61.2", | ||
"rollup-plugin-closure-compiler-js": "^1.0.6" | ||
} | ||
"nyc": "^14.1.1", | ||
"rollup": "^1.27.14", | ||
"typescript": "^3.7.4" | ||
}, | ||
"dependencies": {} | ||
} |
# utf8-buffer | ||
ES6 module to encode and decode UTF-8 strings. | ||
Copyright (c) 2018 Rafael da Silva Rocha. | ||
Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
https://github.com/rochars/utf8-buffer | ||
@@ -11,7 +11,2 @@ | ||
- **MIT licensed** | ||
- **Use it out of the box in the browser** | ||
- **Use it out of the box in Node.js** | ||
- **Less than 2KB minified!** | ||
## Install | ||
@@ -24,22 +19,6 @@ ``` | ||
### Node | ||
If you installed via [NPM](https://www.npmjs.com/) or [Yarn](https://yarnpkg.com), **import utf8Buffer from utf8-buffer**: | ||
```javascript | ||
import * as utf8Buffer from 'utf8-buffer'; | ||
``` | ||
Or **import** just what you need: | ||
```javascript | ||
import {pack, unpack} from 'utf8-buffer'; | ||
``` | ||
Or **require**: | ||
```javascript | ||
const utf8Buffer = require('utf8-buffer'); | ||
``` | ||
### Browser | ||
Use **utf8-buffer.umd.js** in the */dist* folder of this package: | ||
Use the **utf8-buffer.js** file in the */dist* folder: | ||
```html | ||
<script src="./dist/utf8-buffer.umd.js"></script> | ||
<script src="./dist/utf8-buffer.js"></script> | ||
``` | ||
@@ -57,8 +36,15 @@ | ||
### Node | ||
```javascript | ||
const utf8Buffer = require('utf8-buffer'); | ||
``` | ||
Or **import** just what you need: | ||
```javascript | ||
import {pack, unpack} from 'utf8-buffer'; | ||
``` | ||
## About | ||
Only UTF-8 strings with a max of 4 bytes per character are supported. **BOM** is kept untouched. Invalid characters are replaced with *Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD)*. | ||
### Browser compatibility | ||
IE8+. Should work in all modern browsers. Cross-browser tests are on the [ROADMAP](https://github.com/rochars/utf8-buffer/blob/master/ROADMAP.md). | ||
## API | ||
@@ -103,3 +89,3 @@ ```javascript | ||
## LICENSE | ||
Copyright (c) 2018 Rafael da Silva Rocha. | ||
Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
@@ -106,0 +92,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
19961
236
106
1