qb-utf8-from-str-tiny
v1.2.2
Published
tiny browser script for converting string to array of UTF-8 characters. not efficient for large files.
Downloads
293
Maintainers
Readme
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 qb-standard .
Install
npm install qb-utf8-from-str-tiny
Example
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 ]
API
utf8-from-str (s)
- s: a javascript string to convert to an array of integer bytes (integers in the range 0..255) in UTF-8 format