@mark1russell7/vex-domains 0.5.0 → 0.7.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.
- package/README.md +18 -4
- package/build/index.d.ts +117 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +631 -71
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/date.ts +140 -0
- package/src/index.ts +3 -1
- package/src/ndvector.ts +11 -1
- package/src/scalars.ts +54 -25
- package/src/text.ts +8 -7
- package/src/vec2.ts +48 -42
- package/src/xy.ts +74 -0
package/build/index.js
CHANGED
|
@@ -177,25 +177,39 @@ const Vec2Domain = defineDomain({
|
|
|
177
177
|
return new Vec2(typeof x === "number" ? x : NaN, typeof y === "number" ? y : NaN);
|
|
178
178
|
},
|
|
179
179
|
ops: {
|
|
180
|
+
x: {
|
|
181
|
+
fn: (v) => v.x,
|
|
182
|
+
params: [],
|
|
183
|
+
returns: "number"
|
|
184
|
+
},
|
|
185
|
+
y: {
|
|
186
|
+
fn: (v) => v.y,
|
|
187
|
+
params: [],
|
|
188
|
+
returns: "number"
|
|
189
|
+
},
|
|
180
190
|
add: {
|
|
181
191
|
laws: ["commutative", "associative"],
|
|
182
192
|
identity: () => Vec2.zero,
|
|
183
193
|
liftScalar: true,
|
|
184
|
-
params: ["domain"]
|
|
194
|
+
params: ["domain"],
|
|
195
|
+
returns: "domain"
|
|
185
196
|
},
|
|
186
197
|
subtract: {
|
|
187
198
|
liftScalar: true,
|
|
188
|
-
params: ["domain"]
|
|
199
|
+
params: ["domain"],
|
|
200
|
+
returns: "domain"
|
|
189
201
|
},
|
|
190
202
|
multiply: {
|
|
191
203
|
laws: ["commutative", "associative"],
|
|
192
204
|
identity: () => Vec2.one,
|
|
193
205
|
liftScalar: true,
|
|
194
|
-
params: ["domain"]
|
|
206
|
+
params: ["domain"],
|
|
207
|
+
returns: "domain"
|
|
195
208
|
},
|
|
196
209
|
divide: {
|
|
197
210
|
liftScalar: true,
|
|
198
|
-
params: ["domain"]
|
|
211
|
+
params: ["domain"],
|
|
212
|
+
returns: "domain"
|
|
199
213
|
},
|
|
200
214
|
min: {
|
|
201
215
|
laws: [
|
|
@@ -204,7 +218,8 @@ const Vec2Domain = defineDomain({
|
|
|
204
218
|
"idempotent"
|
|
205
219
|
],
|
|
206
220
|
liftScalar: true,
|
|
207
|
-
params: ["domain"]
|
|
221
|
+
params: ["domain"],
|
|
222
|
+
returns: "domain"
|
|
208
223
|
},
|
|
209
224
|
max: {
|
|
210
225
|
laws: [
|
|
@@ -213,47 +228,331 @@ const Vec2Domain = defineDomain({
|
|
|
213
228
|
"idempotent"
|
|
214
229
|
],
|
|
215
230
|
liftScalar: true,
|
|
216
|
-
params: ["domain"]
|
|
231
|
+
params: ["domain"],
|
|
232
|
+
returns: "domain"
|
|
233
|
+
},
|
|
234
|
+
scale: {
|
|
235
|
+
params: ["number"],
|
|
236
|
+
returns: "domain"
|
|
237
|
+
},
|
|
238
|
+
negate: {
|
|
239
|
+
params: [],
|
|
240
|
+
returns: "domain"
|
|
241
|
+
},
|
|
242
|
+
halve: {
|
|
243
|
+
params: [],
|
|
244
|
+
returns: "domain"
|
|
245
|
+
},
|
|
246
|
+
abs: {
|
|
247
|
+
params: [],
|
|
248
|
+
returns: "domain"
|
|
249
|
+
},
|
|
250
|
+
round: {
|
|
251
|
+
params: [],
|
|
252
|
+
returns: "domain"
|
|
253
|
+
},
|
|
254
|
+
floor: {
|
|
255
|
+
params: [],
|
|
256
|
+
returns: "domain"
|
|
257
|
+
},
|
|
258
|
+
ceil: {
|
|
259
|
+
params: [],
|
|
260
|
+
returns: "domain"
|
|
261
|
+
},
|
|
262
|
+
clamp: {
|
|
263
|
+
params: ["number", "number"],
|
|
264
|
+
returns: "domain"
|
|
265
|
+
},
|
|
266
|
+
swap: {
|
|
267
|
+
params: [],
|
|
268
|
+
returns: "domain"
|
|
269
|
+
},
|
|
270
|
+
rotate: {
|
|
271
|
+
params: ["number"],
|
|
272
|
+
returns: "domain"
|
|
273
|
+
},
|
|
274
|
+
normalize: {
|
|
275
|
+
params: [],
|
|
276
|
+
returns: "domain"
|
|
277
|
+
},
|
|
278
|
+
lerp: {
|
|
279
|
+
params: ["domain", "number"],
|
|
280
|
+
returns: "domain"
|
|
281
|
+
},
|
|
282
|
+
length: {
|
|
283
|
+
params: [],
|
|
284
|
+
returns: "number"
|
|
285
|
+
},
|
|
286
|
+
lengthSq: {
|
|
287
|
+
params: [],
|
|
288
|
+
returns: "number"
|
|
289
|
+
},
|
|
290
|
+
distance: {
|
|
291
|
+
params: ["domain"],
|
|
292
|
+
returns: "number"
|
|
217
293
|
},
|
|
218
|
-
scale: { params: ["number"] },
|
|
219
|
-
negate: {},
|
|
220
|
-
halve: {},
|
|
221
|
-
abs: {},
|
|
222
|
-
round: {},
|
|
223
|
-
floor: {},
|
|
224
|
-
ceil: {},
|
|
225
|
-
clamp: { params: ["number", "number"] },
|
|
226
|
-
swap: {},
|
|
227
|
-
rotate: { params: ["number"] },
|
|
228
|
-
normalize: {},
|
|
229
|
-
lerp: { params: ["domain", "number"] },
|
|
230
|
-
length: {},
|
|
231
|
-
lengthSq: {},
|
|
232
|
-
distance: { params: ["domain"] },
|
|
233
294
|
dot: {
|
|
234
295
|
laws: ["commutative"],
|
|
235
|
-
params: ["domain"]
|
|
296
|
+
params: ["domain"],
|
|
297
|
+
returns: "number"
|
|
298
|
+
},
|
|
299
|
+
cross: {
|
|
300
|
+
params: ["domain"],
|
|
301
|
+
returns: "number"
|
|
302
|
+
},
|
|
303
|
+
angle: {
|
|
304
|
+
params: [],
|
|
305
|
+
returns: "number"
|
|
306
|
+
},
|
|
307
|
+
area: {
|
|
308
|
+
params: [],
|
|
309
|
+
returns: "number"
|
|
310
|
+
},
|
|
311
|
+
sum: {
|
|
312
|
+
params: [],
|
|
313
|
+
returns: "number"
|
|
314
|
+
},
|
|
315
|
+
minComponent: {
|
|
316
|
+
params: [],
|
|
317
|
+
returns: "number"
|
|
318
|
+
},
|
|
319
|
+
maxComponent: {
|
|
320
|
+
params: [],
|
|
321
|
+
returns: "number"
|
|
236
322
|
},
|
|
237
|
-
cross: { params: ["domain"] },
|
|
238
|
-
angle: {},
|
|
239
|
-
area: {},
|
|
240
|
-
sum: {},
|
|
241
|
-
minComponent: {},
|
|
242
|
-
maxComponent: {},
|
|
243
323
|
equals: {
|
|
244
324
|
laws: ["commutative"],
|
|
245
|
-
params: ["domain"]
|
|
325
|
+
params: ["domain"],
|
|
326
|
+
returns: "boolean"
|
|
327
|
+
},
|
|
328
|
+
anyNonPositive: {
|
|
329
|
+
params: [],
|
|
330
|
+
returns: "boolean"
|
|
331
|
+
},
|
|
332
|
+
allPositive: {
|
|
333
|
+
params: [],
|
|
334
|
+
returns: "boolean"
|
|
335
|
+
},
|
|
336
|
+
anyNegative: {
|
|
337
|
+
params: [],
|
|
338
|
+
returns: "boolean"
|
|
339
|
+
},
|
|
340
|
+
allNonNegative: {
|
|
341
|
+
params: [],
|
|
342
|
+
returns: "boolean"
|
|
343
|
+
},
|
|
344
|
+
anyZero: {
|
|
345
|
+
params: [],
|
|
346
|
+
returns: "boolean"
|
|
246
347
|
},
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
348
|
+
allZero: {
|
|
349
|
+
params: [],
|
|
350
|
+
returns: "boolean"
|
|
351
|
+
},
|
|
352
|
+
anyLessThan: {
|
|
353
|
+
params: ["number"],
|
|
354
|
+
returns: "boolean"
|
|
355
|
+
},
|
|
356
|
+
allLessThan: {
|
|
357
|
+
params: ["number"],
|
|
358
|
+
returns: "boolean"
|
|
359
|
+
},
|
|
360
|
+
anyGreaterThan: {
|
|
361
|
+
params: ["number"],
|
|
362
|
+
returns: "boolean"
|
|
363
|
+
},
|
|
364
|
+
allGreaterThan: {
|
|
365
|
+
params: ["number"],
|
|
366
|
+
returns: "boolean"
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
//#endregion
|
|
371
|
+
//#region src/xy.ts
|
|
372
|
+
/**
|
|
373
|
+
* The domain of 2D vectors as plain data: an object with the two number fields `x` and `y` and no other field. The op
|
|
374
|
+
* `xy` of `NumDomain` makes one. No op lifts a number, so each op has a static site in a module. Put `XYDomain` before
|
|
375
|
+
* `NumRecordDomain`, because a record of numbers accepts the same objects.
|
|
376
|
+
*/
|
|
377
|
+
const XYDomain = defineDomain({
|
|
378
|
+
name: "XY",
|
|
379
|
+
is: (u) => typeof u === "object" && u !== null && [Object.prototype, null].includes(Object.getPrototypeOf(u)) && Object.keys(u).length === 2 && typeof u.x === "number" && typeof u.y === "number",
|
|
380
|
+
valid: (v) => Number.isFinite(v.x) && Number.isFinite(v.y),
|
|
381
|
+
show: (v) => `(${v.x}, ${v.y})`,
|
|
382
|
+
samples: () => [
|
|
383
|
+
{
|
|
384
|
+
x: 0,
|
|
385
|
+
y: 0
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
x: 1.5,
|
|
389
|
+
y: -2
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
x: 3,
|
|
393
|
+
y: 4
|
|
394
|
+
}
|
|
395
|
+
],
|
|
396
|
+
ops: {
|
|
397
|
+
x: {
|
|
398
|
+
fn: (a) => a.x,
|
|
399
|
+
params: [],
|
|
400
|
+
returns: "number"
|
|
401
|
+
},
|
|
402
|
+
y: {
|
|
403
|
+
fn: (a) => a.y,
|
|
404
|
+
params: [],
|
|
405
|
+
returns: "number"
|
|
406
|
+
},
|
|
407
|
+
add: {
|
|
408
|
+
fn: (a, b) => ({
|
|
409
|
+
x: a.x + b.x,
|
|
410
|
+
y: a.y + b.y
|
|
411
|
+
}),
|
|
412
|
+
laws: ["commutative", "associative"],
|
|
413
|
+
identity: () => ({
|
|
414
|
+
x: 0,
|
|
415
|
+
y: 0
|
|
416
|
+
}),
|
|
417
|
+
params: ["domain"],
|
|
418
|
+
returns: "domain"
|
|
419
|
+
},
|
|
420
|
+
subtract: {
|
|
421
|
+
fn: (a, b) => ({
|
|
422
|
+
x: a.x - b.x,
|
|
423
|
+
y: a.y - b.y
|
|
424
|
+
}),
|
|
425
|
+
params: ["domain"],
|
|
426
|
+
returns: "domain"
|
|
427
|
+
},
|
|
428
|
+
multiply: {
|
|
429
|
+
fn: (a, b) => ({
|
|
430
|
+
x: a.x * b.x,
|
|
431
|
+
y: a.y * b.y
|
|
432
|
+
}),
|
|
433
|
+
laws: ["commutative", "associative"],
|
|
434
|
+
identity: () => ({
|
|
435
|
+
x: 1,
|
|
436
|
+
y: 1
|
|
437
|
+
}),
|
|
438
|
+
params: ["domain"],
|
|
439
|
+
returns: "domain"
|
|
440
|
+
},
|
|
441
|
+
divide: {
|
|
442
|
+
fn: (a, b) => ({
|
|
443
|
+
x: a.x / b.x,
|
|
444
|
+
y: a.y / b.y
|
|
445
|
+
}),
|
|
446
|
+
params: ["domain"],
|
|
447
|
+
returns: "domain"
|
|
448
|
+
},
|
|
449
|
+
min: {
|
|
450
|
+
fn: (a, b) => ({
|
|
451
|
+
x: Math.min(a.x, b.x),
|
|
452
|
+
y: Math.min(a.y, b.y)
|
|
453
|
+
}),
|
|
454
|
+
laws: [
|
|
455
|
+
"commutative",
|
|
456
|
+
"associative",
|
|
457
|
+
"idempotent"
|
|
458
|
+
],
|
|
459
|
+
params: ["domain"],
|
|
460
|
+
returns: "domain"
|
|
461
|
+
},
|
|
462
|
+
max: {
|
|
463
|
+
fn: (a, b) => ({
|
|
464
|
+
x: Math.max(a.x, b.x),
|
|
465
|
+
y: Math.max(a.y, b.y)
|
|
466
|
+
}),
|
|
467
|
+
laws: [
|
|
468
|
+
"commutative",
|
|
469
|
+
"associative",
|
|
470
|
+
"idempotent"
|
|
471
|
+
],
|
|
472
|
+
params: ["domain"],
|
|
473
|
+
returns: "domain"
|
|
474
|
+
},
|
|
475
|
+
scale: {
|
|
476
|
+
fn: (a, k) => ({
|
|
477
|
+
x: a.x * k,
|
|
478
|
+
y: a.y * k
|
|
479
|
+
}),
|
|
480
|
+
params: ["number"],
|
|
481
|
+
returns: "domain"
|
|
482
|
+
},
|
|
483
|
+
negate: {
|
|
484
|
+
fn: (a) => ({
|
|
485
|
+
x: -a.x,
|
|
486
|
+
y: -a.y
|
|
487
|
+
}),
|
|
488
|
+
params: [],
|
|
489
|
+
returns: "domain"
|
|
490
|
+
},
|
|
491
|
+
abs: {
|
|
492
|
+
fn: (a) => ({
|
|
493
|
+
x: Math.abs(a.x),
|
|
494
|
+
y: Math.abs(a.y)
|
|
495
|
+
}),
|
|
496
|
+
params: [],
|
|
497
|
+
returns: "domain"
|
|
498
|
+
},
|
|
499
|
+
floor: {
|
|
500
|
+
fn: (a) => ({
|
|
501
|
+
x: Math.floor(a.x),
|
|
502
|
+
y: Math.floor(a.y)
|
|
503
|
+
}),
|
|
504
|
+
params: [],
|
|
505
|
+
returns: "domain"
|
|
506
|
+
},
|
|
507
|
+
ceil: {
|
|
508
|
+
fn: (a) => ({
|
|
509
|
+
x: Math.ceil(a.x),
|
|
510
|
+
y: Math.ceil(a.y)
|
|
511
|
+
}),
|
|
512
|
+
params: [],
|
|
513
|
+
returns: "domain"
|
|
514
|
+
},
|
|
515
|
+
round: {
|
|
516
|
+
fn: (a) => ({
|
|
517
|
+
x: Math.round(a.x),
|
|
518
|
+
y: Math.round(a.y)
|
|
519
|
+
}),
|
|
520
|
+
params: [],
|
|
521
|
+
returns: "domain"
|
|
522
|
+
},
|
|
523
|
+
dot: {
|
|
524
|
+
fn: (a, b) => a.x * b.x + a.y * b.y,
|
|
525
|
+
laws: ["commutative"],
|
|
526
|
+
params: ["domain"],
|
|
527
|
+
returns: "number"
|
|
528
|
+
},
|
|
529
|
+
length: {
|
|
530
|
+
fn: (a) => Math.hypot(a.x, a.y),
|
|
531
|
+
params: [],
|
|
532
|
+
returns: "number"
|
|
533
|
+
},
|
|
534
|
+
distance: {
|
|
535
|
+
fn: (a, b) => Math.hypot(a.x - b.x, a.y - b.y),
|
|
536
|
+
laws: ["commutative"],
|
|
537
|
+
params: ["domain"],
|
|
538
|
+
returns: "number"
|
|
539
|
+
},
|
|
540
|
+
area: {
|
|
541
|
+
fn: (a) => a.x * a.y,
|
|
542
|
+
params: [],
|
|
543
|
+
returns: "number"
|
|
544
|
+
},
|
|
545
|
+
sum: {
|
|
546
|
+
fn: (a) => a.x + a.y,
|
|
547
|
+
params: [],
|
|
548
|
+
returns: "number"
|
|
549
|
+
},
|
|
550
|
+
equals: {
|
|
551
|
+
fn: (a, b) => a.x === b.x && a.y === b.y,
|
|
552
|
+
laws: ["commutative"],
|
|
553
|
+
params: ["domain"],
|
|
554
|
+
returns: "boolean"
|
|
555
|
+
}
|
|
257
556
|
}
|
|
258
557
|
});
|
|
259
558
|
//#endregion
|
|
@@ -328,6 +627,14 @@ var NDVector = class NDVector {
|
|
|
328
627
|
multiply(o) {
|
|
329
628
|
return this.#combine(o, (a, b) => a * b);
|
|
330
629
|
}
|
|
630
|
+
/** The component-wise minimum. A component that one vector does not have counts as 0. */
|
|
631
|
+
min(o) {
|
|
632
|
+
return this.#combine(o, (a, b) => Math.min(a, b));
|
|
633
|
+
}
|
|
634
|
+
/** The component-wise maximum. A component that one vector does not have counts as 0. */
|
|
635
|
+
max(o) {
|
|
636
|
+
return this.#combine(o, (a, b) => Math.max(a, b));
|
|
637
|
+
}
|
|
331
638
|
/** Of each component, the value of this vector if it is not 0, else the value of `o`. */
|
|
332
639
|
mergePreferNonZero(o) {
|
|
333
640
|
return this.#combine(o, (a, b) => a !== 0 ? a : b);
|
|
@@ -382,6 +689,22 @@ const ndOps = {
|
|
|
382
689
|
laws: ["commutative", "associative"],
|
|
383
690
|
params: ["domain"]
|
|
384
691
|
},
|
|
692
|
+
min: {
|
|
693
|
+
laws: [
|
|
694
|
+
"commutative",
|
|
695
|
+
"associative",
|
|
696
|
+
"idempotent"
|
|
697
|
+
],
|
|
698
|
+
params: ["domain"]
|
|
699
|
+
},
|
|
700
|
+
max: {
|
|
701
|
+
laws: [
|
|
702
|
+
"commutative",
|
|
703
|
+
"associative",
|
|
704
|
+
"idempotent"
|
|
705
|
+
],
|
|
706
|
+
params: ["domain"]
|
|
707
|
+
},
|
|
385
708
|
mergePreferNonZero: {
|
|
386
709
|
laws: ["associative", "idempotent"],
|
|
387
710
|
params: ["domain"]
|
|
@@ -458,11 +781,146 @@ const NumRecordDomain = defineDomain({
|
|
|
458
781
|
ops: { toNDVector: { fn: (r) => new NDVector(r) } }
|
|
459
782
|
});
|
|
460
783
|
//#endregion
|
|
784
|
+
//#region src/date.ts
|
|
785
|
+
const MS_PER_DAY = 864e5;
|
|
786
|
+
/** The largest distance from 1970-01-01 in days. The `Date` of JavaScript has the same range. */
|
|
787
|
+
const MAX_DAYS = 1e8;
|
|
788
|
+
const ISO = /^([+-]?\d{4,6})-(\d{2})-(\d{2})$/;
|
|
789
|
+
/** This function gives the `Date` of JavaScript at 00:00 UTC of a day. */
|
|
790
|
+
const utc = (days) => /* @__PURE__ */ new Date(days * MS_PER_DAY);
|
|
791
|
+
/**
|
|
792
|
+
* An immutable day of the calendar, without a time and without a time zone. It keeps the number of days since
|
|
793
|
+
* 1970-01-01, so the arithmetic of days is the arithmetic of numbers. The calendar is the Gregorian calendar, also
|
|
794
|
+
* before 1582.
|
|
795
|
+
*/
|
|
796
|
+
var LocalDate = class LocalDate {
|
|
797
|
+
/** The number of days since 1970-01-01. */
|
|
798
|
+
days;
|
|
799
|
+
constructor(days) {
|
|
800
|
+
this.days = days;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* This function makes a date from a year, a month from 1 to 12, and a day. A month or a day outside its range rolls
|
|
804
|
+
* over, like the function `DATE` of a spreadsheet. Thus month 13 is January of the next year, and day 0 is the last
|
|
805
|
+
* day of the month before. A number that is not whole gives a date that is not valid.
|
|
806
|
+
*/
|
|
807
|
+
static of(year, month, day) {
|
|
808
|
+
if (![
|
|
809
|
+
year,
|
|
810
|
+
month,
|
|
811
|
+
day
|
|
812
|
+
].every(Number.isSafeInteger)) return new LocalDate(NaN);
|
|
813
|
+
const d = /* @__PURE__ */ new Date(0);
|
|
814
|
+
d.setUTCFullYear(year, month - 1, day);
|
|
815
|
+
return new LocalDate(Math.round(d.getTime() / MS_PER_DAY));
|
|
816
|
+
}
|
|
817
|
+
/** This function reads a date in the form `YYYY-MM-DD`. A day that does not exist, for example `2026-02-30`, gives `undefined`. */
|
|
818
|
+
static parse(text) {
|
|
819
|
+
const m = ISO.exec(text.trim());
|
|
820
|
+
if (m === null) return void 0;
|
|
821
|
+
const [y, mo, d] = [
|
|
822
|
+
Number(m[1]),
|
|
823
|
+
Number(m[2]),
|
|
824
|
+
Number(m[3])
|
|
825
|
+
];
|
|
826
|
+
const date = LocalDate.of(y, mo, d);
|
|
827
|
+
return date.month() === mo ? date : void 0;
|
|
828
|
+
}
|
|
829
|
+
/** The year. */
|
|
830
|
+
year() {
|
|
831
|
+
return utc(this.days).getUTCFullYear();
|
|
832
|
+
}
|
|
833
|
+
/** The month, from 1 to 12. */
|
|
834
|
+
month() {
|
|
835
|
+
return utc(this.days).getUTCMonth() + 1;
|
|
836
|
+
}
|
|
837
|
+
/** The day of the month, from 1 to 31. */
|
|
838
|
+
day() {
|
|
839
|
+
return utc(this.days).getUTCDate();
|
|
840
|
+
}
|
|
841
|
+
/** The day of the week, from 1 (Sunday) to 7 (Saturday), like `WEEKDAY` of a spreadsheet. */
|
|
842
|
+
weekday() {
|
|
843
|
+
return utc(this.days).getUTCDay() + 1;
|
|
844
|
+
}
|
|
845
|
+
/** The date `n` days later. A negative `n` goes back. */
|
|
846
|
+
add(n) {
|
|
847
|
+
return new LocalDate(this.days + n);
|
|
848
|
+
}
|
|
849
|
+
/** The number of days from the date `x` to this date, or the date `x` days before this date. */
|
|
850
|
+
subtract(x) {
|
|
851
|
+
return x instanceof LocalDate ? this.days - x.days : new LocalDate(this.days - x);
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* The same day `n` months later, like `EDATE` of a spreadsheet. When that month is shorter, the result is its last
|
|
855
|
+
* day: one month after 31 January is the last day of February.
|
|
856
|
+
*/
|
|
857
|
+
addMonths(n) {
|
|
858
|
+
const first = LocalDate.of(this.year(), this.month() + n, 1);
|
|
859
|
+
const last = LocalDate.of(first.year(), first.month() + 1, 0);
|
|
860
|
+
return new LocalDate(Math.min(first.days + this.day() - 1, last.days));
|
|
861
|
+
}
|
|
862
|
+
/** The last day of the month `n` months later, like `EOMONTH` of a spreadsheet. */
|
|
863
|
+
endOfMonth(n) {
|
|
864
|
+
return LocalDate.of(this.year(), this.month() + n + 1, 0);
|
|
865
|
+
}
|
|
866
|
+
gt(o) {
|
|
867
|
+
return this.days > o.days;
|
|
868
|
+
}
|
|
869
|
+
gte(o) {
|
|
870
|
+
return this.days >= o.days;
|
|
871
|
+
}
|
|
872
|
+
lt(o) {
|
|
873
|
+
return this.days < o.days;
|
|
874
|
+
}
|
|
875
|
+
lte(o) {
|
|
876
|
+
return this.days <= o.days;
|
|
877
|
+
}
|
|
878
|
+
eq(o) {
|
|
879
|
+
return this.days === o.days;
|
|
880
|
+
}
|
|
881
|
+
/** The form `YYYY-MM-DD`. A year before 0 or after 9999 has a sign and six digits, as in ISO 8601. */
|
|
882
|
+
toString() {
|
|
883
|
+
const y = this.year();
|
|
884
|
+
return `${y >= 0 && y <= 9999 ? String(y).padStart(4, "0") : `${y < 0 ? "-" : "+"}${String(Math.abs(y)).padStart(6, "0")}`}-${String(this.month()).padStart(2, "0")}-${String(this.day()).padStart(2, "0")}`;
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
/**
|
|
888
|
+
* The domain of dates. `add` takes a count of days. `subtract` takes a date, for the days between the two dates, or a
|
|
889
|
+
* count of days. A valid date is a whole count of days, within `MAX_DAYS` of 1970-01-01.
|
|
890
|
+
*/
|
|
891
|
+
const DateDomain = defineDomain({
|
|
892
|
+
name: "Date",
|
|
893
|
+
is: (u) => u instanceof LocalDate,
|
|
894
|
+
valid: (d) => Number.isSafeInteger(d.days) && Math.abs(d.days) <= 1e8,
|
|
895
|
+
show: (d) => d.toString(),
|
|
896
|
+
encode: (d) => d.toString(),
|
|
897
|
+
decode: (u) => (typeof u === "string" ? LocalDate.parse(u) : void 0) ?? new LocalDate(NaN),
|
|
898
|
+
ops: {
|
|
899
|
+
add: { params: ["number"] },
|
|
900
|
+
subtract: { params: ["any"] },
|
|
901
|
+
addMonths: { params: ["number"] },
|
|
902
|
+
endOfMonth: { params: ["number"] },
|
|
903
|
+
year: {},
|
|
904
|
+
month: {},
|
|
905
|
+
day: {},
|
|
906
|
+
weekday: {},
|
|
907
|
+
gt: { params: ["domain"] },
|
|
908
|
+
gte: { params: ["domain"] },
|
|
909
|
+
lt: { params: ["domain"] },
|
|
910
|
+
lte: { params: ["domain"] },
|
|
911
|
+
eq: {
|
|
912
|
+
laws: ["commutative"],
|
|
913
|
+
params: ["domain"]
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
//#endregion
|
|
461
918
|
//#region src/scalars.ts
|
|
462
919
|
/** The domain of numbers. A result that is not a finite number is the error `#NUM!`. */
|
|
463
920
|
const NumDomain = defineDomain({
|
|
464
921
|
name: "Num",
|
|
465
922
|
is: (u) => typeof u === "number",
|
|
923
|
+
primitive: "number",
|
|
466
924
|
valid: (n) => Number.isFinite(n),
|
|
467
925
|
show: (n) => String(n),
|
|
468
926
|
ops: {
|
|
@@ -470,29 +928,35 @@ const NumDomain = defineDomain({
|
|
|
470
928
|
fn: (a, b) => a + b,
|
|
471
929
|
laws: ["commutative", "associative"],
|
|
472
930
|
identity: () => 0,
|
|
473
|
-
params: ["number"]
|
|
931
|
+
params: ["number"],
|
|
932
|
+
returns: "number"
|
|
474
933
|
},
|
|
475
934
|
subtract: {
|
|
476
935
|
fn: (a, b) => a - b,
|
|
477
|
-
params: ["number"]
|
|
936
|
+
params: ["number"],
|
|
937
|
+
returns: "number"
|
|
478
938
|
},
|
|
479
939
|
multiply: {
|
|
480
940
|
fn: (a, b) => a * b,
|
|
481
941
|
laws: ["commutative", "associative"],
|
|
482
942
|
identity: () => 1,
|
|
483
|
-
params: ["number"]
|
|
943
|
+
params: ["number"],
|
|
944
|
+
returns: "number"
|
|
484
945
|
},
|
|
485
946
|
divide: {
|
|
486
947
|
fn: (a, b) => a / b,
|
|
487
|
-
params: ["number"]
|
|
948
|
+
params: ["number"],
|
|
949
|
+
returns: "number"
|
|
488
950
|
},
|
|
489
951
|
mod: {
|
|
490
952
|
fn: (a, b) => a % b,
|
|
491
|
-
params: ["number"]
|
|
953
|
+
params: ["number"],
|
|
954
|
+
returns: "number"
|
|
492
955
|
},
|
|
493
956
|
pow: {
|
|
494
957
|
fn: (a, b) => a ** b,
|
|
495
|
-
params: ["number"]
|
|
958
|
+
params: ["number"],
|
|
959
|
+
returns: "number"
|
|
496
960
|
},
|
|
497
961
|
min: {
|
|
498
962
|
fn: (a, b) => Math.min(a, b),
|
|
@@ -501,7 +965,8 @@ const NumDomain = defineDomain({
|
|
|
501
965
|
"associative",
|
|
502
966
|
"idempotent"
|
|
503
967
|
],
|
|
504
|
-
params: ["number"]
|
|
968
|
+
params: ["number"],
|
|
969
|
+
returns: "number"
|
|
505
970
|
},
|
|
506
971
|
max: {
|
|
507
972
|
fn: (a, b) => Math.max(a, b),
|
|
@@ -510,38 +975,109 @@ const NumDomain = defineDomain({
|
|
|
510
975
|
"associative",
|
|
511
976
|
"idempotent"
|
|
512
977
|
],
|
|
513
|
-
params: ["number"]
|
|
978
|
+
params: ["number"],
|
|
979
|
+
returns: "number"
|
|
514
980
|
},
|
|
515
981
|
clamp: {
|
|
516
982
|
fn: (a, lo, hi) => Math.min(hi, Math.max(lo, a)),
|
|
983
|
+
params: ["number", "number"],
|
|
984
|
+
returns: "number"
|
|
985
|
+
},
|
|
986
|
+
abs: {
|
|
987
|
+
fn: (a) => Math.abs(a),
|
|
988
|
+
returns: "number"
|
|
989
|
+
},
|
|
990
|
+
negate: {
|
|
991
|
+
fn: (a) => -a,
|
|
992
|
+
returns: "number"
|
|
993
|
+
},
|
|
994
|
+
sqrt: {
|
|
995
|
+
fn: (a) => Math.sqrt(a),
|
|
996
|
+
returns: "number"
|
|
997
|
+
},
|
|
998
|
+
ln: {
|
|
999
|
+
fn: (a) => Math.log(a),
|
|
1000
|
+
returns: "number"
|
|
1001
|
+
},
|
|
1002
|
+
exp: {
|
|
1003
|
+
fn: (a) => Math.exp(a),
|
|
1004
|
+
returns: "number"
|
|
1005
|
+
},
|
|
1006
|
+
log10: {
|
|
1007
|
+
fn: (a) => Math.log10(a),
|
|
1008
|
+
returns: "number"
|
|
1009
|
+
},
|
|
1010
|
+
sin: {
|
|
1011
|
+
fn: (a) => Math.sin(a),
|
|
1012
|
+
returns: "number"
|
|
1013
|
+
},
|
|
1014
|
+
cos: {
|
|
1015
|
+
fn: (a) => Math.cos(a),
|
|
1016
|
+
returns: "number"
|
|
1017
|
+
},
|
|
1018
|
+
asin: {
|
|
1019
|
+
fn: (a) => Math.asin(a),
|
|
1020
|
+
returns: "number"
|
|
1021
|
+
},
|
|
1022
|
+
hypot: {
|
|
1023
|
+
fn: (a, b) => Math.hypot(a, b),
|
|
1024
|
+
laws: ["commutative"],
|
|
1025
|
+
params: ["number"],
|
|
1026
|
+
returns: "number"
|
|
1027
|
+
},
|
|
1028
|
+
date: {
|
|
1029
|
+
fn: (year, month, day) => LocalDate.of(year, month, day),
|
|
517
1030
|
params: ["number", "number"]
|
|
518
1031
|
},
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
1032
|
+
vec2: {
|
|
1033
|
+
fn: (x, y) => new Vec2(x, y),
|
|
1034
|
+
params: ["number"]
|
|
1035
|
+
},
|
|
1036
|
+
xy: {
|
|
1037
|
+
fn: (x, y) => ({
|
|
1038
|
+
x,
|
|
1039
|
+
y
|
|
1040
|
+
}),
|
|
1041
|
+
params: ["number"],
|
|
1042
|
+
returns: "domain:XY"
|
|
1043
|
+
},
|
|
1044
|
+
floor: {
|
|
1045
|
+
fn: (a) => Math.floor(a),
|
|
1046
|
+
returns: "number"
|
|
1047
|
+
},
|
|
1048
|
+
ceil: {
|
|
1049
|
+
fn: (a) => Math.ceil(a),
|
|
1050
|
+
returns: "number"
|
|
1051
|
+
},
|
|
1052
|
+
round: {
|
|
1053
|
+
fn: (a) => Math.round(a),
|
|
1054
|
+
returns: "number"
|
|
1055
|
+
},
|
|
525
1056
|
gt: {
|
|
526
1057
|
fn: (a, b) => a > b,
|
|
527
|
-
params: ["number"]
|
|
1058
|
+
params: ["number"],
|
|
1059
|
+
returns: "boolean"
|
|
528
1060
|
},
|
|
529
1061
|
gte: {
|
|
530
1062
|
fn: (a, b) => a >= b,
|
|
531
|
-
params: ["number"]
|
|
1063
|
+
params: ["number"],
|
|
1064
|
+
returns: "boolean"
|
|
532
1065
|
},
|
|
533
1066
|
lt: {
|
|
534
1067
|
fn: (a, b) => a < b,
|
|
535
|
-
params: ["number"]
|
|
1068
|
+
params: ["number"],
|
|
1069
|
+
returns: "boolean"
|
|
536
1070
|
},
|
|
537
1071
|
lte: {
|
|
538
1072
|
fn: (a, b) => a <= b,
|
|
539
|
-
params: ["number"]
|
|
1073
|
+
params: ["number"],
|
|
1074
|
+
returns: "boolean"
|
|
540
1075
|
},
|
|
541
1076
|
eq: {
|
|
542
1077
|
fn: (a, b) => a === b,
|
|
543
1078
|
laws: ["commutative"],
|
|
544
|
-
params: ["number"]
|
|
1079
|
+
params: ["number"],
|
|
1080
|
+
returns: "boolean"
|
|
545
1081
|
}
|
|
546
1082
|
}
|
|
547
1083
|
});
|
|
@@ -549,6 +1085,7 @@ const NumDomain = defineDomain({
|
|
|
549
1085
|
const BoolDomain = defineDomain({
|
|
550
1086
|
name: "Bool",
|
|
551
1087
|
is: (u) => typeof u === "boolean",
|
|
1088
|
+
primitive: "boolean",
|
|
552
1089
|
show: (b) => String(b),
|
|
553
1090
|
ops: {
|
|
554
1091
|
and: {
|
|
@@ -559,7 +1096,8 @@ const BoolDomain = defineDomain({
|
|
|
559
1096
|
"idempotent"
|
|
560
1097
|
],
|
|
561
1098
|
identity: () => true,
|
|
562
|
-
params: ["boolean"]
|
|
1099
|
+
params: ["boolean"],
|
|
1100
|
+
returns: "boolean"
|
|
563
1101
|
},
|
|
564
1102
|
or: {
|
|
565
1103
|
fn: (a, b) => a || b,
|
|
@@ -569,19 +1107,25 @@ const BoolDomain = defineDomain({
|
|
|
569
1107
|
"idempotent"
|
|
570
1108
|
],
|
|
571
1109
|
identity: () => false,
|
|
572
|
-
params: ["boolean"]
|
|
1110
|
+
params: ["boolean"],
|
|
1111
|
+
returns: "boolean"
|
|
573
1112
|
},
|
|
574
1113
|
xor: {
|
|
575
1114
|
fn: (a, b) => a !== b,
|
|
576
1115
|
laws: ["commutative", "associative"],
|
|
577
1116
|
identity: () => false,
|
|
578
|
-
params: ["boolean"]
|
|
1117
|
+
params: ["boolean"],
|
|
1118
|
+
returns: "boolean"
|
|
1119
|
+
},
|
|
1120
|
+
not: {
|
|
1121
|
+
fn: (a) => !a,
|
|
1122
|
+
returns: "boolean"
|
|
579
1123
|
},
|
|
580
|
-
not: { fn: (a) => !a },
|
|
581
1124
|
eq: {
|
|
582
1125
|
fn: (a, b) => a === b,
|
|
583
1126
|
laws: ["commutative"],
|
|
584
|
-
params: ["boolean"]
|
|
1127
|
+
params: ["boolean"],
|
|
1128
|
+
returns: "boolean"
|
|
585
1129
|
}
|
|
586
1130
|
}
|
|
587
1131
|
});
|
|
@@ -600,24 +1144,40 @@ const textOf = (u) => typeof u === "object" && u !== null && Object.getPrototype
|
|
|
600
1144
|
const TextDomain = defineDomain({
|
|
601
1145
|
name: "Text",
|
|
602
1146
|
is: (u) => typeof u === "string",
|
|
1147
|
+
primitive: "string",
|
|
603
1148
|
show: (s) => JSON.stringify(s),
|
|
604
1149
|
ops: {
|
|
605
1150
|
concat: {
|
|
606
1151
|
fn: (a, b) => a + textOf(b),
|
|
607
|
-
params: ["any"]
|
|
1152
|
+
params: ["any"],
|
|
1153
|
+
returns: "string"
|
|
1154
|
+
},
|
|
1155
|
+
length: {
|
|
1156
|
+
fn: (a) => a.length,
|
|
1157
|
+
returns: "number"
|
|
1158
|
+
},
|
|
1159
|
+
upper: {
|
|
1160
|
+
fn: (a) => a.toUpperCase(),
|
|
1161
|
+
returns: "string"
|
|
1162
|
+
},
|
|
1163
|
+
lower: {
|
|
1164
|
+
fn: (a) => a.toLowerCase(),
|
|
1165
|
+
returns: "string"
|
|
1166
|
+
},
|
|
1167
|
+
trim: {
|
|
1168
|
+
fn: (a) => a.trim(),
|
|
1169
|
+
returns: "string"
|
|
608
1170
|
},
|
|
609
|
-
length: { fn: (a) => a.length },
|
|
610
|
-
upper: { fn: (a) => a.toUpperCase() },
|
|
611
|
-
lower: { fn: (a) => a.toLowerCase() },
|
|
612
|
-
trim: { fn: (a) => a.trim() },
|
|
613
1171
|
contains: {
|
|
614
1172
|
fn: (a, b) => a.includes(b),
|
|
615
|
-
params: ["string"]
|
|
1173
|
+
params: ["string"],
|
|
1174
|
+
returns: "boolean"
|
|
616
1175
|
},
|
|
617
1176
|
eq: {
|
|
618
1177
|
fn: (a, b) => a === b,
|
|
619
1178
|
laws: ["commutative"],
|
|
620
|
-
params: ["string"]
|
|
1179
|
+
params: ["string"],
|
|
1180
|
+
returns: "boolean"
|
|
621
1181
|
}
|
|
622
1182
|
}
|
|
623
1183
|
});
|
|
@@ -869,6 +1429,6 @@ const MaybeDomain = defineDomain({
|
|
|
869
1429
|
}
|
|
870
1430
|
});
|
|
871
1431
|
//#endregion
|
|
872
|
-
export { Angle, AngleDomain, BoolDomain, Color, ColorDomain, Fn, FnDomain, LiftedNDVectorDomain, Maybe, MaybeDomain, NDVector, NDVectorDomain, NumDomain, NumRecordDomain, TextDomain, Vec2, Vec2Domain };
|
|
1432
|
+
export { Angle, AngleDomain, BoolDomain, Color, ColorDomain, DateDomain, Fn, FnDomain, LiftedNDVectorDomain, LocalDate, MAX_DAYS, Maybe, MaybeDomain, NDVector, NDVectorDomain, NumDomain, NumRecordDomain, TextDomain, Vec2, Vec2Domain, XYDomain };
|
|
873
1433
|
|
|
874
1434
|
//# sourceMappingURL=index.js.map
|