@ragestudio/scylla-odm 0.22.2 → 0.22.4

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.
Files changed (153) hide show
  1. package/batch/index.d.ts +3 -3
  2. package/batch/index.d.ts.map +1 -1
  3. package/client.d.ts +6 -5
  4. package/client.d.ts.map +1 -1
  5. package/client.js +11 -12
  6. package/client.js.map +1 -1
  7. package/cql_gen/create_table.d.ts +1 -1
  8. package/cql_gen/create_table.d.ts.map +1 -1
  9. package/document/index.d.ts +3 -3
  10. package/document/index.d.ts.map +1 -1
  11. package/driver/LICENSE.txt +177 -0
  12. package/driver/NOTICE.txt +67 -0
  13. package/driver/auth/index.d.ts +37 -0
  14. package/driver/auth/index.js +37 -0
  15. package/driver/auth/no-auth-provider.js +73 -0
  16. package/driver/auth/plain-text-auth-provider.js +81 -0
  17. package/driver/auth/provider.js +77 -0
  18. package/driver/client-options.js +442 -0
  19. package/driver/client.js +1267 -0
  20. package/driver/concurrent/index.d.ts +49 -0
  21. package/driver/concurrent/index.js +366 -0
  22. package/driver/connection.js +1034 -0
  23. package/driver/control-connection.js +1282 -0
  24. package/driver/encoder.js +2316 -0
  25. package/driver/errors.js +223 -0
  26. package/driver/execution-options.js +612 -0
  27. package/driver/execution-profile.js +274 -0
  28. package/driver/host-connection-pool.js +587 -0
  29. package/driver/host.js +699 -0
  30. package/driver/index.d.ts +387 -0
  31. package/driver/index.js +81 -0
  32. package/driver/mapping/cache.js +214 -0
  33. package/driver/mapping/doc-info-adapter.js +171 -0
  34. package/driver/mapping/index.d.ts +219 -0
  35. package/driver/mapping/index.js +57 -0
  36. package/driver/mapping/mapper.js +225 -0
  37. package/driver/mapping/mapping-handler.js +641 -0
  38. package/driver/mapping/model-batch-item.js +215 -0
  39. package/driver/mapping/model-batch-mapper.js +141 -0
  40. package/driver/mapping/model-mapper.js +315 -0
  41. package/driver/mapping/model-mapping-info.js +225 -0
  42. package/driver/mapping/object-selector.js +417 -0
  43. package/driver/mapping/q.js +156 -0
  44. package/driver/mapping/query-generator.js +556 -0
  45. package/driver/mapping/result-mapper.js +123 -0
  46. package/driver/mapping/result.js +139 -0
  47. package/driver/mapping/table-mappings.js +133 -0
  48. package/driver/mapping/tree.js +160 -0
  49. package/driver/metadata/aggregate.js +79 -0
  50. package/driver/metadata/client-state.js +119 -0
  51. package/driver/metadata/data-collection.js +182 -0
  52. package/driver/metadata/event-debouncer.js +174 -0
  53. package/driver/metadata/index.d.ts +276 -0
  54. package/driver/metadata/index.js +1156 -0
  55. package/driver/metadata/materialized-view.js +49 -0
  56. package/driver/metadata/schema-function.js +98 -0
  57. package/driver/metadata/schema-index.js +166 -0
  58. package/driver/metadata/schema-parser.js +1399 -0
  59. package/driver/metadata/table-metadata.js +77 -0
  60. package/driver/operation-state.js +206 -0
  61. package/driver/policies/address-resolution.js +145 -0
  62. package/driver/policies/index.d.ts +241 -0
  63. package/driver/policies/index.js +110 -0
  64. package/driver/policies/load-balancing.js +970 -0
  65. package/driver/policies/reconnection.js +166 -0
  66. package/driver/policies/retry.js +326 -0
  67. package/driver/policies/speculative-execution.js +150 -0
  68. package/driver/policies/timestamp-generation.js +176 -0
  69. package/driver/prepare-handler.js +347 -0
  70. package/driver/promise-utils.js +191 -0
  71. package/driver/readers.js +624 -0
  72. package/driver/request-execution.js +644 -0
  73. package/driver/request-handler.js +332 -0
  74. package/driver/requests.js +618 -0
  75. package/driver/stream-id-stack.js +209 -0
  76. package/driver/streams.js +745 -0
  77. package/driver/token.js +325 -0
  78. package/driver/tokenizer.js +631 -0
  79. package/driver/types/big-decimal.js +282 -0
  80. package/driver/types/duration.js +576 -0
  81. package/driver/types/index.d.ts +486 -0
  82. package/driver/types/index.js +733 -0
  83. package/driver/types/inet-address.js +262 -0
  84. package/driver/types/integer.js +818 -0
  85. package/driver/types/local-date.js +280 -0
  86. package/driver/types/local-time.js +299 -0
  87. package/driver/types/mutable-long.js +385 -0
  88. package/driver/types/protocol-version.js +391 -0
  89. package/driver/types/result-set.js +287 -0
  90. package/driver/types/result-stream.js +164 -0
  91. package/driver/types/row.js +85 -0
  92. package/driver/types/time-uuid.js +414 -0
  93. package/driver/types/tuple.js +103 -0
  94. package/driver/types/uuid.js +160 -0
  95. package/driver/types/vector.js +130 -0
  96. package/driver/types/version-number.js +153 -0
  97. package/driver/utils.js +1485 -0
  98. package/driver/writers.js +350 -0
  99. package/global.d.ts +1 -1
  100. package/global.d.ts.map +1 -1
  101. package/index.d.ts +6 -6
  102. package/index.d.ts.map +1 -1
  103. package/index.js +6 -6
  104. package/index.js.map +1 -1
  105. package/migrate/index.d.ts +1 -1
  106. package/migrate/index.d.ts.map +1 -1
  107. package/migrate/index.js +1 -1
  108. package/migrate/index.js.map +1 -1
  109. package/model/index.d.ts +6 -6
  110. package/model/index.d.ts.map +1 -1
  111. package/model/index.js +10 -10
  112. package/model/index.js.map +1 -1
  113. package/operations/countAll.d.ts +1 -1
  114. package/operations/countAll.d.ts.map +1 -1
  115. package/operations/delete.d.ts +3 -4
  116. package/operations/delete.d.ts.map +1 -1
  117. package/operations/delete.js +1 -1
  118. package/operations/delete.js.map +1 -1
  119. package/operations/find.d.ts +2 -2
  120. package/operations/find.d.ts.map +1 -1
  121. package/operations/find.js +1 -1
  122. package/operations/find.js.map +1 -1
  123. package/operations/findOne.d.ts +2 -2
  124. package/operations/findOne.d.ts.map +1 -1
  125. package/operations/findOne.js +1 -1
  126. package/operations/findOne.js.map +1 -1
  127. package/operations/insert.d.ts +3 -3
  128. package/operations/insert.d.ts.map +1 -1
  129. package/operations/insert.js +2 -2
  130. package/operations/insert.js.map +1 -1
  131. package/operations/sync.d.ts +1 -1
  132. package/operations/sync.d.ts.map +1 -1
  133. package/operations/sync.js +1 -1
  134. package/operations/sync.js.map +1 -1
  135. package/operations/tableExists.d.ts +1 -1
  136. package/operations/tableExists.d.ts.map +1 -1
  137. package/operations/update.d.ts +3 -3
  138. package/operations/update.d.ts.map +1 -1
  139. package/operations/update.js +2 -2
  140. package/operations/update.js.map +1 -1
  141. package/package.json +4 -12
  142. package/schema/index.d.ts +1 -1
  143. package/schema/index.d.ts.map +1 -1
  144. package/types.d.ts +4 -4
  145. package/types.d.ts.map +1 -1
  146. package/utils/queryParser.d.ts +1 -1
  147. package/utils/queryParser.d.ts.map +1 -1
  148. package/utils/queryParser.js +1 -1
  149. package/utils/queryParser.js.map +1 -1
  150. package/utils/typeChecker.d.ts +1 -1
  151. package/utils/typeChecker.d.ts.map +1 -1
  152. package/utils/typeChecker.js +1 -1
  153. package/utils/typeChecker.js.map +1 -1
