Comparing version 0.1.0 to 0.2.0
@@ -42,8 +42,8 @@ /* globals atob, btoa */ | ||
bytes.native = arg => { | ||
if (arg instanceof Uint8Array) return arg | ||
arg = bytes.from(arg) | ||
return new Uint8Array(arg.buffer, arg.byteOffset, arg.byteLength) | ||
bytes.native = (_from, encoding) => { | ||
if (_from instanceof Uint8Array) return _from | ||
_from = bytes.from(_from, encoding) | ||
return new Uint8Array(_from.buffer, _from.byteOffset, _from.byteLength) | ||
} | ||
module.exports = bytes |
20
core.js
@@ -11,3 +11,3 @@ 'use strict' | ||
bytes.sort = (a, b) => { | ||
bytes.sorter = (a, b) => { | ||
a = bytes(a) | ||
@@ -28,3 +28,3 @@ b = bytes(b) | ||
bytes.compare = (a, b) => !bytes.sort(a, b) | ||
bytes.compare = (a, b) => !bytes.sorter(a, b) | ||
bytes.memcopy = (_from, encoding) => { | ||
@@ -34,3 +34,19 @@ const b = bytes(_from, encoding) | ||
} | ||
bytes.arrayBuffer = (_from, encoding) => { | ||
_from = bytes(_from, encoding) | ||
if (_from.buffer.byteLength === _from.byteLength) return _from.buffer | ||
return _from.buffer.slice(_from.byteOffset, _from.byteOffset + _from.byteLength) | ||
} | ||
const sliceOptions = (_from, start = 0, end = null) => { | ||
_from = bytes(_from) | ||
end = (end === null ? _from.byteLength : end) - start | ||
return [_from.buffer, _from.byteOffset + start, end] | ||
} | ||
bytes.slice = (_from, start, end) => new DataView(...sliceOptions(_from, start, end)) | ||
bytes.memcopySlice = (_from, start, end) => { | ||
const [buffer, offset, length] = sliceOptions(_from, start, end) | ||
return buffer.slice(offset, length + offset) | ||
} | ||
module.exports = bytes |
@@ -21,8 +21,8 @@ 'use strict' | ||
bytes.native = arg => { | ||
if (Buffer.isBuffer(arg)) return arg | ||
arg = bytes(arg) | ||
return Buffer.from(arg.buffer, arg.byteOffset, arg.byteLength) | ||
bytes.native = (_from, encoding) => { | ||
if (Buffer.isBuffer(_from)) return _from | ||
_from = bytes(_from, encoding) | ||
return Buffer.from(_from.buffer, _from.byteOffset, _from.byteLength) | ||
} | ||
module.exports = bytes |
{ | ||
"name": "bytesish", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "node.js", | ||
@@ -5,0 +5,0 @@ "browser": "browser.js", |
@@ -60,1 +60,22 @@ # `bytesish` | ||
# API | ||
## Zero Copy | ||
### `bytes(from[, encoding])` | ||
### `bytes.sort(a, b)` | ||
### `bytes.compare(a, b)` | ||
### `bytes.native(from[, encoding]) | ||
## Optimized (memcopy only when necessary) | ||
### `bytes.arrayBuffer(_from[, encoding])` | ||
## Memory Copy | ||
All binary API's that **must** do a memcopy are prefaced with `"memcopy"`. | ||
## String Conversions |
@@ -38,3 +38,3 @@ 'use strict' | ||
test('array buffer', done => { | ||
test('from array buffer', done => { | ||
const a = bytes('hello world') | ||
@@ -46,2 +46,11 @@ const b = bytes(bytes.memcopy(a)) | ||
test('to array buffer', done => { | ||
const a = bytes.arrayBuffer('hello world') | ||
const b = bytes('hello world') | ||
assert(a instanceof ArrayBuffer) | ||
assert(bytes.compare(a, b)) | ||
assert(a, bytes.arrayBuffer(a)) | ||
done() | ||
}) | ||
test('Uint8Array', done => { | ||
@@ -67,1 +76,20 @@ const a = bytes('hello world') | ||
}) | ||
test('slice', done => { | ||
const a = bytes.slice('hello world', 2, 7) | ||
assert(a instanceof DataView) | ||
const b = bytes.arrayBuffer('hello world').slice(2, 7) | ||
same(b, bytes.arrayBuffer(a)) | ||
done() | ||
}) | ||
test('slice memcopy', done => { | ||
const a = bytes.memcopySlice('hello world', 2, 7) | ||
assert(a instanceof ArrayBuffer) | ||
const b = bytes.arrayBuffer('hello world').slice(2, 7) | ||
same(b, a) | ||
const c = bytes.memcopySlice(a) | ||
assert(a !== c) | ||
same(a, c) | ||
done() | ||
}) |
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
10471
191
81