if (!start) start = 0
if (!end) end = this.length
- if (typeof value === 'string') {
- value = value.charCodeAt(0)
- }
-
- assert(typeof value === 'number' && !isNaN(value), 'value is not a number')
assert(end >= start, 'end < start')
// Fill 0 bytes; we're done
assert(start >= 0 && start < this.length, 'start out of bounds')
assert(end >= 0 && end <= this.length, 'end out of bounds')
- for (var i = start; i < end; i++) {
- this[i] = value
+ var i
+ if (typeof value === 'number') {
+ for (i = start; i < end; i++) {
+ this[i] = value
+ }
+ } else {
+ var bytes = utf8ToBytes(value.toString())
+ var len = bytes.length
+ for (i = start; i < end; i++) {
+ this[i] = bytes[i % len]
+ }
}
return this
var byteArray = []
for (var i = 0; i < str.length; i++) {
var b = str.charCodeAt(i)
- if (b <= 0x7F)
- byteArray.push(str.charCodeAt(i))
- else {
+ if (b <= 0x7F) {
+ byteArray.push(b)
+ } else {
var start = i
if (b >= 0xD800 && b <= 0xDFFF) i++
var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%')
})
test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
- t.plan(2*(2*2*2+2))
+ t.plan(2 * (2 * 2 * 2 + 2))
var hex = [
'03', '0300', '0003', '03000000', '00000003',
'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
})
test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
- t.plan(3*(2*2*2+2))
+ t.plan(3 * (2 * 2 * 2 + 2))
var hex = [
'', '03', '00', '030000', '000000',
'', 'fd', 'ff', 'fdffff', 'ffffff'
var zero = []
var one = [ new B('asdf') ]
var long = []
- for (var i = 0; i < 10; i++) long.push(new B('asdf'))
+ for (var i = 0; i < 10; i++) {
+ long.push(new B('asdf'))
+ }
var flatZero = B.concat(zero)
var flatOne = B.concat(one)
t.end()
})
-test('fill', function(t) {
+test('fill', function (t) {
var b = new B(10)
b.fill(2)
t.equal(b.toString('hex'), '02020202020202020202')
t.end()
})
+test('fill (string)', function (t) {
+ var b = new B(10)
+ b.fill('abc')
+ t.equal(b.toString(), 'abcabcabca')
+ b.fill('է')
+ console.log(b[0], b[1], b[2], b[3], b[4])
+ t.equal(b.toString(), 'էէէէէ')
+ t.end()
+})
+
test('copy() empty buffer with sourceEnd=0', function (t) {
var source = new B([42])
var destination = new B([43])
t.end()
})
-test('copy() after slice()', function(t) {
+test('copy() after slice()', function (t) {
var source = new B(200)
var dest = new B(200)
var expected = new B(200)
t.end()
})
-test('base64 ignore whitespace', function(t) {
+test('base64 ignore whitespace', function (t) {
var text = '\n YW9ldQ== '
var buf = new B(text, 'base64')
t.equal(buf.toString(), 'aoeu')