1 var Mode = require('./mode')
3 function NumericData (data) {
4 this.mode = Mode.NUMERIC
5 this.data = data.toString()
8 NumericData.getBitsLength = function getBitsLength (length) {
9 return 10 * Math.floor(length / 3) + ((length % 3) ? ((length % 3) * 3 + 1) : 0)
12 NumericData.prototype.getLength = function getLength () {
13 return this.data.length
16 NumericData.prototype.getBitsLength = function getBitsLength () {
17 return NumericData.getBitsLength(this.data.length)
20 NumericData.prototype.write = function write (bitBuffer) {
23 // The input data string is divided into groups of three digits,
24 // and each group is converted to its 10-bit binary equivalent.
25 for (i = 0; i + 3 <= this.data.length; i += 3) {
26 group = this.data.substr(i, 3)
27 value = parseInt(group, 10)
29 bitBuffer.put(value, 10)
32 // If the number of input digits is not an exact multiple of three,
33 // the final one or two digits are converted to 4 or 7 bits respectively.
34 var remainingNum = this.data.length - i
35 if (remainingNum > 0) {
36 group = this.data.substr(i)
37 value = parseInt(group, 10)
39 bitBuffer.put(value, remainingNum * 3 + 1)
43 module.exports = NumericData