numberValue.js 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var util = require('../core').util;
  2. /**
  3. * An object recognizable as a numeric value that stores the underlying number
  4. * as a string.
  5. *
  6. * Intended to be a deserialization target for the DynamoDB Document Client when
  7. * the `wrapNumbers` flag is set. This allows for numeric values that lose
  8. * precision when converted to JavaScript's `number` type.
  9. */
  10. var DynamoDBNumberValue = util.inherit({
  11. constructor: function NumberValue(value) {
  12. this.wrapperName = 'NumberValue';
  13. this.value = value.toString();
  14. },
  15. /**
  16. * Render the underlying value as a number when converting to JSON.
  17. */
  18. toJSON: function () {
  19. return this.toNumber();
  20. },
  21. /**
  22. * Convert the underlying value to a JavaScript number.
  23. */
  24. toNumber: function () {
  25. return Number(this.value);
  26. },
  27. /**
  28. * Return a string representing the unaltered value provided to the
  29. * constructor.
  30. */
  31. toString: function () {
  32. return this.value;
  33. }
  34. });
  35. /**
  36. * @api private
  37. */
  38. module.exports = DynamoDBNumberValue;