rename precision to decimals, add typeTags, continue refactoring

This commit is contained in:
bordun 2020-02-29 17:01:29 +01:00
parent 37bf03a336
commit 9eb5dd5060
54 changed files with 426 additions and 256 deletions

View file

@ -314,7 +314,7 @@ module.exports = {
if (port) {
osc.send(host, port, data.address, data.args, data.precision)
osc.send(host, port, data.address, data.args, data.typeTags)
}

View file

@ -113,36 +113,41 @@ class OscServer {
}
parseType(type){
var t = type[0].match(/[bhtdscrmifTFNI]/)
parseArg(arg, type){
if (arg === null) return null
if (type && type[0].match(/[bhtdscrmifTFNI]/)) {
type = type[0]
switch (type) {
case 'i':
return {type: type, value: parseInt(arg)}
case 'd':
case 'f':
return {type: type, value: parseFloat(arg)}
case 'T':
case 'F':
case 'N':
return {type: type}
default:
return {type: type, value: arg}
}
if (t!==null) {
return t[0]
} else {
return 's'
}
}
parseArg(arg, precision){
if (arg==null) return null
switch (typeof arg) {
case 'number':
var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null,
typeTag = typeTagMatch === null ? precision === 0 ? 'i' : 'f' : typeTagMatch[1],
precisionValue = parseFloat(precision) || 0
return {type: typeTag, value: precision === undefined ? arg : parseFloat(arg.toFixed(precisionValue))}
return {type: 'f', value: arg}
case 'boolean':
return {type: arg ? 'T' : 'F', value: arg}
case 'object':
if (arg.type) {
return {type: this.parseType(arg.type), value: arg.value}
} else {
return {type: 's', value:JSON.stringify(arg)}
}
return {type: arg ? 'T' : 'F'}
case 'string':
return {type: 's', value:arg}
case 'object':
if (arg.type) return arg
default:
return {type: 's', value:JSON.stringify(arg)}
}
@ -176,7 +181,7 @@ class OscServer {
address: data.address,
args: data.args
}, data.host, data.port)
}
if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To: ' + data.host + ':' + data.port)
@ -272,15 +277,24 @@ oscServer.init()
module.exports = {
server: oscServer,
send: function(host,port,address,args,precision) {
send: function(host, port, address, args, typeTags) {
var message = []
if (!Array.isArray(args)) args = [args]
if (typeof args=='object' && args!==null) {
for (var i in args) {
var arg = oscServer.parseArg(args[i],precision)
if (arg!=null) message.push(arg)
if (typeof args === 'object' && args !== null) {
var typeTag,
arg
for (var i = 0; i < args.length; i++) {
typeTag = typeTags[i] || typeTag
arg = oscServer.parseArg(args[i], typeTag)
if (arg !== null) message.push(arg)
}
}

View file

@ -1,6 +1,6 @@
module.exports = {
deepCopy: function(obj, precision) {
deepCopy: function(obj) {
var copy = obj
@ -11,10 +11,8 @@ module.exports = {
if (typeof obj === 'object') {
copy = Array.isArray(obj) ? [] : {}
for (let key in obj) {
copy[key] = module.exports.deepCopy(obj[key], precision)
copy[key] = module.exports.deepCopy(obj[key])
}
} else if (typeof obj == 'number') {
return precision === undefined ? copy : parseFloat(copy.toFixed(precision))
}
return copy