@wopr-network/platform-core 1.39.2 → 1.39.3

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.
@@ -91,15 +91,27 @@ export class Credit {
91
91
  }
92
92
  /** Add another Credit, returning a new Credit. */
93
93
  add(other) {
94
- return new Credit(this.raw + other.raw);
94
+ const result = this.raw + other.raw;
95
+ if (!Number.isSafeInteger(result)) {
96
+ throw new RangeError(`Credit.add overflow: ${this.raw} + ${other.raw} = ${result}`);
97
+ }
98
+ return new Credit(result);
95
99
  }
96
100
  /** Subtract another Credit, returning a new Credit (may be negative). */
97
101
  subtract(other) {
98
- return new Credit(this.raw - other.raw);
102
+ const result = this.raw - other.raw;
103
+ if (!Number.isSafeInteger(result)) {
104
+ throw new RangeError(`Credit.subtract overflow: ${this.raw} - ${other.raw} = ${result}`);
105
+ }
106
+ return new Credit(result);
99
107
  }
100
108
  /** Multiply by a factor, rounding to nearest raw unit. */
101
109
  multiply(factor) {
102
- return new Credit(Math.round(this.raw * factor));
110
+ const result = Math.round(this.raw * factor);
111
+ if (!Number.isSafeInteger(result)) {
112
+ throw new RangeError(`Credit.multiply overflow: ${this.raw} * ${factor} = ${result}`);
113
+ }
114
+ return new Credit(result);
103
115
  }
104
116
  /** True if this credit is negative. */
105
117
  isNegative() {
@@ -103,6 +103,38 @@ describe("Credit", () => {
103
103
  it("multiply by zero gives zero", () => {
104
104
  expect(Credit.fromDollars(5).multiply(0).isZero()).toBe(true);
105
105
  });
106
+ it("add throws RangeError on positive overflow", () => {
107
+ const a = Credit.fromRaw(Number.MAX_SAFE_INTEGER);
108
+ const b = Credit.fromRaw(1);
109
+ expect(() => a.add(b)).toThrow(RangeError);
110
+ });
111
+ it("subtract throws RangeError on negative overflow", () => {
112
+ const a = Credit.fromRaw(-Number.MAX_SAFE_INTEGER);
113
+ const b = Credit.fromRaw(1);
114
+ expect(() => a.subtract(b)).toThrow(RangeError);
115
+ });
116
+ it("multiply throws RangeError on overflow", () => {
117
+ const a = Credit.fromRaw(Number.MAX_SAFE_INTEGER);
118
+ expect(() => a.multiply(2)).toThrow(RangeError);
119
+ });
120
+ it("multiply throws RangeError on Infinity factor", () => {
121
+ const a = Credit.fromRaw(1);
122
+ expect(() => a.multiply(Infinity)).toThrow(RangeError);
123
+ });
124
+ it("multiply throws RangeError on NaN factor", () => {
125
+ const a = Credit.fromRaw(1);
126
+ expect(() => a.multiply(NaN)).toThrow(RangeError);
127
+ });
128
+ it("add does not throw for values within safe range", () => {
129
+ const a = Credit.fromRaw(Number.MAX_SAFE_INTEGER - 1);
130
+ const b = Credit.fromRaw(1);
131
+ expect(a.add(b).toRaw()).toBe(Number.MAX_SAFE_INTEGER);
132
+ });
133
+ it("subtract does not throw for values within safe range", () => {
134
+ const a = Credit.fromRaw(-(Number.MAX_SAFE_INTEGER - 1));
135
+ const b = Credit.fromRaw(1);
136
+ expect(a.subtract(b).toRaw()).toBe(-Number.MAX_SAFE_INTEGER);
137
+ });
106
138
  });
107
139
  describe("comparison", () => {
108
140
  it("isNegative returns true for negative", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wopr-network/platform-core",
3
- "version": "1.39.2",
3
+ "version": "1.39.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -124,6 +124,45 @@ describe("Credit", () => {
124
124
  it("multiply by zero gives zero", () => {
125
125
  expect(Credit.fromDollars(5).multiply(0).isZero()).toBe(true);
126
126
  });
127
+
128
+ it("add throws RangeError on positive overflow", () => {
129
+ const a = Credit.fromRaw(Number.MAX_SAFE_INTEGER);
130
+ const b = Credit.fromRaw(1);
131
+ expect(() => a.add(b)).toThrow(RangeError);
132
+ });
133
+
134
+ it("subtract throws RangeError on negative overflow", () => {
135
+ const a = Credit.fromRaw(-Number.MAX_SAFE_INTEGER);
136
+ const b = Credit.fromRaw(1);
137
+ expect(() => a.subtract(b)).toThrow(RangeError);
138
+ });
139
+
140
+ it("multiply throws RangeError on overflow", () => {
141
+ const a = Credit.fromRaw(Number.MAX_SAFE_INTEGER);
142
+ expect(() => a.multiply(2)).toThrow(RangeError);
143
+ });
144
+
145
+ it("multiply throws RangeError on Infinity factor", () => {
146
+ const a = Credit.fromRaw(1);
147
+ expect(() => a.multiply(Infinity)).toThrow(RangeError);
148
+ });
149
+
150
+ it("multiply throws RangeError on NaN factor", () => {
151
+ const a = Credit.fromRaw(1);
152
+ expect(() => a.multiply(NaN)).toThrow(RangeError);
153
+ });
154
+
155
+ it("add does not throw for values within safe range", () => {
156
+ const a = Credit.fromRaw(Number.MAX_SAFE_INTEGER - 1);
157
+ const b = Credit.fromRaw(1);
158
+ expect(a.add(b).toRaw()).toBe(Number.MAX_SAFE_INTEGER);
159
+ });
160
+
161
+ it("subtract does not throw for values within safe range", () => {
162
+ const a = Credit.fromRaw(-(Number.MAX_SAFE_INTEGER - 1));
163
+ const b = Credit.fromRaw(1);
164
+ expect(a.subtract(b).toRaw()).toBe(-Number.MAX_SAFE_INTEGER);
165
+ });
127
166
  });
128
167
 
129
168
  describe("comparison", () => {
@@ -101,17 +101,29 @@ export class Credit {
101
101
 
102
102
  /** Add another Credit, returning a new Credit. */
103
103
  add(other: Credit): Credit {
104
- return new Credit(this.raw + other.raw);
104
+ const result = this.raw + other.raw;
105
+ if (!Number.isSafeInteger(result)) {
106
+ throw new RangeError(`Credit.add overflow: ${this.raw} + ${other.raw} = ${result}`);
107
+ }
108
+ return new Credit(result);
105
109
  }
106
110
 
107
111
  /** Subtract another Credit, returning a new Credit (may be negative). */
108
112
  subtract(other: Credit): Credit {
109
- return new Credit(this.raw - other.raw);
113
+ const result = this.raw - other.raw;
114
+ if (!Number.isSafeInteger(result)) {
115
+ throw new RangeError(`Credit.subtract overflow: ${this.raw} - ${other.raw} = ${result}`);
116
+ }
117
+ return new Credit(result);
110
118
  }
111
119
 
112
120
  /** Multiply by a factor, rounding to nearest raw unit. */
113
121
  multiply(factor: number): Credit {
114
- return new Credit(Math.round(this.raw * factor));
122
+ const result = Math.round(this.raw * factor);
123
+ if (!Number.isSafeInteger(result)) {
124
+ throw new RangeError(`Credit.multiply overflow: ${this.raw} * ${factor} = ${result}`);
125
+ }
126
+ return new Credit(result);
115
127
  }
116
128
 
117
129
  /** True if this credit is negative. */