@vitest/utils 2.0.0 → 2.0.2
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/dist/chunk-display.js +1 -1
- package/dist/diff.d.ts +3 -3
- package/dist/diff.js +810 -15
- package/dist/error.d.ts +2 -2
- package/dist/error.js +2 -3
- package/dist/index.d.ts +3 -47
- package/dist/index.js +26 -24
- package/dist/{types-DyShQl4f.d.ts → types-Bxe-2Udy.d.ts} +1 -1
- package/package.json +4 -3
- package/dist/chunk-colors.js +0 -84
package/dist/chunk-display.js
CHANGED
package/dist/diff.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { D as DiffOptions } from './types-
|
2
|
-
export { a as DiffOptionsColor } from './types-
|
3
|
-
import 'pretty-format';
|
1
|
+
import { D as DiffOptions } from './types-Bxe-2Udy.js';
|
2
|
+
export { a as DiffOptionsColor } from './types-Bxe-2Udy.js';
|
3
|
+
import '@vitest/pretty-format';
|
4
4
|
|
5
5
|
/**
|
6
6
|
* Diff Match and Patch
|
package/dist/diff.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
import { format, plugins } from 'pretty-format';
|
2
|
-
import
|
3
|
-
import { g as getColors } from './chunk-colors.js';
|
1
|
+
import { format, plugins } from '@vitest/pretty-format';
|
2
|
+
import c from 'tinyrainbow';
|
4
3
|
|
5
4
|
function getType(value) {
|
6
5
|
if (value === void 0) {
|
@@ -398,6 +397,805 @@ function diff_cleanupMerge(diffs) {
|
|
398
397
|
const NO_DIFF_MESSAGE = "Compared values have no visual difference.";
|
399
398
|
const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\nPrinting internal object structure without calling `toJSON` instead.";
|
400
399
|
|
400
|
+
var build = {};
|
401
|
+
|
402
|
+
Object.defineProperty(build, '__esModule', {
|
403
|
+
value: true
|
404
|
+
});
|
405
|
+
var _default = build.default = diffSequence;
|
406
|
+
/**
|
407
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
408
|
+
*
|
409
|
+
* This source code is licensed under the MIT license found in the
|
410
|
+
* LICENSE file in the root directory of this source tree.
|
411
|
+
*
|
412
|
+
*/
|
413
|
+
|
414
|
+
// This diff-sequences package implements the linear space variation in
|
415
|
+
// An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers
|
416
|
+
|
417
|
+
// Relationship in notation between Myers paper and this package:
|
418
|
+
// A is a
|
419
|
+
// N is aLength, aEnd - aStart, and so on
|
420
|
+
// x is aIndex, aFirst, aLast, and so on
|
421
|
+
// B is b
|
422
|
+
// M is bLength, bEnd - bStart, and so on
|
423
|
+
// y is bIndex, bFirst, bLast, and so on
|
424
|
+
// Δ = N - M is negative of baDeltaLength = bLength - aLength
|
425
|
+
// D is d
|
426
|
+
// k is kF
|
427
|
+
// k + Δ is kF = kR - baDeltaLength
|
428
|
+
// V is aIndexesF or aIndexesR (see comment below about Indexes type)
|
429
|
+
// index intervals [1, N] and [1, M] are [0, aLength) and [0, bLength)
|
430
|
+
// starting point in forward direction (0, 0) is (-1, -1)
|
431
|
+
// starting point in reverse direction (N + 1, M + 1) is (aLength, bLength)
|
432
|
+
|
433
|
+
// The “edit graph” for sequences a and b corresponds to items:
|
434
|
+
// in a on the horizontal axis
|
435
|
+
// in b on the vertical axis
|
436
|
+
//
|
437
|
+
// Given a-coordinate of a point in a diagonal, you can compute b-coordinate.
|
438
|
+
//
|
439
|
+
// Forward diagonals kF:
|
440
|
+
// zero diagonal intersects top left corner
|
441
|
+
// positive diagonals intersect top edge
|
442
|
+
// negative diagonals insersect left edge
|
443
|
+
//
|
444
|
+
// Reverse diagonals kR:
|
445
|
+
// zero diagonal intersects bottom right corner
|
446
|
+
// positive diagonals intersect right edge
|
447
|
+
// negative diagonals intersect bottom edge
|
448
|
+
|
449
|
+
// The graph contains a directed acyclic graph of edges:
|
450
|
+
// horizontal: delete an item from a
|
451
|
+
// vertical: insert an item from b
|
452
|
+
// diagonal: common item in a and b
|
453
|
+
//
|
454
|
+
// The algorithm solves dual problems in the graph analogy:
|
455
|
+
// Find longest common subsequence: path with maximum number of diagonal edges
|
456
|
+
// Find shortest edit script: path with minimum number of non-diagonal edges
|
457
|
+
|
458
|
+
// Input callback function compares items at indexes in the sequences.
|
459
|
+
|
460
|
+
// Output callback function receives the number of adjacent items
|
461
|
+
// and starting indexes of each common subsequence.
|
462
|
+
// Either original functions or wrapped to swap indexes if graph is transposed.
|
463
|
+
// Indexes in sequence a of last point of forward or reverse paths in graph.
|
464
|
+
// Myers algorithm indexes by diagonal k which for negative is bad deopt in V8.
|
465
|
+
// This package indexes by iF and iR which are greater than or equal to zero.
|
466
|
+
// and also updates the index arrays in place to cut memory in half.
|
467
|
+
// kF = 2 * iF - d
|
468
|
+
// kR = d - 2 * iR
|
469
|
+
// Division of index intervals in sequences a and b at the middle change.
|
470
|
+
// Invariant: intervals do not have common items at the start or end.
|
471
|
+
const pkg = 'diff-sequences'; // for error messages
|
472
|
+
const NOT_YET_SET = 0; // small int instead of undefined to avoid deopt in V8
|
473
|
+
|
474
|
+
// Return the number of common items that follow in forward direction.
|
475
|
+
// The length of what Myers paper calls a “snake” in a forward path.
|
476
|
+
const countCommonItemsF = (aIndex, aEnd, bIndex, bEnd, isCommon) => {
|
477
|
+
let nCommon = 0;
|
478
|
+
while (aIndex < aEnd && bIndex < bEnd && isCommon(aIndex, bIndex)) {
|
479
|
+
aIndex += 1;
|
480
|
+
bIndex += 1;
|
481
|
+
nCommon += 1;
|
482
|
+
}
|
483
|
+
return nCommon;
|
484
|
+
};
|
485
|
+
|
486
|
+
// Return the number of common items that precede in reverse direction.
|
487
|
+
// The length of what Myers paper calls a “snake” in a reverse path.
|
488
|
+
const countCommonItemsR = (aStart, aIndex, bStart, bIndex, isCommon) => {
|
489
|
+
let nCommon = 0;
|
490
|
+
while (aStart <= aIndex && bStart <= bIndex && isCommon(aIndex, bIndex)) {
|
491
|
+
aIndex -= 1;
|
492
|
+
bIndex -= 1;
|
493
|
+
nCommon += 1;
|
494
|
+
}
|
495
|
+
return nCommon;
|
496
|
+
};
|
497
|
+
|
498
|
+
// A simple function to extend forward paths from (d - 1) to d changes
|
499
|
+
// when forward and reverse paths cannot yet overlap.
|
500
|
+
const extendPathsF = (
|
501
|
+
d,
|
502
|
+
aEnd,
|
503
|
+
bEnd,
|
504
|
+
bF,
|
505
|
+
isCommon,
|
506
|
+
aIndexesF,
|
507
|
+
iMaxF // return the value because optimization might decrease it
|
508
|
+
) => {
|
509
|
+
// Unroll the first iteration.
|
510
|
+
let iF = 0;
|
511
|
+
let kF = -d; // kF = 2 * iF - d
|
512
|
+
let aFirst = aIndexesF[iF]; // in first iteration always insert
|
513
|
+
let aIndexPrev1 = aFirst; // prev value of [iF - 1] in next iteration
|
514
|
+
aIndexesF[iF] += countCommonItemsF(
|
515
|
+
aFirst + 1,
|
516
|
+
aEnd,
|
517
|
+
bF + aFirst - kF + 1,
|
518
|
+
bEnd,
|
519
|
+
isCommon
|
520
|
+
);
|
521
|
+
|
522
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
523
|
+
const nF = d < iMaxF ? d : iMaxF;
|
524
|
+
|
525
|
+
// The diagonals kF are odd when d is odd and even when d is even.
|
526
|
+
for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
|
527
|
+
// To get first point of path segment, move one change in forward direction
|
528
|
+
// from last point of previous path segment in an adjacent diagonal.
|
529
|
+
// In last possible iteration when iF === d and kF === d always delete.
|
530
|
+
if (iF !== d && aIndexPrev1 < aIndexesF[iF]) {
|
531
|
+
aFirst = aIndexesF[iF]; // vertical to insert from b
|
532
|
+
} else {
|
533
|
+
aFirst = aIndexPrev1 + 1; // horizontal to delete from a
|
534
|
+
|
535
|
+
if (aEnd <= aFirst) {
|
536
|
+
// Optimization: delete moved past right of graph.
|
537
|
+
return iF - 1;
|
538
|
+
}
|
539
|
+
}
|
540
|
+
|
541
|
+
// To get last point of path segment, move along diagonal of common items.
|
542
|
+
aIndexPrev1 = aIndexesF[iF];
|
543
|
+
aIndexesF[iF] =
|
544
|
+
aFirst +
|
545
|
+
countCommonItemsF(aFirst + 1, aEnd, bF + aFirst - kF + 1, bEnd, isCommon);
|
546
|
+
}
|
547
|
+
return iMaxF;
|
548
|
+
};
|
549
|
+
|
550
|
+
// A simple function to extend reverse paths from (d - 1) to d changes
|
551
|
+
// when reverse and forward paths cannot yet overlap.
|
552
|
+
const extendPathsR = (
|
553
|
+
d,
|
554
|
+
aStart,
|
555
|
+
bStart,
|
556
|
+
bR,
|
557
|
+
isCommon,
|
558
|
+
aIndexesR,
|
559
|
+
iMaxR // return the value because optimization might decrease it
|
560
|
+
) => {
|
561
|
+
// Unroll the first iteration.
|
562
|
+
let iR = 0;
|
563
|
+
let kR = d; // kR = d - 2 * iR
|
564
|
+
let aFirst = aIndexesR[iR]; // in first iteration always insert
|
565
|
+
let aIndexPrev1 = aFirst; // prev value of [iR - 1] in next iteration
|
566
|
+
aIndexesR[iR] -= countCommonItemsR(
|
567
|
+
aStart,
|
568
|
+
aFirst - 1,
|
569
|
+
bStart,
|
570
|
+
bR + aFirst - kR - 1,
|
571
|
+
isCommon
|
572
|
+
);
|
573
|
+
|
574
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
575
|
+
const nR = d < iMaxR ? d : iMaxR;
|
576
|
+
|
577
|
+
// The diagonals kR are odd when d is odd and even when d is even.
|
578
|
+
for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
|
579
|
+
// To get first point of path segment, move one change in reverse direction
|
580
|
+
// from last point of previous path segment in an adjacent diagonal.
|
581
|
+
// In last possible iteration when iR === d and kR === -d always delete.
|
582
|
+
if (iR !== d && aIndexesR[iR] < aIndexPrev1) {
|
583
|
+
aFirst = aIndexesR[iR]; // vertical to insert from b
|
584
|
+
} else {
|
585
|
+
aFirst = aIndexPrev1 - 1; // horizontal to delete from a
|
586
|
+
|
587
|
+
if (aFirst < aStart) {
|
588
|
+
// Optimization: delete moved past left of graph.
|
589
|
+
return iR - 1;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
|
593
|
+
// To get last point of path segment, move along diagonal of common items.
|
594
|
+
aIndexPrev1 = aIndexesR[iR];
|
595
|
+
aIndexesR[iR] =
|
596
|
+
aFirst -
|
597
|
+
countCommonItemsR(
|
598
|
+
aStart,
|
599
|
+
aFirst - 1,
|
600
|
+
bStart,
|
601
|
+
bR + aFirst - kR - 1,
|
602
|
+
isCommon
|
603
|
+
);
|
604
|
+
}
|
605
|
+
return iMaxR;
|
606
|
+
};
|
607
|
+
|
608
|
+
// A complete function to extend forward paths from (d - 1) to d changes.
|
609
|
+
// Return true if a path overlaps reverse path of (d - 1) changes in its diagonal.
|
610
|
+
const extendOverlappablePathsF = (
|
611
|
+
d,
|
612
|
+
aStart,
|
613
|
+
aEnd,
|
614
|
+
bStart,
|
615
|
+
bEnd,
|
616
|
+
isCommon,
|
617
|
+
aIndexesF,
|
618
|
+
iMaxF,
|
619
|
+
aIndexesR,
|
620
|
+
iMaxR,
|
621
|
+
division // update prop values if return true
|
622
|
+
) => {
|
623
|
+
const bF = bStart - aStart; // bIndex = bF + aIndex - kF
|
624
|
+
const aLength = aEnd - aStart;
|
625
|
+
const bLength = bEnd - bStart;
|
626
|
+
const baDeltaLength = bLength - aLength; // kF = kR - baDeltaLength
|
627
|
+
|
628
|
+
// Range of diagonals in which forward and reverse paths might overlap.
|
629
|
+
const kMinOverlapF = -baDeltaLength - (d - 1); // -(d - 1) <= kR
|
630
|
+
const kMaxOverlapF = -baDeltaLength + (d - 1); // kR <= (d - 1)
|
631
|
+
|
632
|
+
let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration
|
633
|
+
|
634
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
635
|
+
const nF = d < iMaxF ? d : iMaxF;
|
636
|
+
|
637
|
+
// The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
|
638
|
+
for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
|
639
|
+
// To get first point of path segment, move one change in forward direction
|
640
|
+
// from last point of previous path segment in an adjacent diagonal.
|
641
|
+
// In first iteration when iF === 0 and kF === -d always insert.
|
642
|
+
// In last possible iteration when iF === d and kF === d always delete.
|
643
|
+
const insert = iF === 0 || (iF !== d && aIndexPrev1 < aIndexesF[iF]);
|
644
|
+
const aLastPrev = insert ? aIndexesF[iF] : aIndexPrev1;
|
645
|
+
const aFirst = insert
|
646
|
+
? aLastPrev // vertical to insert from b
|
647
|
+
: aLastPrev + 1; // horizontal to delete from a
|
648
|
+
|
649
|
+
// To get last point of path segment, move along diagonal of common items.
|
650
|
+
const bFirst = bF + aFirst - kF;
|
651
|
+
const nCommonF = countCommonItemsF(
|
652
|
+
aFirst + 1,
|
653
|
+
aEnd,
|
654
|
+
bFirst + 1,
|
655
|
+
bEnd,
|
656
|
+
isCommon
|
657
|
+
);
|
658
|
+
const aLast = aFirst + nCommonF;
|
659
|
+
aIndexPrev1 = aIndexesF[iF];
|
660
|
+
aIndexesF[iF] = aLast;
|
661
|
+
if (kMinOverlapF <= kF && kF <= kMaxOverlapF) {
|
662
|
+
// Solve for iR of reverse path with (d - 1) changes in diagonal kF:
|
663
|
+
// kR = kF + baDeltaLength
|
664
|
+
// kR = (d - 1) - 2 * iR
|
665
|
+
const iR = (d - 1 - (kF + baDeltaLength)) / 2;
|
666
|
+
|
667
|
+
// If this forward path overlaps the reverse path in this diagonal,
|
668
|
+
// then this is the middle change of the index intervals.
|
669
|
+
if (iR <= iMaxR && aIndexesR[iR] - 1 <= aLast) {
|
670
|
+
// Unlike the Myers algorithm which finds only the middle “snake”
|
671
|
+
// this package can find two common subsequences per division.
|
672
|
+
// Last point of previous path segment is on an adjacent diagonal.
|
673
|
+
const bLastPrev = bF + aLastPrev - (insert ? kF + 1 : kF - 1);
|
674
|
+
|
675
|
+
// Because of invariant that intervals preceding the middle change
|
676
|
+
// cannot have common items at the end,
|
677
|
+
// move in reverse direction along a diagonal of common items.
|
678
|
+
const nCommonR = countCommonItemsR(
|
679
|
+
aStart,
|
680
|
+
aLastPrev,
|
681
|
+
bStart,
|
682
|
+
bLastPrev,
|
683
|
+
isCommon
|
684
|
+
);
|
685
|
+
const aIndexPrevFirst = aLastPrev - nCommonR;
|
686
|
+
const bIndexPrevFirst = bLastPrev - nCommonR;
|
687
|
+
const aEndPreceding = aIndexPrevFirst + 1;
|
688
|
+
const bEndPreceding = bIndexPrevFirst + 1;
|
689
|
+
division.nChangePreceding = d - 1;
|
690
|
+
if (d - 1 === aEndPreceding + bEndPreceding - aStart - bStart) {
|
691
|
+
// Optimization: number of preceding changes in forward direction
|
692
|
+
// is equal to number of items in preceding interval,
|
693
|
+
// therefore it cannot contain any common items.
|
694
|
+
division.aEndPreceding = aStart;
|
695
|
+
division.bEndPreceding = bStart;
|
696
|
+
} else {
|
697
|
+
division.aEndPreceding = aEndPreceding;
|
698
|
+
division.bEndPreceding = bEndPreceding;
|
699
|
+
}
|
700
|
+
division.nCommonPreceding = nCommonR;
|
701
|
+
if (nCommonR !== 0) {
|
702
|
+
division.aCommonPreceding = aEndPreceding;
|
703
|
+
division.bCommonPreceding = bEndPreceding;
|
704
|
+
}
|
705
|
+
division.nCommonFollowing = nCommonF;
|
706
|
+
if (nCommonF !== 0) {
|
707
|
+
division.aCommonFollowing = aFirst + 1;
|
708
|
+
division.bCommonFollowing = bFirst + 1;
|
709
|
+
}
|
710
|
+
const aStartFollowing = aLast + 1;
|
711
|
+
const bStartFollowing = bFirst + nCommonF + 1;
|
712
|
+
division.nChangeFollowing = d - 1;
|
713
|
+
if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
|
714
|
+
// Optimization: number of changes in reverse direction
|
715
|
+
// is equal to number of items in following interval,
|
716
|
+
// therefore it cannot contain any common items.
|
717
|
+
division.aStartFollowing = aEnd;
|
718
|
+
division.bStartFollowing = bEnd;
|
719
|
+
} else {
|
720
|
+
division.aStartFollowing = aStartFollowing;
|
721
|
+
division.bStartFollowing = bStartFollowing;
|
722
|
+
}
|
723
|
+
return true;
|
724
|
+
}
|
725
|
+
}
|
726
|
+
}
|
727
|
+
return false;
|
728
|
+
};
|
729
|
+
|
730
|
+
// A complete function to extend reverse paths from (d - 1) to d changes.
|
731
|
+
// Return true if a path overlaps forward path of d changes in its diagonal.
|
732
|
+
const extendOverlappablePathsR = (
|
733
|
+
d,
|
734
|
+
aStart,
|
735
|
+
aEnd,
|
736
|
+
bStart,
|
737
|
+
bEnd,
|
738
|
+
isCommon,
|
739
|
+
aIndexesF,
|
740
|
+
iMaxF,
|
741
|
+
aIndexesR,
|
742
|
+
iMaxR,
|
743
|
+
division // update prop values if return true
|
744
|
+
) => {
|
745
|
+
const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
|
746
|
+
const aLength = aEnd - aStart;
|
747
|
+
const bLength = bEnd - bStart;
|
748
|
+
const baDeltaLength = bLength - aLength; // kR = kF + baDeltaLength
|
749
|
+
|
750
|
+
// Range of diagonals in which forward and reverse paths might overlap.
|
751
|
+
const kMinOverlapR = baDeltaLength - d; // -d <= kF
|
752
|
+
const kMaxOverlapR = baDeltaLength + d; // kF <= d
|
753
|
+
|
754
|
+
let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration
|
755
|
+
|
756
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
757
|
+
const nR = d < iMaxR ? d : iMaxR;
|
758
|
+
|
759
|
+
// The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
|
760
|
+
for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {
|
761
|
+
// To get first point of path segment, move one change in reverse direction
|
762
|
+
// from last point of previous path segment in an adjacent diagonal.
|
763
|
+
// In first iteration when iR === 0 and kR === d always insert.
|
764
|
+
// In last possible iteration when iR === d and kR === -d always delete.
|
765
|
+
const insert = iR === 0 || (iR !== d && aIndexesR[iR] < aIndexPrev1);
|
766
|
+
const aLastPrev = insert ? aIndexesR[iR] : aIndexPrev1;
|
767
|
+
const aFirst = insert
|
768
|
+
? aLastPrev // vertical to insert from b
|
769
|
+
: aLastPrev - 1; // horizontal to delete from a
|
770
|
+
|
771
|
+
// To get last point of path segment, move along diagonal of common items.
|
772
|
+
const bFirst = bR + aFirst - kR;
|
773
|
+
const nCommonR = countCommonItemsR(
|
774
|
+
aStart,
|
775
|
+
aFirst - 1,
|
776
|
+
bStart,
|
777
|
+
bFirst - 1,
|
778
|
+
isCommon
|
779
|
+
);
|
780
|
+
const aLast = aFirst - nCommonR;
|
781
|
+
aIndexPrev1 = aIndexesR[iR];
|
782
|
+
aIndexesR[iR] = aLast;
|
783
|
+
if (kMinOverlapR <= kR && kR <= kMaxOverlapR) {
|
784
|
+
// Solve for iF of forward path with d changes in diagonal kR:
|
785
|
+
// kF = kR - baDeltaLength
|
786
|
+
// kF = 2 * iF - d
|
787
|
+
const iF = (d + (kR - baDeltaLength)) / 2;
|
788
|
+
|
789
|
+
// If this reverse path overlaps the forward path in this diagonal,
|
790
|
+
// then this is a middle change of the index intervals.
|
791
|
+
if (iF <= iMaxF && aLast - 1 <= aIndexesF[iF]) {
|
792
|
+
const bLast = bFirst - nCommonR;
|
793
|
+
division.nChangePreceding = d;
|
794
|
+
if (d === aLast + bLast - aStart - bStart) {
|
795
|
+
// Optimization: number of changes in reverse direction
|
796
|
+
// is equal to number of items in preceding interval,
|
797
|
+
// therefore it cannot contain any common items.
|
798
|
+
division.aEndPreceding = aStart;
|
799
|
+
division.bEndPreceding = bStart;
|
800
|
+
} else {
|
801
|
+
division.aEndPreceding = aLast;
|
802
|
+
division.bEndPreceding = bLast;
|
803
|
+
}
|
804
|
+
division.nCommonPreceding = nCommonR;
|
805
|
+
if (nCommonR !== 0) {
|
806
|
+
// The last point of reverse path segment is start of common subsequence.
|
807
|
+
division.aCommonPreceding = aLast;
|
808
|
+
division.bCommonPreceding = bLast;
|
809
|
+
}
|
810
|
+
division.nChangeFollowing = d - 1;
|
811
|
+
if (d === 1) {
|
812
|
+
// There is no previous path segment.
|
813
|
+
division.nCommonFollowing = 0;
|
814
|
+
division.aStartFollowing = aEnd;
|
815
|
+
division.bStartFollowing = bEnd;
|
816
|
+
} else {
|
817
|
+
// Unlike the Myers algorithm which finds only the middle “snake”
|
818
|
+
// this package can find two common subsequences per division.
|
819
|
+
// Last point of previous path segment is on an adjacent diagonal.
|
820
|
+
const bLastPrev = bR + aLastPrev - (insert ? kR - 1 : kR + 1);
|
821
|
+
|
822
|
+
// Because of invariant that intervals following the middle change
|
823
|
+
// cannot have common items at the start,
|
824
|
+
// move in forward direction along a diagonal of common items.
|
825
|
+
const nCommonF = countCommonItemsF(
|
826
|
+
aLastPrev,
|
827
|
+
aEnd,
|
828
|
+
bLastPrev,
|
829
|
+
bEnd,
|
830
|
+
isCommon
|
831
|
+
);
|
832
|
+
division.nCommonFollowing = nCommonF;
|
833
|
+
if (nCommonF !== 0) {
|
834
|
+
// The last point of reverse path segment is start of common subsequence.
|
835
|
+
division.aCommonFollowing = aLastPrev;
|
836
|
+
division.bCommonFollowing = bLastPrev;
|
837
|
+
}
|
838
|
+
const aStartFollowing = aLastPrev + nCommonF; // aFirstPrev
|
839
|
+
const bStartFollowing = bLastPrev + nCommonF; // bFirstPrev
|
840
|
+
|
841
|
+
if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
|
842
|
+
// Optimization: number of changes in forward direction
|
843
|
+
// is equal to number of items in following interval,
|
844
|
+
// therefore it cannot contain any common items.
|
845
|
+
division.aStartFollowing = aEnd;
|
846
|
+
division.bStartFollowing = bEnd;
|
847
|
+
} else {
|
848
|
+
division.aStartFollowing = aStartFollowing;
|
849
|
+
division.bStartFollowing = bStartFollowing;
|
850
|
+
}
|
851
|
+
}
|
852
|
+
return true;
|
853
|
+
}
|
854
|
+
}
|
855
|
+
}
|
856
|
+
return false;
|
857
|
+
};
|
858
|
+
|
859
|
+
// Given index intervals and input function to compare items at indexes,
|
860
|
+
// divide at the middle change.
|
861
|
+
//
|
862
|
+
// DO NOT CALL if start === end, because interval cannot contain common items
|
863
|
+
// and because this function will throw the “no overlap” error.
|
864
|
+
const divide = (
|
865
|
+
nChange,
|
866
|
+
aStart,
|
867
|
+
aEnd,
|
868
|
+
bStart,
|
869
|
+
bEnd,
|
870
|
+
isCommon,
|
871
|
+
aIndexesF,
|
872
|
+
aIndexesR,
|
873
|
+
division // output
|
874
|
+
) => {
|
875
|
+
const bF = bStart - aStart; // bIndex = bF + aIndex - kF
|
876
|
+
const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
|
877
|
+
const aLength = aEnd - aStart;
|
878
|
+
const bLength = bEnd - bStart;
|
879
|
+
|
880
|
+
// Because graph has square or portrait orientation,
|
881
|
+
// length difference is minimum number of items to insert from b.
|
882
|
+
// Corresponding forward and reverse diagonals in graph
|
883
|
+
// depend on length difference of the sequences:
|
884
|
+
// kF = kR - baDeltaLength
|
885
|
+
// kR = kF + baDeltaLength
|
886
|
+
const baDeltaLength = bLength - aLength;
|
887
|
+
|
888
|
+
// Optimization: max diagonal in graph intersects corner of shorter side.
|
889
|
+
let iMaxF = aLength;
|
890
|
+
let iMaxR = aLength;
|
891
|
+
|
892
|
+
// Initialize no changes yet in forward or reverse direction:
|
893
|
+
aIndexesF[0] = aStart - 1; // at open start of interval, outside closed start
|
894
|
+
aIndexesR[0] = aEnd; // at open end of interval
|
895
|
+
|
896
|
+
if (baDeltaLength % 2 === 0) {
|
897
|
+
// The number of changes in paths is 2 * d if length difference is even.
|
898
|
+
const dMin = (nChange || baDeltaLength) / 2;
|
899
|
+
const dMax = (aLength + bLength) / 2;
|
900
|
+
for (let d = 1; d <= dMax; d += 1) {
|
901
|
+
iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
|
902
|
+
if (d < dMin) {
|
903
|
+
iMaxR = extendPathsR(d, aStart, bStart, bR, isCommon, aIndexesR, iMaxR);
|
904
|
+
} else if (
|
905
|
+
// If a reverse path overlaps a forward path in the same diagonal,
|
906
|
+
// return a division of the index intervals at the middle change.
|
907
|
+
extendOverlappablePathsR(
|
908
|
+
d,
|
909
|
+
aStart,
|
910
|
+
aEnd,
|
911
|
+
bStart,
|
912
|
+
bEnd,
|
913
|
+
isCommon,
|
914
|
+
aIndexesF,
|
915
|
+
iMaxF,
|
916
|
+
aIndexesR,
|
917
|
+
iMaxR,
|
918
|
+
division
|
919
|
+
)
|
920
|
+
) {
|
921
|
+
return;
|
922
|
+
}
|
923
|
+
}
|
924
|
+
} else {
|
925
|
+
// The number of changes in paths is 2 * d - 1 if length difference is odd.
|
926
|
+
const dMin = ((nChange || baDeltaLength) + 1) / 2;
|
927
|
+
const dMax = (aLength + bLength + 1) / 2;
|
928
|
+
|
929
|
+
// Unroll first half iteration so loop extends the relevant pairs of paths.
|
930
|
+
// Because of invariant that intervals have no common items at start or end,
|
931
|
+
// and limitation not to call divide with empty intervals,
|
932
|
+
// therefore it cannot be called if a forward path with one change
|
933
|
+
// would overlap a reverse path with no changes, even if dMin === 1.
|
934
|
+
let d = 1;
|
935
|
+
iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
|
936
|
+
for (d += 1; d <= dMax; d += 1) {
|
937
|
+
iMaxR = extendPathsR(
|
938
|
+
d - 1,
|
939
|
+
aStart,
|
940
|
+
bStart,
|
941
|
+
bR,
|
942
|
+
isCommon,
|
943
|
+
aIndexesR,
|
944
|
+
iMaxR
|
945
|
+
);
|
946
|
+
if (d < dMin) {
|
947
|
+
iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
|
948
|
+
} else if (
|
949
|
+
// If a forward path overlaps a reverse path in the same diagonal,
|
950
|
+
// return a division of the index intervals at the middle change.
|
951
|
+
extendOverlappablePathsF(
|
952
|
+
d,
|
953
|
+
aStart,
|
954
|
+
aEnd,
|
955
|
+
bStart,
|
956
|
+
bEnd,
|
957
|
+
isCommon,
|
958
|
+
aIndexesF,
|
959
|
+
iMaxF,
|
960
|
+
aIndexesR,
|
961
|
+
iMaxR,
|
962
|
+
division
|
963
|
+
)
|
964
|
+
) {
|
965
|
+
return;
|
966
|
+
}
|
967
|
+
}
|
968
|
+
}
|
969
|
+
|
970
|
+
/* istanbul ignore next */
|
971
|
+
throw new Error(
|
972
|
+
`${pkg}: no overlap aStart=${aStart} aEnd=${aEnd} bStart=${bStart} bEnd=${bEnd}`
|
973
|
+
);
|
974
|
+
};
|
975
|
+
|
976
|
+
// Given index intervals and input function to compare items at indexes,
|
977
|
+
// return by output function the number of adjacent items and starting indexes
|
978
|
+
// of each common subsequence. Divide and conquer with only linear space.
|
979
|
+
//
|
980
|
+
// The index intervals are half open [start, end) like array slice method.
|
981
|
+
// DO NOT CALL if start === end, because interval cannot contain common items
|
982
|
+
// and because divide function will throw the “no overlap” error.
|
983
|
+
const findSubsequences = (
|
984
|
+
nChange,
|
985
|
+
aStart,
|
986
|
+
aEnd,
|
987
|
+
bStart,
|
988
|
+
bEnd,
|
989
|
+
transposed,
|
990
|
+
callbacks,
|
991
|
+
aIndexesF,
|
992
|
+
aIndexesR,
|
993
|
+
division // temporary memory, not input nor output
|
994
|
+
) => {
|
995
|
+
if (bEnd - bStart < aEnd - aStart) {
|
996
|
+
// Transpose graph so it has portrait instead of landscape orientation.
|
997
|
+
// Always compare shorter to longer sequence for consistency and optimization.
|
998
|
+
transposed = !transposed;
|
999
|
+
if (transposed && callbacks.length === 1) {
|
1000
|
+
// Lazily wrap callback functions to swap args if graph is transposed.
|
1001
|
+
const {foundSubsequence, isCommon} = callbacks[0];
|
1002
|
+
callbacks[1] = {
|
1003
|
+
foundSubsequence: (nCommon, bCommon, aCommon) => {
|
1004
|
+
foundSubsequence(nCommon, aCommon, bCommon);
|
1005
|
+
},
|
1006
|
+
isCommon: (bIndex, aIndex) => isCommon(aIndex, bIndex)
|
1007
|
+
};
|
1008
|
+
}
|
1009
|
+
const tStart = aStart;
|
1010
|
+
const tEnd = aEnd;
|
1011
|
+
aStart = bStart;
|
1012
|
+
aEnd = bEnd;
|
1013
|
+
bStart = tStart;
|
1014
|
+
bEnd = tEnd;
|
1015
|
+
}
|
1016
|
+
const {foundSubsequence, isCommon} = callbacks[transposed ? 1 : 0];
|
1017
|
+
|
1018
|
+
// Divide the index intervals at the middle change.
|
1019
|
+
divide(
|
1020
|
+
nChange,
|
1021
|
+
aStart,
|
1022
|
+
aEnd,
|
1023
|
+
bStart,
|
1024
|
+
bEnd,
|
1025
|
+
isCommon,
|
1026
|
+
aIndexesF,
|
1027
|
+
aIndexesR,
|
1028
|
+
division
|
1029
|
+
);
|
1030
|
+
const {
|
1031
|
+
nChangePreceding,
|
1032
|
+
aEndPreceding,
|
1033
|
+
bEndPreceding,
|
1034
|
+
nCommonPreceding,
|
1035
|
+
aCommonPreceding,
|
1036
|
+
bCommonPreceding,
|
1037
|
+
nCommonFollowing,
|
1038
|
+
aCommonFollowing,
|
1039
|
+
bCommonFollowing,
|
1040
|
+
nChangeFollowing,
|
1041
|
+
aStartFollowing,
|
1042
|
+
bStartFollowing
|
1043
|
+
} = division;
|
1044
|
+
|
1045
|
+
// Unless either index interval is empty, they might contain common items.
|
1046
|
+
if (aStart < aEndPreceding && bStart < bEndPreceding) {
|
1047
|
+
// Recursely find and return common subsequences preceding the division.
|
1048
|
+
findSubsequences(
|
1049
|
+
nChangePreceding,
|
1050
|
+
aStart,
|
1051
|
+
aEndPreceding,
|
1052
|
+
bStart,
|
1053
|
+
bEndPreceding,
|
1054
|
+
transposed,
|
1055
|
+
callbacks,
|
1056
|
+
aIndexesF,
|
1057
|
+
aIndexesR,
|
1058
|
+
division
|
1059
|
+
);
|
1060
|
+
}
|
1061
|
+
|
1062
|
+
// Return common subsequences that are adjacent to the middle change.
|
1063
|
+
if (nCommonPreceding !== 0) {
|
1064
|
+
foundSubsequence(nCommonPreceding, aCommonPreceding, bCommonPreceding);
|
1065
|
+
}
|
1066
|
+
if (nCommonFollowing !== 0) {
|
1067
|
+
foundSubsequence(nCommonFollowing, aCommonFollowing, bCommonFollowing);
|
1068
|
+
}
|
1069
|
+
|
1070
|
+
// Unless either index interval is empty, they might contain common items.
|
1071
|
+
if (aStartFollowing < aEnd && bStartFollowing < bEnd) {
|
1072
|
+
// Recursely find and return common subsequences following the division.
|
1073
|
+
findSubsequences(
|
1074
|
+
nChangeFollowing,
|
1075
|
+
aStartFollowing,
|
1076
|
+
aEnd,
|
1077
|
+
bStartFollowing,
|
1078
|
+
bEnd,
|
1079
|
+
transposed,
|
1080
|
+
callbacks,
|
1081
|
+
aIndexesF,
|
1082
|
+
aIndexesR,
|
1083
|
+
division
|
1084
|
+
);
|
1085
|
+
}
|
1086
|
+
};
|
1087
|
+
const validateLength = (name, arg) => {
|
1088
|
+
if (typeof arg !== 'number') {
|
1089
|
+
throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
|
1090
|
+
}
|
1091
|
+
if (!Number.isSafeInteger(arg)) {
|
1092
|
+
throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
|
1093
|
+
}
|
1094
|
+
if (arg < 0) {
|
1095
|
+
throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
|
1096
|
+
}
|
1097
|
+
};
|
1098
|
+
const validateCallback = (name, arg) => {
|
1099
|
+
const type = typeof arg;
|
1100
|
+
if (type !== 'function') {
|
1101
|
+
throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
|
1102
|
+
}
|
1103
|
+
};
|
1104
|
+
|
1105
|
+
// Compare items in two sequences to find a longest common subsequence.
|
1106
|
+
// Given lengths of sequences and input function to compare items at indexes,
|
1107
|
+
// return by output function the number of adjacent items and starting indexes
|
1108
|
+
// of each common subsequence.
|
1109
|
+
function diffSequence(aLength, bLength, isCommon, foundSubsequence) {
|
1110
|
+
validateLength('aLength', aLength);
|
1111
|
+
validateLength('bLength', bLength);
|
1112
|
+
validateCallback('isCommon', isCommon);
|
1113
|
+
validateCallback('foundSubsequence', foundSubsequence);
|
1114
|
+
|
1115
|
+
// Count common items from the start in the forward direction.
|
1116
|
+
const nCommonF = countCommonItemsF(0, aLength, 0, bLength, isCommon);
|
1117
|
+
if (nCommonF !== 0) {
|
1118
|
+
foundSubsequence(nCommonF, 0, 0);
|
1119
|
+
}
|
1120
|
+
|
1121
|
+
// Unless both sequences consist of common items only,
|
1122
|
+
// find common items in the half-trimmed index intervals.
|
1123
|
+
if (aLength !== nCommonF || bLength !== nCommonF) {
|
1124
|
+
// Invariant: intervals do not have common items at the start.
|
1125
|
+
// The start of an index interval is closed like array slice method.
|
1126
|
+
const aStart = nCommonF;
|
1127
|
+
const bStart = nCommonF;
|
1128
|
+
|
1129
|
+
// Count common items from the end in the reverse direction.
|
1130
|
+
const nCommonR = countCommonItemsR(
|
1131
|
+
aStart,
|
1132
|
+
aLength - 1,
|
1133
|
+
bStart,
|
1134
|
+
bLength - 1,
|
1135
|
+
isCommon
|
1136
|
+
);
|
1137
|
+
|
1138
|
+
// Invariant: intervals do not have common items at the end.
|
1139
|
+
// The end of an index interval is open like array slice method.
|
1140
|
+
const aEnd = aLength - nCommonR;
|
1141
|
+
const bEnd = bLength - nCommonR;
|
1142
|
+
|
1143
|
+
// Unless one sequence consists of common items only,
|
1144
|
+
// therefore the other trimmed index interval consists of changes only,
|
1145
|
+
// find common items in the trimmed index intervals.
|
1146
|
+
const nCommonFR = nCommonF + nCommonR;
|
1147
|
+
if (aLength !== nCommonFR && bLength !== nCommonFR) {
|
1148
|
+
const nChange = 0; // number of change items is not yet known
|
1149
|
+
const transposed = false; // call the original unwrapped functions
|
1150
|
+
const callbacks = [
|
1151
|
+
{
|
1152
|
+
foundSubsequence,
|
1153
|
+
isCommon
|
1154
|
+
}
|
1155
|
+
];
|
1156
|
+
|
1157
|
+
// Indexes in sequence a of last points in furthest reaching paths
|
1158
|
+
// from outside the start at top left in the forward direction:
|
1159
|
+
const aIndexesF = [NOT_YET_SET];
|
1160
|
+
// from the end at bottom right in the reverse direction:
|
1161
|
+
const aIndexesR = [NOT_YET_SET];
|
1162
|
+
|
1163
|
+
// Initialize one object as output of all calls to divide function.
|
1164
|
+
const division = {
|
1165
|
+
aCommonFollowing: NOT_YET_SET,
|
1166
|
+
aCommonPreceding: NOT_YET_SET,
|
1167
|
+
aEndPreceding: NOT_YET_SET,
|
1168
|
+
aStartFollowing: NOT_YET_SET,
|
1169
|
+
bCommonFollowing: NOT_YET_SET,
|
1170
|
+
bCommonPreceding: NOT_YET_SET,
|
1171
|
+
bEndPreceding: NOT_YET_SET,
|
1172
|
+
bStartFollowing: NOT_YET_SET,
|
1173
|
+
nChangeFollowing: NOT_YET_SET,
|
1174
|
+
nChangePreceding: NOT_YET_SET,
|
1175
|
+
nCommonFollowing: NOT_YET_SET,
|
1176
|
+
nCommonPreceding: NOT_YET_SET
|
1177
|
+
};
|
1178
|
+
|
1179
|
+
// Find and return common subsequences in the trimmed index intervals.
|
1180
|
+
findSubsequences(
|
1181
|
+
nChange,
|
1182
|
+
aStart,
|
1183
|
+
aEnd,
|
1184
|
+
bStart,
|
1185
|
+
bEnd,
|
1186
|
+
transposed,
|
1187
|
+
callbacks,
|
1188
|
+
aIndexesF,
|
1189
|
+
aIndexesR,
|
1190
|
+
division
|
1191
|
+
);
|
1192
|
+
}
|
1193
|
+
if (nCommonR !== 0) {
|
1194
|
+
foundSubsequence(nCommonR, aEnd, bEnd);
|
1195
|
+
}
|
1196
|
+
}
|
1197
|
+
}
|
1198
|
+
|
401
1199
|
function formatTrailingSpaces(line, trailingSpaceFormatter) {
|
402
1200
|
return line.replace(/\s+$/, (match) => trailingSpaceFormatter(match));
|
403
1201
|
}
|
@@ -612,7 +1410,6 @@ const noColor = (string) => string;
|
|
612
1410
|
const DIFF_CONTEXT_DEFAULT = 5;
|
613
1411
|
const DIFF_TRUNCATE_THRESHOLD_DEFAULT = 0;
|
614
1412
|
function getDefaultOptions() {
|
615
|
-
const c = getColors();
|
616
1413
|
return {
|
617
1414
|
aAnnotation: "Expected",
|
618
1415
|
aColor: c.green,
|
@@ -658,8 +1455,8 @@ function isEmptyString(lines) {
|
|
658
1455
|
function countChanges(diffs) {
|
659
1456
|
let a = 0;
|
660
1457
|
let b = 0;
|
661
|
-
diffs.forEach((
|
662
|
-
switch (
|
1458
|
+
diffs.forEach((diff) => {
|
1459
|
+
switch (diff[0]) {
|
663
1460
|
case DIFF_DELETE:
|
664
1461
|
a += 1;
|
665
1462
|
break;
|
@@ -736,18 +1533,18 @@ function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCo
|
|
736
1533
|
);
|
737
1534
|
let aIndex = 0;
|
738
1535
|
let bIndex = 0;
|
739
|
-
diffs.forEach((
|
740
|
-
switch (
|
1536
|
+
diffs.forEach((diff) => {
|
1537
|
+
switch (diff[0]) {
|
741
1538
|
case DIFF_DELETE:
|
742
|
-
|
1539
|
+
diff[1] = aLinesDisplay[aIndex];
|
743
1540
|
aIndex += 1;
|
744
1541
|
break;
|
745
1542
|
case DIFF_INSERT:
|
746
|
-
|
1543
|
+
diff[1] = bLinesDisplay[bIndex];
|
747
1544
|
bIndex += 1;
|
748
1545
|
break;
|
749
1546
|
default:
|
750
|
-
|
1547
|
+
diff[1] = bLinesDisplay[bIndex];
|
751
1548
|
aIndex += 1;
|
752
1549
|
bIndex += 1;
|
753
1550
|
}
|
@@ -778,8 +1575,7 @@ function diffLinesRaw(aLines, bLines, options) {
|
|
778
1575
|
diffs.push(new Diff(DIFF_EQUAL, bLines[bIndex]));
|
779
1576
|
}
|
780
1577
|
};
|
781
|
-
|
782
|
-
diffSequences(aLength, bLength, isCommon, foundSubsequence);
|
1578
|
+
_default(aLength, bLength, isCommon, foundSubsequence);
|
783
1579
|
for (; aIndex !== aLength; aIndex += 1) {
|
784
1580
|
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
785
1581
|
}
|
@@ -828,8 +1624,7 @@ function diffStrings(a, b, options) {
|
|
828
1624
|
bIndex = bCommon + nCommon;
|
829
1625
|
diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
|
830
1626
|
};
|
831
|
-
|
832
|
-
diffSequences(aLength, bLength, isCommon, foundSubsequence);
|
1627
|
+
_default(aLength, bLength, isCommon, foundSubsequence);
|
833
1628
|
if (aIndex !== aLength) {
|
834
1629
|
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
|
835
1630
|
}
|
package/dist/error.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { D as DiffOptions } from './types-
|
2
|
-
import 'pretty-format';
|
1
|
+
import { D as DiffOptions } from './types-Bxe-2Udy.js';
|
2
|
+
import '@vitest/pretty-format';
|
3
3
|
|
4
4
|
declare function serializeError(val: any, seen?: WeakMap<WeakKey, any>): any;
|
5
5
|
declare function processError(err: any, diffOptions?: DiffOptions, seen?: WeakSet<WeakKey>): any;
|
package/dist/error.js
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
import { diff } from './diff.js';
|
2
2
|
import { f as format, s as stringify } from './chunk-display.js';
|
3
3
|
import { deepClone, getOwnProperties, getType } from './helpers.js';
|
4
|
-
import 'pretty-format';
|
5
|
-
import '
|
6
|
-
import './chunk-colors.js';
|
4
|
+
import '@vitest/pretty-format';
|
5
|
+
import 'tinyrainbow';
|
7
6
|
import 'loupe';
|
8
7
|
|
9
8
|
const IS_RECORD_SYMBOL = "@@__IMMUTABLE_RECORD__@@";
|
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
export { DeferPromise, assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isNegativeNaN, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray } from './helpers.js';
|
2
2
|
export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, ErrorWithDiff, MergeInsertions, MutableArray, Nullable, ParsedStack } from './types.js';
|
3
|
-
import { PrettyFormatOptions } from 'pretty-format';
|
3
|
+
import { PrettyFormatOptions } from '@vitest/pretty-format';
|
4
|
+
import { Colors } from 'tinyrainbow';
|
4
5
|
|
5
6
|
declare function stringify(object: unknown, maxDepth?: number, { maxLength, ...options }?: PrettyFormatOptions & {
|
6
7
|
maxLength?: number;
|
@@ -41,50 +42,6 @@ declare function objDisplay(obj: unknown, options?: LoupeOptions): string;
|
|
41
42
|
declare const SAFE_TIMERS_SYMBOL: unique symbol;
|
42
43
|
declare const SAFE_COLORS_SYMBOL: unique symbol;
|
43
44
|
|
44
|
-
declare const colorsMap: {
|
45
|
-
readonly bold: readonly ["\u001B[1m", "\u001B[22m", "\u001B[22m\u001B[1m"];
|
46
|
-
readonly dim: readonly ["\u001B[2m", "\u001B[22m", "\u001B[22m\u001B[2m"];
|
47
|
-
readonly italic: readonly ["\u001B[3m", "\u001B[23m"];
|
48
|
-
readonly underline: readonly ["\u001B[4m", "\u001B[24m"];
|
49
|
-
readonly inverse: readonly ["\u001B[7m", "\u001B[27m"];
|
50
|
-
readonly hidden: readonly ["\u001B[8m", "\u001B[28m"];
|
51
|
-
readonly strikethrough: readonly ["\u001B[9m", "\u001B[29m"];
|
52
|
-
readonly black: readonly ["\u001B[30m", "\u001B[39m"];
|
53
|
-
readonly red: readonly ["\u001B[31m", "\u001B[39m"];
|
54
|
-
readonly green: readonly ["\u001B[32m", "\u001B[39m"];
|
55
|
-
readonly yellow: readonly ["\u001B[33m", "\u001B[39m"];
|
56
|
-
readonly blue: readonly ["\u001B[34m", "\u001B[39m"];
|
57
|
-
readonly magenta: readonly ["\u001B[35m", "\u001B[39m"];
|
58
|
-
readonly cyan: readonly ["\u001B[36m", "\u001B[39m"];
|
59
|
-
readonly white: readonly ["\u001B[37m", "\u001B[39m"];
|
60
|
-
readonly gray: readonly ["\u001B[90m", "\u001B[39m"];
|
61
|
-
readonly bgBlack: readonly ["\u001B[40m", "\u001B[49m"];
|
62
|
-
readonly bgRed: readonly ["\u001B[41m", "\u001B[49m"];
|
63
|
-
readonly bgGreen: readonly ["\u001B[42m", "\u001B[49m"];
|
64
|
-
readonly bgYellow: readonly ["\u001B[43m", "\u001B[49m"];
|
65
|
-
readonly bgBlue: readonly ["\u001B[44m", "\u001B[49m"];
|
66
|
-
readonly bgMagenta: readonly ["\u001B[45m", "\u001B[49m"];
|
67
|
-
readonly bgCyan: readonly ["\u001B[46m", "\u001B[49m"];
|
68
|
-
readonly bgWhite: readonly ["\u001B[47m", "\u001B[49m"];
|
69
|
-
};
|
70
|
-
type ColorName = keyof typeof colorsMap;
|
71
|
-
interface ColorMethod {
|
72
|
-
(input: unknown): string;
|
73
|
-
open: string;
|
74
|
-
close: string;
|
75
|
-
}
|
76
|
-
type ColorsMethods = {
|
77
|
-
[Key in ColorName]: ColorMethod;
|
78
|
-
};
|
79
|
-
type Colors$1 = ColorsMethods & {
|
80
|
-
isColorSupported: boolean;
|
81
|
-
reset: (input: unknown) => string;
|
82
|
-
};
|
83
|
-
declare function getDefaultColors(): Colors$1;
|
84
|
-
declare function getColors(): Colors$1;
|
85
|
-
declare function createColors(isTTY?: boolean): Colors$1;
|
86
|
-
declare function setupColors(colors: Colors$1): void;
|
87
|
-
|
88
45
|
interface ErrorOptions {
|
89
46
|
message?: string;
|
90
47
|
stackTraceLimit?: number;
|
@@ -100,11 +57,10 @@ declare const lineSplitRE: RegExp;
|
|
100
57
|
declare function positionToOffset(source: string, lineNumber: number, columnNumber: number): number;
|
101
58
|
declare function offsetToLineNumber(source: string, offset: number): number;
|
102
59
|
|
103
|
-
type Colors = Record<ColorName, (input: string) => string>;
|
104
60
|
interface HighlightOptions {
|
105
61
|
jsx?: boolean;
|
106
62
|
colors?: Colors;
|
107
63
|
}
|
108
64
|
declare function highlight(code: string, options?: HighlightOptions): string;
|
109
65
|
|
110
|
-
export {
|
66
|
+
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, createSimpleStackTrace, format, getSafeTimers, highlight, inspect, lineSplitRE, objDisplay, offsetToLineNumber, positionToOffset, setSafeTimers, shuffle, stringify };
|
package/dist/index.js
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
export { assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isNegativeNaN, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray } from './helpers.js';
|
2
2
|
export { f as format, i as inspect, o as objDisplay, s as stringify } from './chunk-display.js';
|
3
|
-
import
|
4
|
-
|
5
|
-
import 'pretty-format';
|
3
|
+
import c from 'tinyrainbow';
|
4
|
+
import '@vitest/pretty-format';
|
6
5
|
import 'loupe';
|
7
6
|
|
7
|
+
const SAFE_TIMERS_SYMBOL = Symbol("vitest:SAFE_TIMERS");
|
8
|
+
const SAFE_COLORS_SYMBOL = Symbol("vitest:SAFE_COLORS");
|
9
|
+
|
8
10
|
function getSafeTimers() {
|
9
11
|
const {
|
10
12
|
setTimeout: safeSetTimeout,
|
@@ -610,35 +612,35 @@ function highlight$1(code, options = { jsx: !1, colors: {} }) {
|
|
610
612
|
return code && highlightTokens(options.colors || {}, code, options.jsx);
|
611
613
|
}
|
612
614
|
|
613
|
-
function getDefs(
|
614
|
-
const Invalid = (text) =>
|
615
|
+
function getDefs(c2) {
|
616
|
+
const Invalid = (text) => c2.white(c2.bgRed(c2.bold(text)));
|
615
617
|
return {
|
616
|
-
Keyword:
|
617
|
-
IdentifierCapitalized:
|
618
|
-
Punctuator:
|
619
|
-
StringLiteral:
|
620
|
-
NoSubstitutionTemplate:
|
621
|
-
MultiLineComment:
|
622
|
-
SingleLineComment:
|
623
|
-
RegularExpressionLiteral:
|
624
|
-
NumericLiteral:
|
625
|
-
TemplateHead: (text) =>
|
626
|
-
TemplateTail: (text) =>
|
627
|
-
TemplateMiddle: (text) =>
|
628
|
-
IdentifierCallable:
|
629
|
-
PrivateIdentifierCallable: (text) => `#${
|
618
|
+
Keyword: c2.magenta,
|
619
|
+
IdentifierCapitalized: c2.yellow,
|
620
|
+
Punctuator: c2.yellow,
|
621
|
+
StringLiteral: c2.green,
|
622
|
+
NoSubstitutionTemplate: c2.green,
|
623
|
+
MultiLineComment: c2.gray,
|
624
|
+
SingleLineComment: c2.gray,
|
625
|
+
RegularExpressionLiteral: c2.cyan,
|
626
|
+
NumericLiteral: c2.blue,
|
627
|
+
TemplateHead: (text) => c2.green(text.slice(0, text.length - 2)) + c2.cyan(text.slice(-2)),
|
628
|
+
TemplateTail: (text) => c2.cyan(text.slice(0, 1)) + c2.green(text.slice(1)),
|
629
|
+
TemplateMiddle: (text) => c2.cyan(text.slice(0, 1)) + c2.green(text.slice(1, text.length - 2)) + c2.cyan(text.slice(-2)),
|
630
|
+
IdentifierCallable: c2.blue,
|
631
|
+
PrivateIdentifierCallable: (text) => `#${c2.blue(text.slice(1))}`,
|
630
632
|
Invalid,
|
631
|
-
JSXString:
|
632
|
-
JSXIdentifier:
|
633
|
+
JSXString: c2.green,
|
634
|
+
JSXIdentifier: c2.yellow,
|
633
635
|
JSXInvalid: Invalid,
|
634
|
-
JSXPunctuator:
|
636
|
+
JSXPunctuator: c2.yellow
|
635
637
|
};
|
636
638
|
}
|
637
639
|
function highlight(code, options = { jsx: false }) {
|
638
640
|
return highlight$1(code, {
|
639
641
|
jsx: options.jsx,
|
640
|
-
colors: getDefs(options.colors ||
|
642
|
+
colors: getDefs(options.colors || c)
|
641
643
|
});
|
642
644
|
}
|
643
645
|
|
644
|
-
export { SAFE_TIMERS_SYMBOL, createSimpleStackTrace,
|
646
|
+
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, createSimpleStackTrace, getSafeTimers, highlight, lineSplitRE, offsetToLineNumber, positionToOffset, setSafeTimers, shuffle };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "2.0.
|
4
|
+
"version": "2.0.2",
|
5
5
|
"description": "Shared Vitest utility functions",
|
6
6
|
"license": "MIT",
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
@@ -60,14 +60,15 @@
|
|
60
60
|
"dist"
|
61
61
|
],
|
62
62
|
"dependencies": {
|
63
|
-
"diff-sequences": "^29.6.3",
|
64
63
|
"estree-walker": "^3.0.3",
|
65
64
|
"loupe": "^3.1.1",
|
66
|
-
"
|
65
|
+
"tinyrainbow": "^1.2.0",
|
66
|
+
"@vitest/pretty-format": "2.0.2"
|
67
67
|
},
|
68
68
|
"devDependencies": {
|
69
69
|
"@jridgewell/trace-mapping": "^0.3.25",
|
70
70
|
"@types/estree": "^1.0.5",
|
71
|
+
"diff-sequences": "^29.6.3",
|
71
72
|
"tinyhighlight": "^0.3.2"
|
72
73
|
},
|
73
74
|
"scripts": {
|
package/dist/chunk-colors.js
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
const SAFE_TIMERS_SYMBOL = Symbol("vitest:SAFE_TIMERS");
|
2
|
-
const SAFE_COLORS_SYMBOL = Symbol("vitest:SAFE_COLORS");
|
3
|
-
|
4
|
-
const colorsMap = {
|
5
|
-
bold: ["\x1B[1m", "\x1B[22m", "\x1B[22m\x1B[1m"],
|
6
|
-
dim: ["\x1B[2m", "\x1B[22m", "\x1B[22m\x1B[2m"],
|
7
|
-
italic: ["\x1B[3m", "\x1B[23m"],
|
8
|
-
underline: ["\x1B[4m", "\x1B[24m"],
|
9
|
-
inverse: ["\x1B[7m", "\x1B[27m"],
|
10
|
-
hidden: ["\x1B[8m", "\x1B[28m"],
|
11
|
-
strikethrough: ["\x1B[9m", "\x1B[29m"],
|
12
|
-
black: ["\x1B[30m", "\x1B[39m"],
|
13
|
-
red: ["\x1B[31m", "\x1B[39m"],
|
14
|
-
green: ["\x1B[32m", "\x1B[39m"],
|
15
|
-
yellow: ["\x1B[33m", "\x1B[39m"],
|
16
|
-
blue: ["\x1B[34m", "\x1B[39m"],
|
17
|
-
magenta: ["\x1B[35m", "\x1B[39m"],
|
18
|
-
cyan: ["\x1B[36m", "\x1B[39m"],
|
19
|
-
white: ["\x1B[37m", "\x1B[39m"],
|
20
|
-
gray: ["\x1B[90m", "\x1B[39m"],
|
21
|
-
bgBlack: ["\x1B[40m", "\x1B[49m"],
|
22
|
-
bgRed: ["\x1B[41m", "\x1B[49m"],
|
23
|
-
bgGreen: ["\x1B[42m", "\x1B[49m"],
|
24
|
-
bgYellow: ["\x1B[43m", "\x1B[49m"],
|
25
|
-
bgBlue: ["\x1B[44m", "\x1B[49m"],
|
26
|
-
bgMagenta: ["\x1B[45m", "\x1B[49m"],
|
27
|
-
bgCyan: ["\x1B[46m", "\x1B[49m"],
|
28
|
-
bgWhite: ["\x1B[47m", "\x1B[49m"]
|
29
|
-
};
|
30
|
-
const colorsEntries = Object.entries(colorsMap);
|
31
|
-
function string(str) {
|
32
|
-
return String(str);
|
33
|
-
}
|
34
|
-
string.open = "";
|
35
|
-
string.close = "";
|
36
|
-
const defaultColors = /* @__PURE__ */ colorsEntries.reduce(
|
37
|
-
(acc, [key]) => {
|
38
|
-
acc[key] = string;
|
39
|
-
return acc;
|
40
|
-
},
|
41
|
-
{ isColorSupported: false }
|
42
|
-
);
|
43
|
-
function getDefaultColors() {
|
44
|
-
return { ...defaultColors };
|
45
|
-
}
|
46
|
-
function getColors() {
|
47
|
-
return globalThis[SAFE_COLORS_SYMBOL] || defaultColors;
|
48
|
-
}
|
49
|
-
function createColors(isTTY = false) {
|
50
|
-
const enabled = typeof process !== "undefined" && !("NO_COLOR" in process.env || process.argv.includes("--no-color")) && !("GITHUB_ACTIONS" in process.env) && ("FORCE_COLOR" in process.env || process.argv.includes("--color") || process.platform === "win32" || isTTY && process.env.TERM !== "dumb" || "CI" in process.env);
|
51
|
-
const replaceClose = (string2, close, replace, index) => {
|
52
|
-
let result = "";
|
53
|
-
let cursor = 0;
|
54
|
-
do {
|
55
|
-
result += string2.substring(cursor, index) + replace;
|
56
|
-
cursor = index + close.length;
|
57
|
-
index = string2.indexOf(close, cursor);
|
58
|
-
} while (~index);
|
59
|
-
return result + string2.substring(cursor);
|
60
|
-
};
|
61
|
-
const formatter = (open, close, replace = open) => {
|
62
|
-
const fn = (input) => {
|
63
|
-
const string2 = String(input);
|
64
|
-
const index = string2.indexOf(close, open.length);
|
65
|
-
return ~index ? open + replaceClose(string2, close, replace, index) + close : open + string2 + close;
|
66
|
-
};
|
67
|
-
fn.open = open;
|
68
|
-
fn.close = close;
|
69
|
-
return fn;
|
70
|
-
};
|
71
|
-
const colorsObject = {
|
72
|
-
isColorSupported: enabled,
|
73
|
-
reset: enabled ? (s) => `\x1B[0m${s}\x1B[0m` : string
|
74
|
-
};
|
75
|
-
for (const [name, formatterArgs] of colorsEntries) {
|
76
|
-
colorsObject[name] = enabled ? formatter(...formatterArgs) : string;
|
77
|
-
}
|
78
|
-
return colorsObject;
|
79
|
-
}
|
80
|
-
function setupColors(colors) {
|
81
|
-
globalThis[SAFE_COLORS_SYMBOL] = colors;
|
82
|
-
}
|
83
|
-
|
84
|
-
export { SAFE_TIMERS_SYMBOL as S, SAFE_COLORS_SYMBOL as a, getDefaultColors as b, createColors as c, getColors as g, setupColors as s };
|