fastestsmallesttextencoderdecoder
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -16,3 +16,41 @@ (function(global){ | ||
if (usingTypedArrays || NativeBuffer) { | ||
function decoderReplacer(nonAsciiChars){ | ||
function decoderReplacer(encoded){ | ||
var codePoint = encoded.charCodeAt(0) << 24; | ||
var leadingOnes = clz32(~codePoint)|0; | ||
var endPos = 0, stringLen = encoded.length|0; | ||
var result = ""; | ||
if (leadingOnes < 5 && stringLen >= leadingOnes) { | ||
codePoint = (codePoint<<leadingOnes)>>>(24+leadingOnes); | ||
for (endPos = 1; endPos < leadingOnes; endPos=endPos+1|0) | ||
codePoint = (codePoint<<6) | (encoded.charCodeAt(endPos)&0x3f/*0b00111111*/); | ||
if (codePoint <= 0xFFFF) { // BMP code point | ||
result += fromCharCode(codePoint); | ||
} else if (codePoint <= 0x10FFFF) { | ||
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae | ||
codePoint = codePoint - 0x10000|0; | ||
result += fromCharCode( | ||
(codePoint >> 10) + 0xD800|0, // highSurrogate | ||
(codePoint & 0x3ff) + 0xDC00|0 // lowSurrogate | ||
); | ||
} else endPos = 0; // to fill it in with INVALIDs | ||
} | ||
for (; endPos < stringLen; endPos=endPos+1|0) result += "\ufffd"; // replacement character | ||
return result; | ||
} | ||
function TextDecoder(){}; | ||
function decode(inputArrayOrBuffer){ | ||
var buffer = (inputArrayOrBuffer && inputArrayOrBuffer.buffer) || inputArrayOrBuffer; | ||
var asString = Object_prototype_toString.call(buffer); | ||
if (asString !== ArrayBufferString && asString !== NativeBufferString) | ||
throw Error("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
var inputAs8 = usingTypedArrays ? new NativeUint8Array(buffer) : buffer; | ||
var resultingString = ""; | ||
for (var index=0,len=inputAs8.length|0; index<len; index=index+32768|0) | ||
resultingString += fromCharCode.apply(0, inputAs8[usingTypedArrays ? "subarray" : "slice"](index,index+32768|0)); | ||
return resultingString.replace(/[\xc0-\xff][\x80-\xbf]*/g, decoderReplacer); | ||
} | ||
TextDecoder.prototype.decode = decode; | ||
////////////////////////////////////////////////////////////////////////////////////// | ||
function encoderReplacer(nonAsciiChars){ | ||
// make the UTF string into a binary UTF-8 encoded string | ||
@@ -45,40 +83,2 @@ var point = nonAsciiChars.charCodeAt(0)|0; | ||
} | ||
function TextDecoder(){}; | ||
function decode(inputArrayOrBuffer){ | ||
var buffer = (inputArrayOrBuffer && inputArrayOrBuffer.buffer) || inputArrayOrBuffer; | ||
var asString = Object_prototype_toString.call(buffer); | ||
if (asString !== ArrayBufferString && asString !== NativeBufferString) | ||
throw Error("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
var inputAs8 = usingTypedArrays ? new NativeUint8Array(buffer) : buffer; | ||
var resultingString = ""; | ||
for (var index=0,len=inputAs8.length|0; index<len; index=index+32768|0) | ||
resultingString += fromCharCode.apply(0, inputAs8[usingTypedArrays ? "subarray" : "slice"](index,index+32768|0)); | ||
return resultingString.replace(/[\x80-\uD7ff\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g, decoderReplacer); | ||
} | ||
TextDecoder.prototype.decode = decode; | ||
////////////////////////////////////////////////////////////////////////////////////// | ||
function encoderReplacer(encoded){ | ||
var codePoint = encoded.charCodeAt(0) << 24; | ||
var leadingOnes = clz32(~codePoint)|0; | ||
var endPos = 0, stringLen = encoded.length|0; | ||
var result = ""; | ||
if (leadingOnes < 5 && stringLen >= leadingOnes) { | ||
codePoint = (codePoint<<leadingOnes)>>>(24+leadingOnes); | ||
for (endPos = 1; endPos < leadingOnes; endPos=endPos+1|0) | ||
codePoint = (codePoint<<6) | (encoded.charCodeAt(endPos)&0x3f/*0b00111111*/); | ||
if (codePoint <= 0xFFFF) { // BMP code point | ||
result += fromCharCode(codePoint); | ||
} else if (codePoint <= 0x10FFFF) { | ||
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae | ||
codePoint = codePoint - 0x10000|0; | ||
result += fromCharCode( | ||
(codePoint >> 10) + 0xD800|0, // highSurrogate | ||
(codePoint & 0x3ff) + 0xDC00|0 // lowSurrogate | ||
); | ||
} else endPos = 0; // to fill it in with INVALIDs | ||
} | ||
for (; endPos < stringLen; endPos=endPos+1|0) result += "\ufffd"; // replacement character | ||
return result; | ||
} | ||
function TextEncoder(){}; | ||
@@ -88,3 +88,3 @@ function encode(inputString){ | ||
// 0x80 => 0b10000000; 0xbf => 0b10111111; 0x80-0xbf => 0b10xxxxxx | ||
var encodedString = inputString === void 0 ? "" : ("" + inputString).replace(/[\xc0-\xff][\x80-\xbf]*/g, encoderReplacer); | ||
var encodedString = inputString === void 0 ? "" : ("" + inputString).replace(/[\x80-\uD7ff\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g, encoderReplacer); | ||
var len=encodedString.length|0, result = new (usingTypedArrays ? NativeUint8Array : NativeBuffer)(len); | ||
@@ -103,6 +103,6 @@ for (var i=0; i<len; i=i+1|0) | ||
typeof define === typeof factory && define["amd"] ? define(function(){ | ||
typeof define == typeof factory && define["amd"] ? define(function(){ | ||
return factory({}); | ||
}) : factory(typeof exports === 'object' ? exports : global); | ||
}) : factory(typeof exports == 'object' ? exports : global); | ||
} | ||
})(typeof global == "" + void 0 ? typeof self == "" + void 0 ? this : self : global); |
{ | ||
"name": "fastestsmallesttextencoderdecoder", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "The fastest smallest Javascript polyfill for the encode of TextEncoder and decode of TextDecoder for UTF-8 only. Made by AnonyCo with ❤️ from 🐕s.", | ||
@@ -5,0 +5,0 @@ "keywords": ["utf-8","encoding","decoding","encoder","decoder","javascript","polyfill","js","utf8","compact","cross-browser","utf8-string","pure-javascript","nodejs","node-js","node-module","nodejs-modules","performance","tiny","small"], |
@@ -6,3 +6,3 @@ | ||
[![GitHub file size in bytes](https://img.shields.io/github/size/anonyco/FastestSmallestTextEncoderDecoder/EncoderDecoderTogether.min.js.svg?label=without%20gzip)](https://github.com/anonyco/FastestSmallestTextEncoderDecoder/blob/master/EncoderDecoderTogether.min.js "File without gzip") | ||
[![GitHub file size in bytes](https://img.shields.io/github/size/anonyco/FastestSmallestTextEncoderDecoder/EncoderDecoderTogether.min.js.gz?label=gzip%20applied)](https://github.com/anonyco/FastestSmallestTextEncoderDecoder/blob/master/EncoderDecoderTogether.min.js.gz "Gzipped file") | ||
[![GitHub file size in bytes](https://img.shields.io/github/size/anonyco/FastestSmallestTextEncoderDecoder/gh-pages/EncoderDecoderTogether.min.js.gz?label=gzip%20applied)](https://github.com/anonyco/FastestSmallestTextEncoderDecoder/blob/master/gh-pages/EncoderDecoderTogether.min.js.gz "Gzipped file") | ||
[![npm bundle size (version)](https://img.shields.io/bundlephobia/min/fastestsmallesttextencoderdecoder/latest.svg?color=maroon&label=NPM%20bundle%20size)](https://npmjs.org/package/fastestsmallesttextencoderdecoder "View this project on npm") | ||
@@ -9,0 +9,0 @@ [![Issues](http://img.shields.io/github/issues/anonyco/FastestSmallestTextEncoderDecoder.svg)]( https://github.com/anonyco/FastestSmallestTextEncoderDecoder/issues ) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13133