From f9b4aac204db28547f0cfa78d12f8d01f8380f93 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 29 May 2012 18:38:59 -0700 Subject: [PATCH] added support for writing JS strings to the "char" and "uchar" types --- lib/ref.js | 6 ++++++ test/char.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/char.js diff --git a/lib/ref.js b/lib/ref.js index ee34a03..4996691 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -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) } } @@ -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) } } diff --git a/test/char.js b/test/char.js new file mode 100644 index 0000000..80a1071 --- /dev/null +++ b/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()) + }) + +})