Comparing version 0.0.7 to 0.1.0
{ | ||
"name": "bops", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"description": "buffer operations", | ||
@@ -43,7 +43,7 @@ "main": "index.js", | ||
"browsers": [ | ||
"ie/6..latest", | ||
"ie/10..latest", | ||
"chrome/20..latest", | ||
"firefox/10..latest", | ||
"firefox/15..latest", | ||
"safari/latest", | ||
"opera/11.0..latest", | ||
"opera/11.6..latest", | ||
"iphone/6", | ||
@@ -50,0 +50,0 @@ "ipad/6" |
# bops | ||
[![Build Status](https://travis-ci.org/chrisdickinson/tar-parse.png)](https://travis-ci.org/chrisdickinson/tar-parse) | ||
[![TESTLING](https://ci.testling.com/chrisdickinson/bops.png)](https://ci.testling.com/chrisdickinson/bops) | ||
[![NPM](https://nodei.co/npm/bops.png?downloads=true&stars=true)](https://nodei.co/npm/bops/) | ||
buffer/typed array agnostic buffer operations. | ||
@@ -4,0 +8,0 @@ |
@@ -16,6 +16,6 @@ var test = require('tape') | ||
test('from utf8 works as expected', function(assert) { | ||
var buf = binary.from('ƒello 淾淾淾 hello world 淾淾 yep ƒuu 淾', 'utf8') | ||
var buf = binary.from('ƒello 淾淾淾 hello world 淾淾 yep ƒuu 淾 \ud83d\ude04', 'utf8') | ||
, expect | ||
expect = [198,146,101,108,108,111,32,230,183,190,230,183,190,230,183,190,32,104,101,108,108,111,32,119,111,114,108,100,32,230,183,190,230,183,190,32,121,101,112,32,198,146,117,117,32,230,183,190] | ||
expect = [198,146,101,108,108,111,32,230,183,190,230,183,190,230,183,190,32,104,101,108,108,111,32,119,111,114,108,100,32,230,183,190,230,183,190,32,121,101,112,32,198,146,117,117,32,230,183,190,32,0xF0,0x9F,0x98,0x84] | ||
@@ -22,0 +22,0 @@ assert.equal(buf.length, expect.length) |
@@ -37,23 +37,99 @@ module.exports = from | ||
function from_utf(str) { | ||
var bytes = [] | ||
, tmp | ||
, ch | ||
var arr = [] | ||
, code | ||
for(var i = 0, len = str.length; i < len; ++i) { | ||
ch = str.charCodeAt(i) | ||
if(ch & 0x80) { | ||
tmp = encodeURIComponent(str.charAt(i)).substr(1).split('%') | ||
for(var j = 0, jlen = tmp.length; j < jlen; ++j) { | ||
bytes[bytes.length] = parseInt(tmp[j], 16) | ||
} | ||
} else { | ||
bytes[bytes.length] = ch | ||
code = fixed_cca(str, i) | ||
if(code === false) { | ||
continue | ||
} | ||
if(code < 0x80) { | ||
arr[arr.length] = code | ||
continue | ||
} | ||
codepoint_to_bytes(arr, code) | ||
} | ||
return new Uint8Array(bytes) | ||
return new Uint8Array(arr) | ||
} | ||
function codepoint_to_bytes(arr, code) { | ||
// find MSB, use that to determine byte count | ||
var copy_code = code | ||
, bit_count = 0 | ||
, byte_count | ||
, prefix | ||
, _byte | ||
, pos | ||
do { | ||
++bit_count | ||
} while(copy_code >>>= 1) | ||
byte_count = Math.ceil((bit_count - 1) / 5) | 0 | ||
prefix = [0, 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc][byte_count] | ||
pos = [0, 0, 3, 4, 5, 6, 7][byte_count] | ||
_byte |= prefix | ||
bit_count = (7 - pos) + 6 * (byte_count - 1) | ||
while(bit_count) { | ||
_byte |= +!!(code & (1 << bit_count)) << (7 - pos) | ||
++pos | ||
if(pos % 8 === 0) { | ||
arr[arr.length] = _byte | ||
_byte = 0x80 | ||
pos = 2 | ||
} | ||
--bit_count | ||
} | ||
if(pos) { | ||
arr[arr.length] = _byte | ||
} | ||
} | ||
function pad(str) { | ||
while(str.length < 8) { | ||
str = '0' + str | ||
} | ||
return str | ||
} | ||
function fixed_cca(str, idx) { | ||
idx = idx || 0 | ||
var code = str.charCodeAt(idx) | ||
, lo | ||
, hi | ||
if(0xD800 <= code && code <= 0xDBFF) { | ||
lo = str.charCodeAt(idx + 1) | ||
hi = code | ||
if(isNaN(lo)) { | ||
throw new Error('High surrogate not followed by low surrogate') | ||
} | ||
return ((hi - 0xD800) * 0x400) + (lo - 0xDC00) + 0x10000 | ||
} | ||
if(0xDC00 <= code && code <= 0xDFFF) { | ||
return false | ||
} | ||
return code | ||
} | ||
function from_base64(str) { | ||
return new Uint8Array(base64.toByteArray(str)) | ||
} |
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
38778
34
1137
94