UNPKG

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