@aws-sdk/util-dynamodb 3.438.0 → 3.441.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NumberValue = void 0;
4
+ class NumberValue {
5
+ constructor(value) {
6
+ if (typeof value === "object" && "N" in value) {
7
+ this.value = String(value.N);
8
+ }
9
+ else {
10
+ this.value = String(value);
11
+ }
12
+ const valueOf = typeof value.valueOf() === "number" ? value.valueOf() : 0;
13
+ const imprecise = valueOf > Number.MAX_SAFE_INTEGER ||
14
+ valueOf < Number.MIN_SAFE_INTEGER ||
15
+ Math.abs(valueOf) === Infinity ||
16
+ Number.isNaN(valueOf);
17
+ if (imprecise) {
18
+ throw new Error(`NumberValue should not be initialized with an imprecise number=${valueOf}. Use a string instead.`);
19
+ }
20
+ }
21
+ static from(value) {
22
+ return new NumberValue(value);
23
+ }
24
+ toAttributeValue() {
25
+ return {
26
+ N: this.toString(),
27
+ };
28
+ }
29
+ toBigInt() {
30
+ const stringValue = this.toString();
31
+ return BigInt(stringValue);
32
+ }
33
+ toString() {
34
+ return String(this.value);
35
+ }
36
+ valueOf() {
37
+ return this.toString();
38
+ }
39
+ }
40
+ exports.NumberValue = NumberValue;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertToAttr = void 0;
4
+ const NumberValue_1 = require("./NumberValue");
4
5
  const convertToAttr = (data, options) => {
5
6
  var _a, _b, _c, _d, _e, _f;
6
7
  if (data === undefined) {
@@ -34,6 +35,9 @@ const convertToAttr = (data, options) => {
34
35
  else if (typeof data === "number" || ((_e = data === null || data === void 0 ? void 0 : data.constructor) === null || _e === void 0 ? void 0 : _e.name) === "Number") {
35
36
  return convertToNumberAttr(data);
36
37
  }
38
+ else if (data instanceof NumberValue_1.NumberValue) {
39
+ return data.toAttributeValue();
40
+ }
37
41
  else if (typeof data === "bigint") {
38
42
  return convertToBigIntAttr(data);
39
43
  }
@@ -66,7 +70,12 @@ const convertToSetAttr = (set, options) => {
66
70
  throw new Error(`Pass a non-empty set, or options.convertEmptyValues=true.`);
67
71
  }
68
72
  const item = setToOperate.values().next().value;
69
- if (typeof item === "number") {
73
+ if (item instanceof NumberValue_1.NumberValue) {
74
+ return {
75
+ NS: Array.from(setToOperate).map((_) => _.toString()),
76
+ };
77
+ }
78
+ else if (typeof item === "number") {
70
79
  return {
71
80
  NS: Array.from(setToOperate)
72
81
  .map(convertToNumberAttr)
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertToNative = void 0;
4
+ const NumberValue_1 = require("./NumberValue");
4
5
  const convertToNative = (data, options) => {
5
6
  for (const [key, value] of Object.entries(data)) {
6
7
  if (value !== undefined) {
@@ -35,11 +36,12 @@ const convertToNative = (data, options) => {
35
36
  exports.convertToNative = convertToNative;
36
37
  const convertNumber = (numString, options) => {
37
38
  if (options === null || options === void 0 ? void 0 : options.wrapNumbers) {
38
- return { value: numString };
39
+ return NumberValue_1.NumberValue.from(numString);
39
40
  }
40
41
  const num = Number(numString);
41
42
  const infinityValues = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
42
- if ((num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num)) {
43
+ const isLargeFiniteNumber = (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num);
44
+ if (isLargeFiniteNumber) {
43
45
  if (typeof BigInt === "function") {
44
46
  try {
45
47
  return BigInt(numString);
package/dist-cjs/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NumberValueImpl = void 0;
3
4
  const tslib_1 = require("tslib");
5
+ var NumberValue_1 = require("./NumberValue");
6
+ Object.defineProperty(exports, "NumberValueImpl", { enumerable: true, get: function () { return NumberValue_1.NumberValue; } });
4
7
  tslib_1.__exportStar(require("./convertToAttr"), exports);
5
8
  tslib_1.__exportStar(require("./convertToNative"), exports);
6
9
  tslib_1.__exportStar(require("./marshall"), exports);
@@ -0,0 +1,36 @@
1
+ export class NumberValue {
2
+ constructor(value) {
3
+ if (typeof value === "object" && "N" in value) {
4
+ this.value = String(value.N);
5
+ }
6
+ else {
7
+ this.value = String(value);
8
+ }
9
+ const valueOf = typeof value.valueOf() === "number" ? value.valueOf() : 0;
10
+ const imprecise = valueOf > Number.MAX_SAFE_INTEGER ||
11
+ valueOf < Number.MIN_SAFE_INTEGER ||
12
+ Math.abs(valueOf) === Infinity ||
13
+ Number.isNaN(valueOf);
14
+ if (imprecise) {
15
+ throw new Error(`NumberValue should not be initialized with an imprecise number=${valueOf}. Use a string instead.`);
16
+ }
17
+ }
18
+ static from(value) {
19
+ return new NumberValue(value);
20
+ }
21
+ toAttributeValue() {
22
+ return {
23
+ N: this.toString(),
24
+ };
25
+ }
26
+ toBigInt() {
27
+ const stringValue = this.toString();
28
+ return BigInt(stringValue);
29
+ }
30
+ toString() {
31
+ return String(this.value);
32
+ }
33
+ valueOf() {
34
+ return this.toString();
35
+ }
36
+ }
@@ -1,3 +1,4 @@
1
+ import { NumberValue } from "./NumberValue";
1
2
  export const convertToAttr = (data, options) => {
2
3
  if (data === undefined) {
3
4
  throw new Error(`Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.`);
@@ -30,6 +31,9 @@ export const convertToAttr = (data, options) => {
30
31
  else if (typeof data === "number" || data?.constructor?.name === "Number") {
31
32
  return convertToNumberAttr(data);
32
33
  }
34
+ else if (data instanceof NumberValue) {
35
+ return data.toAttributeValue();
36
+ }
33
37
  else if (typeof data === "bigint") {
34
38
  return convertToBigIntAttr(data);
35
39
  }
@@ -61,7 +65,12 @@ const convertToSetAttr = (set, options) => {
61
65
  throw new Error(`Pass a non-empty set, or options.convertEmptyValues=true.`);
62
66
  }
63
67
  const item = setToOperate.values().next().value;
64
- if (typeof item === "number") {
68
+ if (item instanceof NumberValue) {
69
+ return {
70
+ NS: Array.from(setToOperate).map((_) => _.toString()),
71
+ };
72
+ }
73
+ else if (typeof item === "number") {
65
74
  return {
66
75
  NS: Array.from(setToOperate)
67
76
  .map(convertToNumberAttr)
@@ -1,3 +1,4 @@
1
+ import { NumberValue } from "./NumberValue";
1
2
  export const convertToNative = (data, options) => {
2
3
  for (const [key, value] of Object.entries(data)) {
3
4
  if (value !== undefined) {
@@ -31,11 +32,12 @@ export const convertToNative = (data, options) => {
31
32
  };
32
33
  const convertNumber = (numString, options) => {
33
34
  if (options?.wrapNumbers) {
34
- return { value: numString };
35
+ return NumberValue.from(numString);
35
36
  }
36
37
  const num = Number(numString);
37
38
  const infinityValues = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
38
- if ((num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num)) {
39
+ const isLargeFiniteNumber = (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num);
40
+ if (isLargeFiniteNumber) {
39
41
  if (typeof BigInt === "function") {
40
42
  try {
41
43
  return BigInt(numString);
package/dist-es/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { NumberValue as NumberValueImpl } from "./NumberValue";
1
2
  export * from "./convertToAttr";
2
3
  export * from "./convertToNative";
3
4
  export * from "./marshall";
@@ -0,0 +1,55 @@
1
+ import { NumberValue as INumberValue } from "./models";
2
+ /**
3
+ *
4
+ * Class for storing DynamoDB numbers that exceed the scale of
5
+ * JavaScript's MAX_SAFE_INTEGER and MIN_SAFE_INTEGER, or the
6
+ * decimal precision limit.
7
+ *
8
+ * This class does not support mathematical operations in JavaScript.
9
+ * Convert the contained string value to your application-specific
10
+ * large number implementation to perform mathematical operations.
11
+ *
12
+ * @public
13
+ *
14
+ */
15
+ export declare class NumberValue implements INumberValue {
16
+ value: string;
17
+ /**
18
+ * This class does not validate that your string input is a valid number.
19
+ *
20
+ * @param value - a precise number, or any BigInt or string, or AttributeValue.
21
+ */
22
+ constructor(value: number | Number | BigInt | string | {
23
+ N: string;
24
+ });
25
+ /**
26
+ * This class does not validate that your string input is a valid number.
27
+ *
28
+ * @param value - a precise number, or any BigInt or string, or AttributeValue.
29
+ */
30
+ static from(value: number | Number | BigInt | string | {
31
+ N: string;
32
+ }): NumberValue;
33
+ /**
34
+ * @returns the AttributeValue form for DynamoDB.
35
+ */
36
+ toAttributeValue(): {
37
+ N: string;
38
+ };
39
+ /**
40
+ * @returns BigInt representation.
41
+ *
42
+ * @throws SyntaxError if the string representation is not convertable to a BigInt.
43
+ */
44
+ toBigInt(): bigint;
45
+ /**
46
+ * @override
47
+ *
48
+ * @returns string representation. This is the canonical format in DynamoDB.
49
+ */
50
+ toString(): string;
51
+ /**
52
+ * @override
53
+ */
54
+ valueOf(): string;
55
+ }
@@ -1,5 +1,5 @@
1
1
  import { AttributeValue } from "@aws-sdk/client-dynamodb";
2
- import { NativeAttributeValue } from "./models";
2
+ import type { NativeAttributeValue } from "./models";
3
3
  import { unmarshallOptions } from "./unmarshall";
4
4
  /**
5
5
  * Convert a DynamoDB AttributeValue object to its equivalent JavaScript type.
@@ -1,3 +1,4 @@
1
+ export { NumberValue as NumberValueImpl } from "./NumberValue";
1
2
  export * from "./convertToAttr";
2
3
  export * from "./convertToNative";
3
4
  export * from "./marshall";
@@ -0,0 +1,30 @@
1
+ import { NumberValue as INumberValue } from "./models";
2
+ export declare class NumberValue implements INumberValue {
3
+ value: string;
4
+ constructor(
5
+ value:
6
+ | number
7
+ | Number
8
+ | BigInt
9
+ | string
10
+ | {
11
+ N: string;
12
+ }
13
+ );
14
+ static from(
15
+ value:
16
+ | number
17
+ | Number
18
+ | BigInt
19
+ | string
20
+ | {
21
+ N: string;
22
+ }
23
+ ): NumberValue;
24
+ toAttributeValue(): {
25
+ N: string;
26
+ };
27
+ toBigInt(): bigint;
28
+ toString(): string;
29
+ valueOf(): string;
30
+ }
@@ -1,3 +1,4 @@
1
+ export { NumberValue as NumberValueImpl } from "./NumberValue";
1
2
  export * from "./convertToAttr";
2
3
  export * from "./convertToNative";
3
4
  export * from "./marshall";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/util-dynamodb",
3
- "version": "3.438.0",
3
+ "version": "3.441.0",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
6
6
  "build:cjs": "tsc -p tsconfig.cjs.json",
@@ -24,7 +24,7 @@
24
24
  "tslib": "^2.5.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@aws-sdk/client-dynamodb": "3.438.0",
27
+ "@aws-sdk/client-dynamodb": "3.441.0",
28
28
  "@tsconfig/recommended": "1.0.1",
29
29
  "concurrently": "7.0.0",
30
30
  "downlevel-dts": "0.10.1",