Comparing version 0.2.3 to 0.3.0
@@ -1,2 +0,2 @@ | ||
/* globals atob, btoa */ | ||
/* globals atob, btoa, crypto */ | ||
/* istanbul ignore file */ | ||
@@ -48,2 +48,9 @@ 'use strict' | ||
bytes.random = length => { | ||
const ab = new ArrayBuffer(length) | ||
const view = new Uint8Array(ab) | ||
crypto.getRandomValues(view) | ||
return ab | ||
} | ||
module.exports = bytes |
18
core.js
@@ -48,3 +48,21 @@ 'use strict' | ||
} | ||
bytes.typedArray = (_from, _Class = Uint8Array) => { | ||
_from = bytes(_from) | ||
return new _Class(_from.buffer, _from.byteOffset, _from.byteLength / _Class.BYTES_PER_ELEMENT) | ||
} | ||
bytes.concat = (_from) => { | ||
_from = Array.from(_from) | ||
_from = _from.map(b => bytes(b)) | ||
const length = _from.reduce((x, y) => x + y.byteLength, 0) | ||
const ret = new Uint8Array(length) | ||
let i = 0 | ||
for (const part of _from) { | ||
const view = bytes.typedArray(part) | ||
ret.set(view, i) | ||
i += view.byteLength | ||
} | ||
return ret.buffer | ||
} | ||
module.exports = bytes |
'use strict' | ||
const crypto = require('crypto') | ||
const fallback = require('./browser').from | ||
@@ -27,2 +28,4 @@ const bytes = require('./core') | ||
bytes.random = length => crypto.randomBytes(length).buffer | ||
module.exports = bytes |
{ | ||
"name": "bytesish", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"main": "node.js", | ||
@@ -5,0 +5,0 @@ "browser": "browser.js", |
@@ -76,2 +76,4 @@ # `bytesish` | ||
### `bytes.typedArray(from[, Class=Uint8Array])` | ||
## Optimized (memcopy only when necessary) | ||
@@ -83,4 +85,2 @@ | ||
All binary API's that **must** do a memcopy are prefaced with `"memcopy"`. | ||
All memcopy APIs return an `ArrayBuffer` | ||
@@ -96,2 +96,8 @@ | ||
### `bytes.concat(values)` | ||
`values` is an iterable of binary or string types. | ||
Returns a newly allocated `ArrayBuffer` contained the concatenated binary data. | ||
## String Conversions | ||
@@ -98,0 +104,0 @@ |
@@ -38,2 +38,11 @@ 'use strict' | ||
test('typed array', done => { | ||
const ab = bytes.random(4) | ||
const view = bytes(ab) | ||
const uint = bytes.typedArray(ab, Uint32Array) | ||
assert(uint instanceof Uint32Array) | ||
assert(bytes.compare(uint, view)) | ||
done() | ||
}) | ||
test('from array buffer', done => { | ||
@@ -94,1 +103,12 @@ const a = bytes('hello world') | ||
}) | ||
test('concat', done => { | ||
let values = [bytes('1'), bytes.native('2'), bytes.arrayBuffer('3')] | ||
let ab = bytes.concat(values) | ||
assert(ab instanceof ArrayBuffer) | ||
assert(bytes.compare(ab, '123')) | ||
values = [bytes('one')] | ||
ab = bytes.concat(values) | ||
assert(bytes.compare(ab, 'one')) | ||
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
12384
234
105