Skip to content

Commit

Permalink
Support Array and Uint8Array in "bytes" (as used by setcode) #6
Browse files Browse the repository at this point in the history
  • Loading branch information
james committed Sep 23, 2018
1 parent 58fb50b commit 259e50c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 139 deletions.
166 changes: 33 additions & 133 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ const fromBuffer = (types, structs) => (typeName, buf) => {
return Fcbuffer.fromBuffer(type, buf)
}

const toBuffer = (types, structs) => (typeName, object) => {
const toBuffer = (types, structs) => (typeName, value) => {
assert.equal(typeof typeName, 'string', 'typeName (type or struct name)')
assert.equal(typeof object, 'object', 'object')
assert(value != null, 'value is required')

let type = types[typeName]
if(type) {
Expand All @@ -102,7 +102,7 @@ const toBuffer = (types, structs) => (typeName, object) => {
type = structs[typeName]
}
assert(type, 'missing type or struct: ' + typeName)
return Fcbuffer.toBuffer(type, object)
return Fcbuffer.toBuffer(type, value)
}

module.exports.fromBuffer = Fcbuffer.fromBuffer
Expand Down
6 changes: 4 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('API', function () {
const {bytes} = Types()
const type = bytes()
assertSerializer(type, '00aaeeff')
assertSerializer(type, Array.from([2, 1]), '020201', '0201')
assertSerializer(type, new Uint8Array([2, 1]), '020201', '0201')
assertRequired(type)
})

Expand Down Expand Up @@ -465,15 +467,15 @@ function assertCompile (definitions, config) {
return structs
}

function assertSerializer (type, value, bufferHex) {
function assertSerializer (type, value, bufferHex, internalValue = value) {
const obj = type.fromObject(value) // tests fromObject
const buf = Fcbuffer.toBuffer(type, obj) // tests appendByteBuffer
if(bufferHex != null) {
assert.equal(buf.toString('hex'), bufferHex)
}
const obj2 = Fcbuffer.fromBuffer(type, buf) // tests fromByteBuffer
const obj3 = type.toObject(obj) // tests toObject
deepEqual(value, obj3, 'serialize object')
deepEqual(internalValue, obj3, 'serialize object')
deepEqual(obj3, obj2, 'serialize buffer')
return value
}
Expand Down
8 changes: 7 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,13 @@ const bytebuf = (validation) => {
b.append(value.toString('binary'), 'binary')
},
fromObject (value) {
if (typeof value === 'string') { value = Buffer.from(value, 'hex') }
if (typeof value === 'string') {
value = Buffer.from(value, 'hex')
} else if (value instanceof Array) {
value = Buffer.from(value)
} else if (value instanceof Uint8Array) {
value = Buffer.from(value)
}

validate(value, validation)
return value
Expand Down

0 comments on commit 259e50c

Please sign in to comment.