qb-utf8-from-str-tiny
Version:
tiny browser script for converting string to array of UTF-8 characters. not efficient for large files.
62 lines (43 loc) • 2.22 kB
Markdown
//img.shields.io/npm/v/qb-utf8-from-str-tiny.svg
[ ]: https://img.shields.io/npm/dm/qb-utf8-from-str-tiny.svg
[ ]: https://npmjs.org/package/qb-utf8-from-str-tiny
[ ]: https://www.bithound.io/github/quicbit-js/qb-utf8-from-str-tiny/badges/dependencies.svg
[ ]: https://www.bithound.io/github/quicbit-js/qb-utf8-from-str-tiny/master/dependencies/npm
[ ]: https://www.bithound.io/github/quicbit-js/qb-utf8-from-str-tiny/badges/devDependencies.svg
[ ]: https://www.bithound.io/github/quicbit-js/qb-utf8-from-str-tiny/master/dependencies/npm
[ ]: https://www.bithound.io/github/quicbit-js/qb-utf8-from-str-tiny/badges/code.svg
[ ]: https://www.bithound.io/github/quicbit-js/qb-utf8-from-str-tiny
Tiny script for converting string to array of utf-8 based upon encodeURIComponent().
Runs in nodejs and in browser. Not efficient for large strings.
module.exports = function (s) {
for (var i = 0, enc = encodeURIComponent(s), a = []; i < enc.length;) {
if (enc[i] === '%') {
a.push(parseInt(enc.substr(i + 1, 2), 16))
i += 3
} else {
a.push(enc.charCodeAt(i++))
}
}
return a
}
That's it. That's the code.
**Complies with the 100% test coverage and minimum dependency requirements** of
[ ](http://github.com/quicbit-js/qb-standard) .
npm install qb-utf8-from-str-tiny
npm install qb-utf8-from-str-tiny
node
> var str = require('qb-utf8-from-str-tiny')
> str('gîddñup𐂃!')
[ 103, 195, 174, 100, 100, 195, 177, 117, 112, 240, 144, 130, 131, 33 ]
* [s](https://github.com/quicbit-js/qb-standard/blob/master/doc/variable-glossary.md#s-string): a
javascript string to convert to an array of integer bytes (integers in the range 0..255) in UTF-8 format
[![npm][npm-image]][npm-url]
[![downloads][downloads-image]][npm-url]
[![bitHound Dependencies][proddep-image]][proddep-link]
[![dev dependencies][devdep-image]][devdep-link]
[![code analysis][code-image]][code-link]
[ ]: https: