@ragestudio/scylla-odm 0.22.2 → 0.22.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.
- package/batch/index.d.ts +3 -3
- package/batch/index.d.ts.map +1 -1
- package/client.d.ts +6 -5
- package/client.d.ts.map +1 -1
- package/client.js +7 -7
- package/client.js.map +1 -1
- package/cql_gen/create_table.d.ts +1 -1
- package/cql_gen/create_table.d.ts.map +1 -1
- package/document/index.d.ts +3 -3
- package/document/index.d.ts.map +1 -1
- package/driver/LICENSE.txt +177 -0
- package/driver/NOTICE.txt +67 -0
- package/driver/auth/index.d.ts +37 -0
- package/driver/auth/index.js +37 -0
- package/driver/auth/no-auth-provider.js +73 -0
- package/driver/auth/plain-text-auth-provider.js +81 -0
- package/driver/auth/provider.js +77 -0
- package/driver/client-options.js +442 -0
- package/driver/client.js +1267 -0
- package/driver/concurrent/index.d.ts +49 -0
- package/driver/concurrent/index.js +366 -0
- package/driver/connection.js +1034 -0
- package/driver/control-connection.js +1282 -0
- package/driver/encoder.js +2316 -0
- package/driver/errors.js +223 -0
- package/driver/execution-options.js +612 -0
- package/driver/execution-profile.js +274 -0
- package/driver/host-connection-pool.js +587 -0
- package/driver/host.js +699 -0
- package/driver/index.d.ts +387 -0
- package/driver/index.js +81 -0
- package/driver/mapping/cache.js +214 -0
- package/driver/mapping/doc-info-adapter.js +171 -0
- package/driver/mapping/index.d.ts +219 -0
- package/driver/mapping/index.js +57 -0
- package/driver/mapping/mapper.js +225 -0
- package/driver/mapping/mapping-handler.js +641 -0
- package/driver/mapping/model-batch-item.js +215 -0
- package/driver/mapping/model-batch-mapper.js +141 -0
- package/driver/mapping/model-mapper.js +315 -0
- package/driver/mapping/model-mapping-info.js +225 -0
- package/driver/mapping/object-selector.js +417 -0
- package/driver/mapping/q.js +156 -0
- package/driver/mapping/query-generator.js +556 -0
- package/driver/mapping/result-mapper.js +123 -0
- package/driver/mapping/result.js +139 -0
- package/driver/mapping/table-mappings.js +133 -0
- package/driver/mapping/tree.js +160 -0
- package/driver/metadata/aggregate.js +79 -0
- package/driver/metadata/client-state.js +119 -0
- package/driver/metadata/data-collection.js +182 -0
- package/driver/metadata/event-debouncer.js +174 -0
- package/driver/metadata/index.d.ts +276 -0
- package/driver/metadata/index.js +1156 -0
- package/driver/metadata/materialized-view.js +49 -0
- package/driver/metadata/schema-function.js +98 -0
- package/driver/metadata/schema-index.js +166 -0
- package/driver/metadata/schema-parser.js +1399 -0
- package/driver/metadata/table-metadata.js +77 -0
- package/driver/operation-state.js +206 -0
- package/driver/policies/address-resolution.js +145 -0
- package/driver/policies/index.d.ts +241 -0
- package/driver/policies/index.js +110 -0
- package/driver/policies/load-balancing.js +970 -0
- package/driver/policies/reconnection.js +166 -0
- package/driver/policies/retry.js +326 -0
- package/driver/policies/speculative-execution.js +150 -0
- package/driver/policies/timestamp-generation.js +176 -0
- package/driver/prepare-handler.js +347 -0
- package/driver/promise-utils.js +191 -0
- package/driver/readers.js +624 -0
- package/driver/request-execution.js +644 -0
- package/driver/request-handler.js +332 -0
- package/driver/requests.js +618 -0
- package/driver/stream-id-stack.js +209 -0
- package/driver/streams.js +745 -0
- package/driver/token.js +325 -0
- package/driver/tokenizer.js +631 -0
- package/driver/types/big-decimal.js +282 -0
- package/driver/types/duration.js +576 -0
- package/driver/types/index.d.ts +486 -0
- package/driver/types/index.js +733 -0
- package/driver/types/inet-address.js +262 -0
- package/driver/types/integer.js +818 -0
- package/driver/types/local-date.js +280 -0
- package/driver/types/local-time.js +299 -0
- package/driver/types/mutable-long.js +385 -0
- package/driver/types/protocol-version.js +391 -0
- package/driver/types/result-set.js +287 -0
- package/driver/types/result-stream.js +164 -0
- package/driver/types/row.js +85 -0
- package/driver/types/time-uuid.js +414 -0
- package/driver/types/tuple.js +103 -0
- package/driver/types/uuid.js +160 -0
- package/driver/types/vector.js +130 -0
- package/driver/types/version-number.js +153 -0
- package/driver/utils.js +1485 -0
- package/driver/writers.js +350 -0
- package/global.d.ts +1 -1
- package/global.d.ts.map +1 -1
- package/index.d.ts +6 -6
- package/index.d.ts.map +1 -1
- package/index.js +6 -6
- package/index.js.map +1 -1
- package/migrate/index.d.ts +1 -1
- package/migrate/index.d.ts.map +1 -1
- package/migrate/index.js +1 -1
- package/migrate/index.js.map +1 -1
- package/model/index.d.ts +6 -6
- package/model/index.d.ts.map +1 -1
- package/model/index.js +10 -10
- package/model/index.js.map +1 -1
- package/operations/countAll.d.ts +1 -1
- package/operations/countAll.d.ts.map +1 -1
- package/operations/delete.d.ts +3 -4
- package/operations/delete.d.ts.map +1 -1
- package/operations/delete.js +1 -1
- package/operations/delete.js.map +1 -1
- package/operations/find.d.ts +2 -2
- package/operations/find.d.ts.map +1 -1
- package/operations/find.js +1 -1
- package/operations/find.js.map +1 -1
- package/operations/findOne.d.ts +2 -2
- package/operations/findOne.d.ts.map +1 -1
- package/operations/findOne.js +1 -1
- package/operations/findOne.js.map +1 -1
- package/operations/insert.d.ts +3 -3
- package/operations/insert.d.ts.map +1 -1
- package/operations/insert.js +2 -2
- package/operations/insert.js.map +1 -1
- package/operations/sync.d.ts +1 -1
- package/operations/sync.d.ts.map +1 -1
- package/operations/sync.js +1 -1
- package/operations/sync.js.map +1 -1
- package/operations/tableExists.d.ts +1 -1
- package/operations/tableExists.d.ts.map +1 -1
- package/operations/update.d.ts +3 -3
- package/operations/update.d.ts.map +1 -1
- package/operations/update.js +2 -2
- package/operations/update.js.map +1 -1
- package/package.json +4 -12
- package/schema/index.d.ts +1 -1
- package/schema/index.d.ts.map +1 -1
- package/types.d.ts +4 -4
- package/types.d.ts.map +1 -1
- package/utils/queryParser.d.ts +1 -1
- package/utils/queryParser.d.ts.map +1 -1
- package/utils/queryParser.js +1 -1
- package/utils/queryParser.js.map +1 -1
- package/utils/typeChecker.d.ts +1 -1
- package/utils/typeChecker.d.ts.map +1 -1
- package/utils/typeChecker.js +1 -1
- package/utils/typeChecker.js.map +1 -1
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
import Long from "long"
|
|
19
|
+
import util from "util"
|
|
20
|
+
import utils from "../utils.js"
|
|
21
|
+
|
|
22
|
+
const VIntCoding = utils.VIntCoding
|
|
23
|
+
|
|
24
|
+
/** @module types */
|
|
25
|
+
|
|
26
|
+
// Reuse the same buffers that should perform slightly better than built-in buffer pool
|
|
27
|
+
const reusableBuffers = {
|
|
28
|
+
months: utils.allocBuffer(9),
|
|
29
|
+
days: utils.allocBuffer(9),
|
|
30
|
+
nanoseconds: utils.allocBuffer(9),
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const maxInt32 = 0x7fffffff
|
|
34
|
+
const longOneThousand = Long.fromInt(1000)
|
|
35
|
+
const nanosPerMicro = longOneThousand
|
|
36
|
+
const nanosPerMilli = longOneThousand.multiply(nanosPerMicro)
|
|
37
|
+
const nanosPerSecond = longOneThousand.multiply(nanosPerMilli)
|
|
38
|
+
const nanosPerMinute = Long.fromInt(60).multiply(nanosPerSecond)
|
|
39
|
+
const nanosPerHour = Long.fromInt(60).multiply(nanosPerMinute)
|
|
40
|
+
const daysPerWeek = 7
|
|
41
|
+
const monthsPerYear = 12
|
|
42
|
+
const standardRegex = /(\d+)(y|mo|w|d|h|s|ms|us|µs|ns|m)/gi
|
|
43
|
+
const iso8601Regex =
|
|
44
|
+
/P((\d+)Y)?((\d+)M)?((\d+)D)?(T((\d+)H)?((\d+)M)?((\d+)S)?)?/
|
|
45
|
+
const iso8601WeekRegex = /P(\d+)W/
|
|
46
|
+
const iso8601AlternateRegex = /P(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new instance of {@link Duration}.
|
|
50
|
+
* @classdesc
|
|
51
|
+
* Represents a duration. A duration stores separately months, days, and seconds due to the fact that the number of
|
|
52
|
+
* days in a month varies, and a day can have 23 or 25 hours if a daylight saving is involved.
|
|
53
|
+
* @param {Number} months The number of months.
|
|
54
|
+
* @param {Number} days The number of days.
|
|
55
|
+
* @param {Number|Long} nanoseconds The number of nanoseconds.
|
|
56
|
+
* @constructor
|
|
57
|
+
*/
|
|
58
|
+
function Duration(months, days, nanoseconds) {
|
|
59
|
+
/**
|
|
60
|
+
* Gets the number of months.
|
|
61
|
+
* @type {Number}
|
|
62
|
+
*/
|
|
63
|
+
this.months = months
|
|
64
|
+
/**
|
|
65
|
+
* Gets the number of days.
|
|
66
|
+
* @type {Number}
|
|
67
|
+
*/
|
|
68
|
+
this.days = days
|
|
69
|
+
/**
|
|
70
|
+
* Gets the number of nanoseconds represented as a <code>int64</code>.
|
|
71
|
+
* @type {Long}
|
|
72
|
+
*/
|
|
73
|
+
this.nanoseconds =
|
|
74
|
+
typeof nanoseconds === "number"
|
|
75
|
+
? Long.fromNumber(nanoseconds)
|
|
76
|
+
: nanoseconds
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
Duration.prototype.equals = function (other) {
|
|
80
|
+
if (!(other instanceof Duration)) {
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
return (
|
|
84
|
+
this.months === other.months &&
|
|
85
|
+
this.days === other.days &&
|
|
86
|
+
this.nanoseconds.equals(other.nanoseconds)
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Serializes the duration and returns the representation of the value in bytes.
|
|
92
|
+
* @returns {Buffer}
|
|
93
|
+
*/
|
|
94
|
+
Duration.prototype.toBuffer = function () {
|
|
95
|
+
const lengthMonths = VIntCoding.writeVInt(
|
|
96
|
+
Long.fromNumber(this.months),
|
|
97
|
+
reusableBuffers.months,
|
|
98
|
+
)
|
|
99
|
+
const lengthDays = VIntCoding.writeVInt(
|
|
100
|
+
Long.fromNumber(this.days),
|
|
101
|
+
reusableBuffers.days,
|
|
102
|
+
)
|
|
103
|
+
const lengthNanoseconds = VIntCoding.writeVInt(
|
|
104
|
+
this.nanoseconds,
|
|
105
|
+
reusableBuffers.nanoseconds,
|
|
106
|
+
)
|
|
107
|
+
const buffer = utils.allocBufferUnsafe(
|
|
108
|
+
lengthMonths + lengthDays + lengthNanoseconds,
|
|
109
|
+
)
|
|
110
|
+
reusableBuffers.months.copy(buffer, 0, 0, lengthMonths)
|
|
111
|
+
let offset = lengthMonths
|
|
112
|
+
reusableBuffers.days.copy(buffer, offset, 0, lengthDays)
|
|
113
|
+
offset += lengthDays
|
|
114
|
+
reusableBuffers.nanoseconds.copy(buffer, offset, 0, lengthNanoseconds)
|
|
115
|
+
return buffer
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Returns the string representation of the value.
|
|
120
|
+
* @return {string}
|
|
121
|
+
*/
|
|
122
|
+
Duration.prototype.toString = function () {
|
|
123
|
+
let value = ""
|
|
124
|
+
function append(dividend, divisor, unit) {
|
|
125
|
+
if (dividend === 0 || dividend < divisor) {
|
|
126
|
+
return dividend
|
|
127
|
+
}
|
|
128
|
+
// string concatenation is supposed to be fasted than join()
|
|
129
|
+
value += (dividend / divisor).toFixed(0) + unit
|
|
130
|
+
return dividend % divisor
|
|
131
|
+
}
|
|
132
|
+
function append64(dividend, divisor, unit) {
|
|
133
|
+
if (dividend.equals(Long.ZERO) || dividend.lessThan(divisor)) {
|
|
134
|
+
return dividend
|
|
135
|
+
}
|
|
136
|
+
// string concatenation is supposed to be fasted than join()
|
|
137
|
+
value += dividend.divide(divisor).toString() + unit
|
|
138
|
+
return dividend.modulo(divisor)
|
|
139
|
+
}
|
|
140
|
+
if (this.months < 0 || this.days < 0 || this.nanoseconds.isNegative()) {
|
|
141
|
+
value = "-"
|
|
142
|
+
}
|
|
143
|
+
let remainder = append(Math.abs(this.months), monthsPerYear, "y")
|
|
144
|
+
append(remainder, 1, "mo")
|
|
145
|
+
append(Math.abs(this.days), 1, "d")
|
|
146
|
+
|
|
147
|
+
if (!this.nanoseconds.equals(Long.ZERO)) {
|
|
148
|
+
const nanos = this.nanoseconds.isNegative()
|
|
149
|
+
? this.nanoseconds.negate()
|
|
150
|
+
: this.nanoseconds
|
|
151
|
+
remainder = append64(nanos, nanosPerHour, "h")
|
|
152
|
+
remainder = append64(remainder, nanosPerMinute, "m")
|
|
153
|
+
remainder = append64(remainder, nanosPerSecond, "s")
|
|
154
|
+
remainder = append64(remainder, nanosPerMilli, "ms")
|
|
155
|
+
remainder = append64(remainder, nanosPerMicro, "us")
|
|
156
|
+
append64(remainder, Long.ONE, "ns")
|
|
157
|
+
}
|
|
158
|
+
return value
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Creates a new {@link Duration} instance from the binary representation of the value.
|
|
163
|
+
* @param {Buffer} buffer
|
|
164
|
+
* @returns {Duration}
|
|
165
|
+
*/
|
|
166
|
+
Duration.fromBuffer = function (buffer) {
|
|
167
|
+
const offset = { value: 0 }
|
|
168
|
+
const months = VIntCoding.readVInt(buffer, offset).toNumber()
|
|
169
|
+
const days = VIntCoding.readVInt(buffer, offset).toNumber()
|
|
170
|
+
const nanoseconds = VIntCoding.readVInt(buffer, offset)
|
|
171
|
+
return new Duration(months, days, nanoseconds)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Creates a new {@link Duration} instance from the string representation of the value.
|
|
176
|
+
* <p>
|
|
177
|
+
* Accepted formats:
|
|
178
|
+
* </p>
|
|
179
|
+
* <ul>
|
|
180
|
+
* <li>multiple digits followed by a time unit like: 12h30m where the time unit can be:
|
|
181
|
+
* <ul>
|
|
182
|
+
* <li>{@code y}: years</li>
|
|
183
|
+
* <li>{@code m}: months</li>
|
|
184
|
+
* <li>{@code w}: weeks</li>
|
|
185
|
+
* <li>{@code d}: days</li>
|
|
186
|
+
* <li>{@code h}: hours</li>
|
|
187
|
+
* <li>{@code m}: minutes</li>
|
|
188
|
+
* <li>{@code s}: seconds</li>
|
|
189
|
+
* <li>{@code ms}: milliseconds</li>
|
|
190
|
+
* <li>{@code us} or {@code µs}: microseconds</li>
|
|
191
|
+
* <li>{@code ns}: nanoseconds</li>
|
|
192
|
+
* </ul>
|
|
193
|
+
* </li>
|
|
194
|
+
* <li>ISO 8601 format: <code>P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W</code></li>
|
|
195
|
+
* <li>ISO 8601 alternative format: <code>P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]</code></li>
|
|
196
|
+
* </ul>
|
|
197
|
+
* @param {String} input
|
|
198
|
+
* @returns {Duration}
|
|
199
|
+
*/
|
|
200
|
+
Duration.fromString = function (input) {
|
|
201
|
+
const isNegative = input.charAt(0) === "-"
|
|
202
|
+
const source = isNegative ? input.substr(1) : input
|
|
203
|
+
if (source.charAt(0) === "P") {
|
|
204
|
+
if (source.charAt(source.length - 1) === "W") {
|
|
205
|
+
return parseIso8601WeekFormat(isNegative, source)
|
|
206
|
+
}
|
|
207
|
+
if (source.indexOf("-") > 0) {
|
|
208
|
+
return parseIso8601AlternativeFormat(isNegative, source)
|
|
209
|
+
}
|
|
210
|
+
return parseIso8601Format(isNegative, source)
|
|
211
|
+
}
|
|
212
|
+
return parseStandardFormat(isNegative, source)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {Boolean} isNegative
|
|
217
|
+
* @param {String} source
|
|
218
|
+
* @returns {Duration}
|
|
219
|
+
* @private
|
|
220
|
+
*/
|
|
221
|
+
function parseStandardFormat(isNegative, source) {
|
|
222
|
+
const builder = new Builder(isNegative)
|
|
223
|
+
standardRegex.lastIndex = 0
|
|
224
|
+
let matches
|
|
225
|
+
while ((matches = standardRegex.exec(source)) && matches.length <= 3) {
|
|
226
|
+
builder.add(matches[1], matches[2])
|
|
227
|
+
}
|
|
228
|
+
return builder.build()
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @param {Boolean} isNegative
|
|
233
|
+
* @param {String} source
|
|
234
|
+
* @returns {Duration}
|
|
235
|
+
* @private
|
|
236
|
+
*/
|
|
237
|
+
function parseIso8601Format(isNegative, source) {
|
|
238
|
+
const matches = iso8601Regex.exec(source)
|
|
239
|
+
if (!matches || matches[0] !== source) {
|
|
240
|
+
throw new TypeError(
|
|
241
|
+
util.format("Unable to convert '%s' to a duration", source),
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
const builder = new Builder(isNegative)
|
|
245
|
+
if (matches[1]) {
|
|
246
|
+
builder.addYears(matches[2])
|
|
247
|
+
}
|
|
248
|
+
if (matches[3]) {
|
|
249
|
+
builder.addMonths(matches[4])
|
|
250
|
+
}
|
|
251
|
+
if (matches[5]) {
|
|
252
|
+
builder.addDays(matches[6])
|
|
253
|
+
}
|
|
254
|
+
if (matches[7]) {
|
|
255
|
+
if (matches[8]) {
|
|
256
|
+
builder.addHours(matches[9])
|
|
257
|
+
}
|
|
258
|
+
if (matches[10]) {
|
|
259
|
+
builder.addMinutes(matches[11])
|
|
260
|
+
}
|
|
261
|
+
if (matches[12]) {
|
|
262
|
+
builder.addSeconds(matches[13])
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return builder.build()
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* @param {Boolean} isNegative
|
|
270
|
+
* @param {String} source
|
|
271
|
+
* @returns {Duration}
|
|
272
|
+
* @private
|
|
273
|
+
*/
|
|
274
|
+
function parseIso8601WeekFormat(isNegative, source) {
|
|
275
|
+
const matches = iso8601WeekRegex.exec(source)
|
|
276
|
+
if (!matches || matches[0] !== source) {
|
|
277
|
+
throw new TypeError(
|
|
278
|
+
util.format("Unable to convert '%s' to a duration", source),
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
return new Builder(isNegative).addWeeks(matches[1]).build()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @param {Boolean} isNegative
|
|
286
|
+
* @param {String} source
|
|
287
|
+
* @returns {Duration}
|
|
288
|
+
* @private
|
|
289
|
+
*/
|
|
290
|
+
function parseIso8601AlternativeFormat(isNegative, source) {
|
|
291
|
+
const matches = iso8601AlternateRegex.exec(source)
|
|
292
|
+
if (!matches || matches[0] !== source) {
|
|
293
|
+
throw new TypeError(
|
|
294
|
+
util.format("Unable to convert '%s' to a duration", source),
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
return new Builder(isNegative)
|
|
298
|
+
.addYears(matches[1])
|
|
299
|
+
.addMonths(matches[2])
|
|
300
|
+
.addDays(matches[3])
|
|
301
|
+
.addHours(matches[4])
|
|
302
|
+
.addMinutes(matches[5])
|
|
303
|
+
.addSeconds(matches[6])
|
|
304
|
+
.build()
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* @param {Boolean} isNegative
|
|
309
|
+
* @private
|
|
310
|
+
* @constructor
|
|
311
|
+
*/
|
|
312
|
+
function Builder(isNegative) {
|
|
313
|
+
this._isNegative = isNegative
|
|
314
|
+
this._unitIndex = 0
|
|
315
|
+
this._months = 0
|
|
316
|
+
this._days = 0
|
|
317
|
+
this._nanoseconds = Long.ZERO
|
|
318
|
+
this._addMethods = {
|
|
319
|
+
y: this.addYears,
|
|
320
|
+
mo: this.addMonths,
|
|
321
|
+
w: this.addWeeks,
|
|
322
|
+
d: this.addDays,
|
|
323
|
+
h: this.addHours,
|
|
324
|
+
m: this.addMinutes,
|
|
325
|
+
s: this.addSeconds,
|
|
326
|
+
ms: this.addMillis,
|
|
327
|
+
// µs
|
|
328
|
+
"\u00B5s": this.addMicros,
|
|
329
|
+
us: this.addMicros,
|
|
330
|
+
ns: this.addNanos,
|
|
331
|
+
}
|
|
332
|
+
this._unitByIndex = [
|
|
333
|
+
null,
|
|
334
|
+
"years",
|
|
335
|
+
"months",
|
|
336
|
+
"weeks",
|
|
337
|
+
"days",
|
|
338
|
+
"hours",
|
|
339
|
+
"minutes",
|
|
340
|
+
"seconds",
|
|
341
|
+
"milliseconds",
|
|
342
|
+
"microseconds",
|
|
343
|
+
"nanoseconds",
|
|
344
|
+
]
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
Builder.prototype._validateOrder = function (unitIndex) {
|
|
348
|
+
if (unitIndex === this._unitIndex) {
|
|
349
|
+
throw new TypeError(
|
|
350
|
+
util.format(
|
|
351
|
+
"Invalid duration. The %s are specified multiple times",
|
|
352
|
+
this._getUnitName(unitIndex),
|
|
353
|
+
),
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (unitIndex <= this._unitIndex) {
|
|
358
|
+
throw new TypeError(
|
|
359
|
+
util.format(
|
|
360
|
+
"Invalid duration. The %s should be after %s",
|
|
361
|
+
this._getUnitName(this._unitIndex),
|
|
362
|
+
this._getUnitName(unitIndex),
|
|
363
|
+
),
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
this._unitIndex = unitIndex
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* @param {Number} units
|
|
371
|
+
* @param {Number} monthsPerUnit
|
|
372
|
+
*/
|
|
373
|
+
Builder.prototype._validateMonths = function (units, monthsPerUnit) {
|
|
374
|
+
this._validate32(units, (maxInt32 - this._months) / monthsPerUnit, "months")
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* @param {Number} units
|
|
379
|
+
* @param {Number} daysPerUnit
|
|
380
|
+
*/
|
|
381
|
+
Builder.prototype._validateDays = function (units, daysPerUnit) {
|
|
382
|
+
this._validate32(units, (maxInt32 - this._days) / daysPerUnit, "days")
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @param {Long} units
|
|
387
|
+
* @param {Long} nanosPerUnit
|
|
388
|
+
*/
|
|
389
|
+
Builder.prototype._validateNanos = function (units, nanosPerUnit) {
|
|
390
|
+
this._validate64(
|
|
391
|
+
units,
|
|
392
|
+
Long.MAX_VALUE.subtract(this._nanoseconds).divide(nanosPerUnit),
|
|
393
|
+
"nanoseconds",
|
|
394
|
+
)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* @param {Number} units
|
|
399
|
+
* @param {Number} limit
|
|
400
|
+
* @param {String} unitName
|
|
401
|
+
*/
|
|
402
|
+
Builder.prototype._validate32 = function (units, limit, unitName) {
|
|
403
|
+
if (units > limit) {
|
|
404
|
+
throw new TypeError(
|
|
405
|
+
util.format(
|
|
406
|
+
"Invalid duration. The total number of %s must be less or equal to %s",
|
|
407
|
+
unitName,
|
|
408
|
+
maxInt32,
|
|
409
|
+
),
|
|
410
|
+
)
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* @param {Long} units
|
|
416
|
+
* @param {Long} limit
|
|
417
|
+
* @param {String} unitName
|
|
418
|
+
*/
|
|
419
|
+
Builder.prototype._validate64 = function (units, limit, unitName) {
|
|
420
|
+
if (units.greaterThan(limit)) {
|
|
421
|
+
throw new TypeError(
|
|
422
|
+
util.format(
|
|
423
|
+
"Invalid duration. The total number of %s must be less or equal to %s",
|
|
424
|
+
unitName,
|
|
425
|
+
Long.MAX_VALUE.toString(),
|
|
426
|
+
),
|
|
427
|
+
)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
Builder.prototype._getUnitName = function (unitIndex) {
|
|
432
|
+
const name = this._unitByIndex[+unitIndex]
|
|
433
|
+
if (!name) {
|
|
434
|
+
throw new Error("unknown unit index: " + unitIndex)
|
|
435
|
+
}
|
|
436
|
+
return name
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
Builder.prototype.add = function (textValue, symbol) {
|
|
440
|
+
const addMethod = this._addMethods[symbol.toLowerCase()]
|
|
441
|
+
if (!addMethod) {
|
|
442
|
+
throw new TypeError(util.format("Unknown duration symbol '%s'", symbol))
|
|
443
|
+
}
|
|
444
|
+
return addMethod.call(this, textValue)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* @param {String|Number} years
|
|
449
|
+
* @return {Builder}
|
|
450
|
+
*/
|
|
451
|
+
Builder.prototype.addYears = function (years) {
|
|
452
|
+
const value = +years
|
|
453
|
+
this._validateOrder(1)
|
|
454
|
+
this._validateMonths(value, monthsPerYear)
|
|
455
|
+
this._months += value * monthsPerYear
|
|
456
|
+
return this
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @param {String|Number} months
|
|
461
|
+
* @return {Builder}
|
|
462
|
+
*/
|
|
463
|
+
Builder.prototype.addMonths = function (months) {
|
|
464
|
+
const value = +months
|
|
465
|
+
this._validateOrder(2)
|
|
466
|
+
this._validateMonths(value, 1)
|
|
467
|
+
this._months += value
|
|
468
|
+
return this
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* @param {String|Number} weeks
|
|
473
|
+
* @return {Builder}
|
|
474
|
+
*/
|
|
475
|
+
Builder.prototype.addWeeks = function (weeks) {
|
|
476
|
+
const value = +weeks
|
|
477
|
+
this._validateOrder(3)
|
|
478
|
+
this._validateDays(value, daysPerWeek)
|
|
479
|
+
this._days += value * daysPerWeek
|
|
480
|
+
return this
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* @param {String|Number} days
|
|
485
|
+
* @return {Builder}
|
|
486
|
+
*/
|
|
487
|
+
Builder.prototype.addDays = function (days) {
|
|
488
|
+
const value = +days
|
|
489
|
+
this._validateOrder(4)
|
|
490
|
+
this._validateDays(value, 1)
|
|
491
|
+
this._days += value
|
|
492
|
+
return this
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* @param {String|Long} hours
|
|
497
|
+
* @return {Builder}
|
|
498
|
+
*/
|
|
499
|
+
Builder.prototype.addHours = function (hours) {
|
|
500
|
+
const value = typeof hours === "string" ? Long.fromString(hours) : hours
|
|
501
|
+
this._validateOrder(5)
|
|
502
|
+
this._validateNanos(value, nanosPerHour)
|
|
503
|
+
this._nanoseconds = this._nanoseconds.add(value.multiply(nanosPerHour))
|
|
504
|
+
return this
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* @param {String|Long} minutes
|
|
509
|
+
* @return {Builder}
|
|
510
|
+
*/
|
|
511
|
+
Builder.prototype.addMinutes = function (minutes) {
|
|
512
|
+
const value =
|
|
513
|
+
typeof minutes === "string" ? Long.fromString(minutes) : minutes
|
|
514
|
+
this._validateOrder(6)
|
|
515
|
+
this._validateNanos(value, nanosPerMinute)
|
|
516
|
+
this._nanoseconds = this._nanoseconds.add(value.multiply(nanosPerMinute))
|
|
517
|
+
return this
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* @param {String|Long} seconds
|
|
522
|
+
* @return {Builder}
|
|
523
|
+
*/
|
|
524
|
+
Builder.prototype.addSeconds = function (seconds) {
|
|
525
|
+
const value =
|
|
526
|
+
typeof seconds === "string" ? Long.fromString(seconds) : seconds
|
|
527
|
+
this._validateOrder(7)
|
|
528
|
+
this._validateNanos(value, nanosPerSecond)
|
|
529
|
+
this._nanoseconds = this._nanoseconds.add(value.multiply(nanosPerSecond))
|
|
530
|
+
return this
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* @param {String|Long} millis
|
|
535
|
+
* @return {Builder}
|
|
536
|
+
*/
|
|
537
|
+
Builder.prototype.addMillis = function (millis) {
|
|
538
|
+
const value = typeof millis === "string" ? Long.fromString(millis) : millis
|
|
539
|
+
this._validateOrder(8)
|
|
540
|
+
this._validateNanos(value, nanosPerMilli)
|
|
541
|
+
this._nanoseconds = this._nanoseconds.add(value.multiply(nanosPerMilli))
|
|
542
|
+
return this
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* @param {String|Long} micros
|
|
547
|
+
* @return {Builder}
|
|
548
|
+
*/
|
|
549
|
+
Builder.prototype.addMicros = function (micros) {
|
|
550
|
+
const value = typeof micros === "string" ? Long.fromString(micros) : micros
|
|
551
|
+
this._validateOrder(9)
|
|
552
|
+
this._validateNanos(value, nanosPerMicro)
|
|
553
|
+
this._nanoseconds = this._nanoseconds.add(value.multiply(nanosPerMicro))
|
|
554
|
+
return this
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* @param {String|Long} nanos
|
|
559
|
+
* @return {Builder}
|
|
560
|
+
*/
|
|
561
|
+
Builder.prototype.addNanos = function (nanos) {
|
|
562
|
+
const value = typeof nanos === "string" ? Long.fromString(nanos) : nanos
|
|
563
|
+
this._validateOrder(10)
|
|
564
|
+
this._validateNanos(value, Long.ONE)
|
|
565
|
+
this._nanoseconds = this._nanoseconds.add(value)
|
|
566
|
+
return this
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** @return {Duration} */
|
|
570
|
+
Builder.prototype.build = function () {
|
|
571
|
+
return this._isNegative
|
|
572
|
+
? new Duration(-this._months, -this._days, this._nanoseconds.negate())
|
|
573
|
+
: new Duration(this._months, this._days, this._nanoseconds)
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export default Duration
|