Skip to content

Commit

Permalink
added support for writing JS strings to the "char" and "uchar" types
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 30, 2012
1 parent 313157f commit f9b4aac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ref.js
Expand Up @@ -337,6 +337,9 @@ exports.types.int8 = {
return buf.readInt8(offset || 0)
}
, set: function set (buf, offset, val) {
if (typeof val === 'string') {
val = val.charCodeAt(0)
}
return buf.writeInt8(val, offset || 0)
}
}
Expand All @@ -347,6 +350,9 @@ exports.types.uint8 = {
return buf.readUInt8(offset || 0)
}
, set: function set (buf, offset, val) {
if (typeof val === 'string') {
val = val.charCodeAt(0)
}
return buf.writeUInt8(val, offset || 0)
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/char.js
@@ -0,0 +1,17 @@

var assert = require('assert')
var ref = require('../')

describe('char', function () {

it('should accept a JS String, and write the first char\'s code', function () {
var val = 'a'

var buf = ref.alloc('char', val)
assert.strictEqual(val.charCodeAt(0), buf.deref())

buf = ref.alloc('uchar', val)
assert.strictEqual(val.charCodeAt(0), buf.deref())
})

})

0 comments on commit f9b4aac

Please sign in to comment.