numeric-data.js 1.2 KB

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