Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

message: add alias ability for type tags #108

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Message.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const typeTags = {
s: 'string',
f: 'float',
i: 'integer',
b: 'blob'
};

class Argument {
constructor(type, value) {
this.type = type;
Expand All @@ -19,6 +26,7 @@ class Message {
if (arg instanceof Array) {
arg.forEach(a => this.append(a));
} else if (arg.type) {
if (typeTags[arg.type]) arg.type = typeTags[arg.type];
this.args.push(arg);
} else {
throw new Error(`don't know how to encode object ${arg}`);
Expand Down
41 changes: 39 additions & 2 deletions test/test-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { bootstrap } from './util.mjs';

import { Server, Client, Message } from 'node-osc';

function round(num) {
return Math.round(num * 100) / 100;
}

beforeEach(bootstrap);

test('message: basic usage', (t) => {
Expand Down Expand Up @@ -79,7 +83,40 @@ test('message: float', (t) => {
3.14
];
t.equal(msg[0], expected[0], `We reveived the payload: ${msg}`);
t.equal(msg[1][0], expected[1][0], 'pie please');
t.equal(round(msg[1]), expected[1], 'pie please');
oscServer.close();
t.end();
});

client.send(m, () => {
client.close();
});
});

test('message: alias messages', (t) => {
const oscServer = new Server(t.context.port, '127.0.0.1');
const client = new Client('127.0.0.1', t.context.port);
const m = new Message('/address');
m.append({
type: 'i',
value: 123
});
m.append({
type: 'f',
value: 3.14
});

oscServer.on('message', (msg) => {
const expected = [
'/address',
123,
3.14
];
t.equal(msg[0], expected[0], `We reveived the payload: ${msg}`);
t.equal(msg[1], expected[1], 'easy as abc');
t.ok(Number.isInteger(msg[1]), 'the first value is an int');
t.equal(round(msg[2]), expected[2], 'pie please');
t.ok(msg[2] % 1 !== 0, 'the second value is a float');
oscServer.close();
t.end();
});
Expand Down Expand Up @@ -138,7 +175,7 @@ test('message: blob', (t) => {
// test('message: timetag', (t) => {
// const oscServer = new osc.Server(3333, '127.0.0.1');
// const client = new osc.Client('127.0.0.1', 3333);
// const m = new osc.Message('/address');z
// const m = new osc.Message('/address');
//
// oscServer.on('message', (msg) => {
// const expected = [
Expand Down
Loading