@@ -0,0 +1,818 @@
1
+ // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS-IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ /** @module types */
16
+
17
+ import utils from "../utils.js"
18
+
19
+ /**
20
+ * Constructs a two's-complement integer an array containing bits of the
21
+ * integer in 32-bit (signed) pieces, given in little-endian order (i.e.,
22
+ * lowest-order bits in the first piece), and the sign of -1 or 0.
23
+ *
24
+ * See the from* functions below for other convenient ways of constructing
25
+ * Integers.
26
+ *
27
+ * The internal representation of an integer is an array of 32-bit signed
28
+ * pieces, along with a sign (0 or -1) that indicates the contents of all the
29
+ * other 32-bit pieces out to infinity. We use 32-bit pieces because these are
30
+ * the size of integers on which Javascript performs bit-operations. For
31
+ * operations like addition and multiplication, we split each number into 16-bit
32
+ * pieces, which can easily be multiplied within Javascript's floating-point
33
+ * representation without overflow or change in sign.
34
+ *
35
+ * @constructor
36
+ * @param {Array.<number>} bits Array containing the bits of the number.
37
+ * @param {number} sign The sign of the number: -1 for negative and 0 positive.
38
+ * @final
39
+ */
40
+ function Integer(bits, sign) {
41
+ /**
42
+ * @type {!Array.<number>}
43
+ * @private
44
+ */
45
+ this.bits_ = []
46
+
47
+ /**
48
+ * @type {number}
49
+ * @private
50
+ */
51
+ this.sign_ = sign
52
+
53
+ // Copy the 32-bit signed integer values passed in. We prune out those at the
54
+ // top that equal the sign since they are redundant.
55
+ var top = true
56
+ for (var i = bits.length - 1; i >= 0; i--) {
57
+ var val = bits[i] | 0
58
+ if (!top || val != sign) {
59
+ this.bits_[i] = val
60
+ top = false
61
+ }
62
+ }
63
+ }
64
+
65
+ // NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
66
+ // from* methods on which they depend.
67
+
68
+ /**
69
+ * A cache of the Integer representations of small integer values.
70
+ * @type {!Object}
71
+ * @private
72
+ */
73
+ Integer.IntCache_ = {}
74
+
75
+ /**
76
+ * Returns an Integer representing the given (32-bit) integer value.
77
+ * @param {number} value A 32-bit integer value.
78
+ * @return {!Integer} The corresponding Integer value.
79
+ */
80
+ Integer.fromInt = function (value) {
81
+ if (-128 <= value && value < 128) {
82
+ var cachedObj = Integer.IntCache_[value]
83
+ if (cachedObj) {
84
+ return cachedObj
85
+ }
86
+ }
87
+
88
+ var obj = new Integer([value | 0], value < 0 ? -1 : 0)
89
+ if (-128 <= value && value < 128) {
90
+ Integer.IntCache_[value] = obj
91
+ }
92
+ return obj
93
+ }
94
+
95
+ /**
96
+ * Returns an Integer representing the given value, provided that it is a finite
97
+ * number. Otherwise, zero is returned.
98
+ * @param {number} value The value in question.
99
+ * @return {!Integer} The corresponding Integer value.
100
+ */
101
+ Integer.fromNumber = function (value) {
102
+ if (isNaN(value) || !isFinite(value)) {
103
+ return Integer.ZERO
104
+ } else if (value < 0) {
105
+ return Integer.fromNumber(-value).negate()
106
+ } else {
107
+ var bits = []
108
+ var pow = 1
109
+ for (var i = 0; value >= pow; i++) {
110
+ bits[i] = (value / pow) | 0
111
+ pow *= Integer.TWO_PWR_32_DBL_
112
+ }
113
+ return new Integer(bits, 0)
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Returns a Integer representing the value that comes by concatenating the
119
+ * given entries, each is assumed to be 32 signed bits, given in little-endian
120
+ * order (lowest order bits in the lowest index), and sign-extending the highest
121
+ * order 32-bit value.
122
+ * @param {Array.<number>} bits The bits of the number, in 32-bit signed pieces,
123
+ * in little-endian order.
124
+ * @return {!Integer} The corresponding Integer value.
125
+ */
126
+ Integer.fromBits = function (bits) {
127
+ var high = bits[bits.length - 1]
128
+ //noinspection JSBitwiseOperatorUsage
129
+ return new Integer(bits, high & (1 << 31) ? -1 : 0)
130
+ }
131
+
132
+ /**
133
+ * Returns an Integer representation of the given string, written using the
134
+ * given radix.
135
+ * @param {string} str The textual representation of the Integer.
136
+ * @param {number=} opt_radix The radix in which the text is written.
137
+ * @return {!Integer} The corresponding Integer value.
138
+ */
139
+ Integer.fromString = function (str, opt_radix) {
140
+ if (str.length == 0) {
141
+ throw TypeError("number format error: empty string")
142
+ }
143
+
144
+ var radix = opt_radix || 10
145
+ if (radix < 2 || 36 < radix) {
146
+ throw Error("radix out of range: " + radix)
147
+ }
148
+
149
+ if (str.charAt(0) == "-") {
150
+ return Integer.fromString(str.substring(1), radix).negate()
151
+ } else if (str.indexOf("-") >= 0) {
152
+ throw TypeError('number format error: interior "-" character')
153
+ }
154
+
155
+ // Do several (8) digits each time through the loop, so as to
156
+ // minimize the calls to the very expensive emulated div.
157
+ var radixToPower = Integer.fromNumber(Math.pow(radix, 8))
158
+
159
+ var result = Integer.ZERO
160
+ for (var i = 0; i < str.length; i += 8) {
161
+ var size = Math.min(8, str.length - i)
162
+ var value = parseInt(str.substring(i, i + size), radix)
163
+ if (size < 8) {
164
+ var power = Integer.fromNumber(Math.pow(radix, size))
165
+ result = result.multiply(power).add(Integer.fromNumber(value))
166
+ } else {
167
+ result = result.multiply(radixToPower)
168
+ result = result.add(Integer.fromNumber(value))
169
+ }
170
+ }
171
+ return result
172
+ }
173
+
174
+ /**
175
+ * Returns an Integer representation of a given big endian Buffer.
176
+ * The internal representation of bits contains bytes in groups of 4
177
+ * @param {Buffer} buf
178
+ * @returns {Integer}
179
+ */
180
+ Integer.fromBuffer = function (buf) {
181
+ var bits = new Array(Math.ceil(buf.length / 4))
182
+ //noinspection JSBitwiseOperatorUsage
183
+ var sign = buf[0] & (1 << 7) ? -1 : 0
184
+ for (var i = 0; i < bits.length; i++) {
185
+ var offset = buf.length - (i + 1) * 4
186
+ var value
187
+ if (offset < 0) {
188
+ //The buffer length is not multiple of 4
189
+ offset = offset + 4
190
+ value = 0
191
+ for (var j = 0; j < offset; j++) {
192
+ var byte = buf[j]
193
+ if (sign === -1) {
194
+ //invert the bits
195
+ byte = ~byte & 0xff
196
+ }
197
+ value = value | (byte << ((offset - j - 1) * 8))
198
+ }
199
+ if (sign === -1) {
200
+ //invert all the bits
201
+ value = ~value
202
+ }
203
+ } else {
204
+ value = buf.readInt32BE(offset)
205
+ }
206
+ bits[i] = value
207
+ }
208
+ return new Integer(bits, sign)
209
+ }
210
+
211
+ /**
212
+ * Returns a big endian buffer representation of an Integer.
213
+ * Internally the bits are represented using 4 bytes groups (numbers),
214
+ * in the Buffer representation there might be the case where we need less than the 4 bytes.
215
+ * For example: 0x00000001 -> '01', 0xFFFFFFFF -> 'FF', 0xFFFFFF01 -> 'FF01'
216
+ * @param {Integer} value
217
+ * @returns {Buffer}
218
+ */
219
+ Integer.toBuffer = function (value) {
220
+ var sign = value.sign_
221
+ var bits = value.bits_
222
+ if (bits.length === 0) {
223
+ //[0] or [0xffffffff]
224
+ return utils.allocBufferFromArray([value.sign_])
225
+ }
226
+ //the high bits might need to be represented in less than 4 bytes
227
+ var highBits = bits[bits.length - 1]
228
+ if (sign === -1) {
229
+ highBits = ~highBits
230
+ }
231
+ var high = []
232
+ if (highBits >>> 24 > 0) {
233
+ high.push((highBits >> 24) & 0xff)
234
+ }
235
+ if (highBits >>> 16 > 0) {
236
+ high.push((highBits >> 16) & 0xff)
237
+ }
238
+ if (highBits >>> 8 > 0) {
239
+ high.push((highBits >> 8) & 0xff)
240
+ }
241
+ high.push(highBits & 0xff)
242
+ if (sign === -1) {
243
+ //The byte containing the sign bit got removed
244
+ if (high[0] >> 7 !== 0) {
245
+ //it is going to be negated
246
+ high.unshift(0)
247
+ }
248
+ } else if (high[0] >> 7 !== 0) {
249
+ //its positive but it lost the byte containing the sign bit
250
+ high.unshift(0)
251
+ }
252
+ var buf = utils.allocBufferUnsafe(high.length + (bits.length - 1) * 4)
253
+ for (var j = 0; j < high.length; j++) {
254
+ var b = high[j]
255
+ if (sign === -1) {
256
+ buf[j] = ~b
257
+ } else {
258
+ buf[j] = b
259
+ }
260
+ }
261
+ for (var i = 0; i < bits.length - 1; i++) {
262
+ var group = bits[bits.length - 2 - i]
263
+ var offset = high.length + i * 4
264
+ buf.writeInt32BE(group, offset)
265
+ }
266
+ return buf
267
+ }
268
+
269
+ /**
270
+ * A number used repeatedly in calculations. This must appear before the first
271
+ * call to the from* functions below.
272
+ * @type {number}
273
+ * @private
274
+ */
275
+ Integer.TWO_PWR_32_DBL_ = (1 << 16) * (1 << 16)
276
+
277
+ /** @type {!Integer} */
278
+ Integer.ZERO = Integer.fromInt(0)
279
+
280
+ /** @type {!Integer} */
281
+ Integer.ONE = Integer.fromInt(1)
282
+
283
+ /**
284
+ * @type {!Integer}
285
+ * @private
286
+ */
287
+ Integer.TWO_PWR_24_ = Integer.fromInt(1 << 24)
288
+
289
+ /**
290
+ * Returns the value, assuming it is a 32-bit integer.
291
+ * @return {number} The corresponding int value.
292
+ */
293
+ Integer.prototype.toInt = function () {
294
+ return this.bits_.length > 0 ? this.bits_[0] : this.sign_
295
+ }
296
+
297
+ /** @return {number} The closest floating-point representation to this value. */
298
+ Integer.prototype.toNumber = function () {
299
+ if (this.isNegative()) {
300
+ return -this.negate().toNumber()
301
+ } else {
302
+ var val = 0
303
+ var pow = 1
304
+ for (var i = 0; i < this.bits_.length; i++) {
305
+ val += this.getBitsUnsigned(i) * pow
306
+ pow *= Integer.TWO_PWR_32_DBL_
307
+ }
308
+ return val
309
+ }
310
+ }
311
+
312
+ /**
313
+ * @param {number=} opt_radix The radix in which the text should be written.
314
+ * @return {string} The textual representation of this value.
315
+ * @override
316
+ */
317
+ Integer.prototype.toString = function (opt_radix) {
318
+ var radix = opt_radix || 10
319
+ if (radix < 2 || 36 < radix) {
320
+ throw Error("radix out of range: " + radix)
321
+ }
322
+
323
+ if (this.isZero()) {
324
+ return "0"
325
+ } else if (this.isNegative()) {
326
+ return "-" + this.negate().toString(radix)
327
+ }
328
+
329
+ // Do several (6) digits each time through the loop, so as to
330
+ // minimize the calls to the very expensive emulated div.
331
+ var radixToPower = Integer.fromNumber(Math.pow(radix, 6))
332
+
333
+ var rem = this
334
+ var result = ""
335
+ while (true) {
336
+ var remDiv = rem.divide(radixToPower)
337
+ var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt()
338
+ var digits = intval.toString(radix)
339
+
340
+ rem = remDiv
341
+ if (rem.isZero()) {
342
+ return digits + result
343
+ } else {
344
+ while (digits.length < 6) {
345
+ digits = "0" + digits
346
+ }
347
+ result = "" + digits + result
348
+ }
349
+ }
350
+ }
351
+
352
+ /**
353
+ * Returns the index-th 32-bit (signed) piece of the Integer according to
354
+ * little-endian order (i.e., index 0 contains the smallest bits).
355
+ * @param {number} index The index in question.
356
+ * @return {number} The requested 32-bits as a signed number.
357
+ */
358
+ Integer.prototype.getBits = function (index) {
359
+ if (index < 0) {
360
+ return 0 // Allowing this simplifies bit shifting operations below...
361
+ } else if (index < this.bits_.length) {
362
+ return this.bits_[index]
363
+ } else {
364
+ return this.sign_
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Returns the index-th 32-bit piece as an unsigned number.
370
+ * @param {number} index The index in question.
371
+ * @return {number} The requested 32-bits as an unsigned number.
372
+ */
373
+ Integer.prototype.getBitsUnsigned = function (index) {
374
+ var val = this.getBits(index)
375
+ return val >= 0 ? val : Integer.TWO_PWR_32_DBL_ + val
376
+ }
377
+
378
+ /** @return {number} The sign bit of this number, -1 or 0. */
379
+ Integer.prototype.getSign = function () {
380
+ return this.sign_
381
+ }
382
+
383
+ /** @return {boolean} Whether this value is zero. */
384
+ Integer.prototype.isZero = function () {
385
+ if (this.sign_ != 0) {
386
+ return false
387
+ }
388
+ for (var i = 0; i < this.bits_.length; i++) {
389
+ if (this.bits_[i] != 0) {
390
+ return false
391
+ }
392
+ }
393
+ return true
394
+ }
395
+
396
+ /** @return {boolean} Whether this value is negative. */
397
+ Integer.prototype.isNegative = function () {
398
+ return this.sign_ == -1
399
+ }
400
+
401
+ /** @return {boolean} Whether this value is odd. */
402
+ Integer.prototype.isOdd = function () {
403
+ return (
404
+ (this.bits_.length == 0 && this.sign_ == -1) ||
405
+ (this.bits_.length > 0 && (this.bits_[0] & 1) != 0)
406
+ )
407
+ }
408
+
409
+ /**
410
+ * @param {Integer} other Integer to compare against.
411
+ * @return {boolean} Whether this Integer equals the other.
412
+ */
413
+ Integer.prototype.equals = function (other) {
414
+ if (this.sign_ != other.sign_) {
415
+ return false
416
+ }
417
+ var len = Math.max(this.bits_.length, other.bits_.length)
418
+ for (var i = 0; i < len; i++) {
419
+ if (this.getBits(i) != other.getBits(i)) {
420
+ return false
421
+ }
422
+ }
423
+ return true
424
+ }
425
+
426
+ /**
427
+ * @param {Integer} other Integer to compare against.
428
+ * @return {boolean} Whether this Integer does not equal the other.
429
+ */
430
+ Integer.prototype.notEquals = function (other) {
431
+ return !this.equals(other)
432
+ }
433
+
434
+ /**
435
+ * @param {Integer} other Integer to compare against.
436
+ * @return {boolean} Whether this Integer is greater than the other.
437
+ */
438
+ Integer.prototype.greaterThan = function (other) {
439
+ return this.compare(other) > 0
440
+ }
441
+
442
+ /**
443
+ * @param {Integer} other Integer to compare against.
444
+ * @return {boolean} Whether this Integer is greater than or equal to the other.
445
+ */
446
+ Integer.prototype.greaterThanOrEqual = function (other) {
447
+ return this.compare(other) >= 0
448
+ }
449
+
450
+ /**
451
+ * @param {Integer} other Integer to compare against.
452
+ * @return {boolean} Whether this Integer is less than the other.
453
+ */
454
+ Integer.prototype.lessThan = function (other) {
455
+ return this.compare(other) < 0
456
+ }
457
+
458
+ /**
459
+ * @param {Integer} other Integer to compare against.
460
+ * @return {boolean} Whether this Integer is less than or equal to the other.
461
+ */
462
+ Integer.prototype.lessThanOrEqual = function (other) {
463
+ return this.compare(other) <= 0
464
+ }
465
+
466
+ /**
467
+ * Compares this Integer with the given one.
468
+ * @param {Integer} other Integer to compare against.
469
+ * @return {number} 0 if they are the same, 1 if the this is greater, and -1
470
+ * if the given one is greater.
471
+ */
472
+ Integer.prototype.compare = function (other) {
473
+ var diff = this.subtract(other)
474
+ if (diff.isNegative()) {
475
+ return -1
476
+ } else if (diff.isZero()) {
477
+ return 0
478
+ } else {
479
+ return +1
480
+ }
481
+ }
482
+
483
+ /**
484
+ * Returns an integer with only the first numBits bits of this value, sign
485
+ * extended from the final bit.
486
+ * @param {number} numBits The number of bits by which to shift.
487
+ * @return {!Integer} The shorted integer value.
488
+ */
489
+ Integer.prototype.shorten = function (numBits) {
490
+ var arr_index = (numBits - 1) >> 5
491
+ var bit_index = (numBits - 1) % 32
492
+ var bits = []
493
+ for (var i = 0; i < arr_index; i++) {
494
+ bits[i] = this.getBits(i)
495
+ }
496
+ var sigBits = bit_index == 31 ? 0xffffffff : (1 << (bit_index + 1)) - 1
497
+ var val = this.getBits(arr_index) & sigBits
498
+ //noinspection JSBitwiseOperatorUsage
499
+ if (val & (1 << bit_index)) {
500
+ val |= 0xffffffff - sigBits
501
+ bits[arr_index] = val
502
+ return new Integer(bits, -1)
503
+ } else {
504
+ bits[arr_index] = val
505
+ return new Integer(bits, 0)
506
+ }
507
+ }
508
+
509
+ /** @return {!Integer} The negation of this value. */
510
+ Integer.prototype.negate = function () {
511
+ return this.not().add(Integer.ONE)
512
+ }
513
+
514
+ /**
515
+ * Returns the sum of this and the given Integer.
516
+ * @param {Integer} other The Integer to add to this.
517
+ * @return {!Integer} The Integer result.
518
+ */
519
+ Integer.prototype.add = function (other) {
520
+ var len = Math.max(this.bits_.length, other.bits_.length)
521
+ var arr = []
522
+ var carry = 0
523
+
524
+ for (var i = 0; i <= len; i++) {
525
+ var a1 = this.getBits(i) >>> 16
526
+ var a0 = this.getBits(i) & 0xffff
527
+
528
+ var b1 = other.getBits(i) >>> 16
529
+ var b0 = other.getBits(i) & 0xffff
530
+
531
+ var c0 = carry + a0 + b0
532
+ var c1 = (c0 >>> 16) + a1 + b1
533
+ carry = c1 >>> 16
534
+ c0 &= 0xffff
535
+ c1 &= 0xffff
536
+ arr[i] = (c1 << 16) | c0
537
+ }
538
+ return Integer.fromBits(arr)
539
+ }
540
+
541
+ /**
542
+ * Returns the difference of this and the given Integer.
543
+ * @param {Integer} other The Integer to subtract from this.
544
+ * @return {!Integer} The Integer result.
545
+ */
546
+ Integer.prototype.subtract = function (other) {
547
+ return this.add(other.negate())
548
+ }
549
+
550
+ /**
551
+ * Returns the product of this and the given Integer.
552
+ * @param {Integer} other The Integer to multiply against this.
553
+ * @return {!Integer} The product of this and the other.
554
+ */
555
+ Integer.prototype.multiply = function (other) {
556
+ if (this.isZero()) {
557
+ return Integer.ZERO
558
+ } else if (other.isZero()) {
559
+ return Integer.ZERO
560
+ }
561
+
562
+ if (this.isNegative()) {
563
+ if (other.isNegative()) {
564
+ return this.negate().multiply(other.negate())
565
+ } else {
566
+ return this.negate().multiply(other).negate()
567
+ }
568
+ } else if (other.isNegative()) {
569
+ return this.multiply(other.negate()).negate()
570
+ }
571
+
572
+ // If both numbers are small, use float multiplication
573
+ if (
574
+ this.lessThan(Integer.TWO_PWR_24_) &&
575
+ other.lessThan(Integer.TWO_PWR_24_)
576
+ ) {
577
+ return Integer.fromNumber(this.toNumber() * other.toNumber())
578
+ }
579
+
580
+ // Fill in an array of 16-bit products.
581
+ var len = this.bits_.length + other.bits_.length
582
+ var arr = []
583
+ for (var i = 0; i < 2 * len; i++) {
584
+ arr[i] = 0
585
+ }
586
+ for (var i = 0; i < this.bits_.length; i++) {
587
+ for (var j = 0; j < other.bits_.length; j++) {
588
+ var a1 = this.getBits(i) >>> 16
589
+ var a0 = this.getBits(i) & 0xffff
590
+
591
+ var b1 = other.getBits(j) >>> 16
592
+ var b0 = other.getBits(j) & 0xffff
593
+
594
+ arr[2 * i + 2 * j] += a0 * b0
595
+ Integer.carry16_(arr, 2 * i + 2 * j)
596
+ arr[2 * i + 2 * j + 1] += a1 * b0
597
+ Integer.carry16_(arr, 2 * i + 2 * j + 1)
598
+ arr[2 * i + 2 * j + 1] += a0 * b1
599
+ Integer.carry16_(arr, 2 * i + 2 * j + 1)
600
+ arr[2 * i + 2 * j + 2] += a1 * b1
601
+ Integer.carry16_(arr, 2 * i + 2 * j + 2)
602
+ }
603
+ }
604
+
605
+ // Combine the 16-bit values into 32-bit values.
606
+ for (var i = 0; i < len; i++) {
607
+ arr[i] = (arr[2 * i + 1] << 16) | arr[2 * i]
608
+ }
609
+ for (var i = len; i < 2 * len; i++) {
610
+ arr[i] = 0
611
+ }
612
+ return new Integer(arr, 0)
613
+ }
614
+
615
+ /**
616
+ * Carries any overflow from the given index into later entries.
617
+ * @param {Array.<number>} bits Array of 16-bit values in little-endian order.
618
+ * @param {number} index The index in question.
619
+ * @private
620
+ */
621
+ Integer.carry16_ = function (bits, index) {
622
+ while ((bits[index] & 0xffff) != bits[index]) {
623
+ bits[index + 1] += bits[index] >>> 16
624
+ bits[index] &= 0xffff
625
+ }
626
+ }
627
+
628
+ /**
629
+ * Returns this Integer divided by the given one.
630
+ * @param {Integer} other Th Integer to divide this by.
631
+ * @return {!Integer} This value divided by the given one.
632
+ */
633
+ Integer.prototype.divide = function (other) {
634
+ if (other.isZero()) {
635
+ throw Error("division by zero")
636
+ } else if (this.isZero()) {
637
+ return Integer.ZERO
638
+ }
639
+
640
+ if (this.isNegative()) {
641
+ if (other.isNegative()) {
642
+ return this.negate().divide(other.negate())
643
+ } else {
644
+ return this.negate().divide(other).negate()
645
+ }
646
+ } else if (other.isNegative()) {
647
+ return this.divide(other.negate()).negate()
648
+ }
649
+
650
+ // Repeat the following until the remainder is less than other: find a
651
+ // floating-point that approximates remainder / other *from below*, add this
652
+ // into the result, and subtract it from the remainder. It is critical that
653
+ // the approximate value is less than or equal to the real value so that the
654
+ // remainder never becomes negative.
655
+ var res = Integer.ZERO
656
+ var rem = this
657
+ while (rem.greaterThanOrEqual(other)) {
658
+ // Approximate the result of division. This may be a little greater or
659
+ // smaller than the actual value.
660
+ var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()))
661
+
662
+ // We will tweak the approximate result by changing it in the 48-th digit or
663
+ // the smallest non-fractional digit, whichever is larger.
664
+ var log2 = Math.ceil(Math.log(approx) / Math.LN2)
665
+ var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48)
666
+
667
+ // Decrease the approximation until it is smaller than the remainder. Note
668
+ // that if it is too large, the product overflows and is negative.
669
+ var approxRes = Integer.fromNumber(approx)
670
+ var approxRem = approxRes.multiply(other)
671
+ while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
672
+ approx -= delta
673
+ approxRes = Integer.fromNumber(approx)
674
+ approxRem = approxRes.multiply(other)
675
+ }
676
+
677
+ // We know the answer can't be zero... and actually, zero would cause
678
+ // infinite recursion since we would make no progress.
679
+ if (approxRes.isZero()) {
680
+ approxRes = Integer.ONE
681
+ }
682
+
683
+ res = res.add(approxRes)
684
+ rem = rem.subtract(approxRem)
685
+ }
686
+ return res
687
+ }
688
+
689
+ /**
690
+ * Returns this Integer modulo the given one.
691
+ * @param {Integer} other The Integer by which to mod.
692
+ * @return {!Integer} This value modulo the given one.
693
+ */
694
+ Integer.prototype.modulo = function (other) {
695
+ return this.subtract(this.divide(other).multiply(other))
696
+ }
697
+
698
+ /** @return {!Integer} The bitwise-NOT of this value. */
699
+ Integer.prototype.not = function () {
700
+ var len = this.bits_.length
701
+ var arr = []
702
+ for (var i = 0; i < len; i++) {
703
+ arr[i] = ~this.bits_[i]
704
+ }
705
+ return new Integer(arr, ~this.sign_)
706
+ }
707
+
708
+ /**
709
+ * Returns the bitwise-AND of this Integer and the given one.
710
+ * @param {Integer} other The Integer to AND with this.
711
+ * @return {!Integer} The bitwise-AND of this and the other.
712
+ */
713
+ Integer.prototype.and = function (other) {
714
+ var len = Math.max(this.bits_.length, other.bits_.length)
715
+ var arr = []
716
+ for (var i = 0; i < len; i++) {
717
+ arr[i] = this.getBits(i) & other.getBits(i)
718
+ }
719
+ return new Integer(arr, this.sign_ & other.sign_)
720
+ }
721
+
722
+ /**
723
+ * Returns the bitwise-OR of this Integer and the given one.
724
+ * @param {Integer} other The Integer to OR with this.
725
+ * @return {!Integer} The bitwise-OR of this and the other.
726
+ */
727
+ Integer.prototype.or = function (other) {
728
+ var len = Math.max(this.bits_.length, other.bits_.length)
729
+ var arr = []
730
+ for (var i = 0; i < len; i++) {
731
+ arr[i] = this.getBits(i) | other.getBits(i)
732
+ }
733
+ return new Integer(arr, this.sign_ | other.sign_)
734
+ }
735
+
736
+ /**
737
+ * Returns the bitwise-XOR of this Integer and the given one.
738
+ * @param {Integer} other The Integer to XOR with this.
739
+ * @return {!Integer} The bitwise-XOR of this and the other.
740
+ */
741
+ Integer.prototype.xor = function (other) {
742
+ var len = Math.max(this.bits_.length, other.bits_.length)
743
+ var arr = []
744
+ for (var i = 0; i < len; i++) {
745
+ arr[i] = this.getBits(i) ^ other.getBits(i)
746
+ }
747
+ return new Integer(arr, this.sign_ ^ other.sign_)
748
+ }
749
+
750
+ /**
751
+ * Returns this value with bits shifted to the left by the given amount.
752
+ * @param {number} numBits The number of bits by which to shift.
753
+ * @return {!Integer} This shifted to the left by the given amount.
754
+ */
755
+ Integer.prototype.shiftLeft = function (numBits) {
756
+ var arr_delta = numBits >> 5
757
+ var bit_delta = numBits % 32
758
+ var len = this.bits_.length + arr_delta + (bit_delta > 0 ? 1 : 0)
759
+ var arr = []
760
+ for (var i = 0; i < len; i++) {
761
+ if (bit_delta > 0) {
762
+ arr[i] =
763
+ (this.getBits(i - arr_delta) << bit_delta) |
764
+ (this.getBits(i - arr_delta - 1) >>> (32 - bit_delta))
765
+ } else {
766
+ arr[i] = this.getBits(i - arr_delta)
767
+ }
768
+ }
769
+ return new Integer(arr, this.sign_)
770
+ }
771
+
772
+ /**
773
+ * Returns this value with bits shifted to the right by the given amount.
774
+ * @param {number} numBits The number of bits by which to shift.
775
+ * @return {!Integer} This shifted to the right by the given amount.
776
+ */
777
+ Integer.prototype.shiftRight = function (numBits) {
778
+ var arr_delta = numBits >> 5
779
+ var bit_delta = numBits % 32
780
+ var len = this.bits_.length - arr_delta
781
+ var arr = []
782
+ for (var i = 0; i < len; i++) {
783
+ if (bit_delta > 0) {
784
+ arr[i] =
785
+ (this.getBits(i + arr_delta) >>> bit_delta) |
786
+ (this.getBits(i + arr_delta + 1) << (32 - bit_delta))
787
+ } else {
788
+ arr[i] = this.getBits(i + arr_delta)
789
+ }
790
+ }
791
+ return new Integer(arr, this.sign_)
792
+ }
793
+
794
+ /**
795
+ * Provide the name of the constructor and the string representation
796
+ * @returns {string}
797
+ */
798
+ Integer.prototype.inspect = function () {
799
+ return this.constructor.name + ": " + this.toString()
800
+ }
801
+
802
+ /**
803
+ * Returns a Integer whose value is the absolute value of this
804
+ * @returns {Integer}
805
+ */
806
+ Integer.prototype.abs = function () {
807
+ return this.sign_ === 0 ? this : this.negate()
808
+ }
809
+
810
+ /**
811
+ * Returns the string representation.
812
+ * Method used by the native JSON.stringify() to serialize this instance.
813
+ */
814
+ Integer.prototype.toJSON = function () {
815
+ return this.toString()
816
+ }
817
+
818
+ export default Integer