fast-protocol
Version:
FAST streaming protocol for Node.js (Encoder/Decoder for Javascript) (FAST protocol version 1.1)
25 lines (18 loc) • 831 B
JavaScript
var Long = require('long')
//[ "+" | "-" ] digit+ [ "." digit+ ]
//[ "+" | "-" ] digit [ "." digit+ ] "e" [ "+" | "-" ] digit+
function parseDecimal(str) {
if (!str) return undefined
// [1] SIGN
// [2] + [4] MANTISSA
// [6] EXPONENT
var matches = str.match(/^([+-])?(\d+)?(\.)?(\d*)?(e([+-]?\d+))?$/)
var sign = matches[1] != null ? matches[1] : '+'
var pre = matches[2] != null ? matches[2] : ''
var post = matches[4] != null ? matches[4].replace(/0*$/, '') : ''
var mantissa = Long.fromString(pre.concat(post)).multiply(sign == '-' ? Long.NEG_ONE : Long.ONE)
var exponent = matches[6] != null ? Number(matches[6]) - post.length : 0 - post.length
return {m: mantissa.toString(10), e: exponent}
}
console.log('\n')
console.log('Parse number:', process.argv[2], 'result:', parseDecimal(process.argv[2]))