@tanstack/react-query-devtools 4.18.0 → 4.19.1
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.
|
@@ -734,7 +734,6 @@
|
|
|
734
734
|
MATCHES: 1,
|
|
735
735
|
NO_MATCH: 0
|
|
736
736
|
};
|
|
737
|
-
|
|
738
737
|
/**
|
|
739
738
|
* Gets the highest ranking for value for the given item based on its values for the given keys
|
|
740
739
|
* @param {*} item - the item to rank
|
|
@@ -745,10 +744,8 @@
|
|
|
745
744
|
*/
|
|
746
745
|
function rankItem(item, value, options) {
|
|
747
746
|
var _options$threshold;
|
|
748
|
-
|
|
749
747
|
options = options || {};
|
|
750
748
|
options.threshold = (_options$threshold = options.threshold) != null ? _options$threshold : rankings.MATCHES;
|
|
751
|
-
|
|
752
749
|
if (!options.accessors) {
|
|
753
750
|
// if keys is not specified, then we assume the item given is ready to be matched
|
|
754
751
|
const rank = getMatchRanking(item, value, options);
|
|
@@ -761,7 +758,6 @@
|
|
|
761
758
|
passed: rank >= options.threshold
|
|
762
759
|
};
|
|
763
760
|
}
|
|
764
|
-
|
|
765
761
|
const valuesToRank = getAllValuesToRank(item, options.accessors);
|
|
766
762
|
const rankingInfo = {
|
|
767
763
|
rankedValue: item,
|
|
@@ -770,34 +766,31 @@
|
|
|
770
766
|
accessorThreshold: options.threshold,
|
|
771
767
|
passed: false
|
|
772
768
|
};
|
|
773
|
-
|
|
774
769
|
for (let i = 0; i < valuesToRank.length; i++) {
|
|
775
770
|
const rankValue = valuesToRank[i];
|
|
776
771
|
let newRank = getMatchRanking(rankValue.itemValue, value, options);
|
|
777
772
|
const {
|
|
778
773
|
minRanking,
|
|
779
774
|
maxRanking,
|
|
780
|
-
threshold
|
|
775
|
+
threshold = options.threshold
|
|
781
776
|
} = rankValue.attributes;
|
|
782
|
-
|
|
783
777
|
if (newRank < minRanking && newRank >= rankings.MATCHES) {
|
|
784
778
|
newRank = minRanking;
|
|
785
779
|
} else if (newRank > maxRanking) {
|
|
786
780
|
newRank = maxRanking;
|
|
787
781
|
}
|
|
788
|
-
|
|
789
782
|
newRank = Math.min(newRank, maxRanking);
|
|
790
|
-
|
|
791
|
-
if (newRank > rankingInfo.rank) {
|
|
783
|
+
if (newRank >= threshold && newRank > rankingInfo.rank) {
|
|
792
784
|
rankingInfo.rank = newRank;
|
|
785
|
+
rankingInfo.passed = true;
|
|
793
786
|
rankingInfo.accessorIndex = i;
|
|
794
787
|
rankingInfo.accessorThreshold = threshold;
|
|
795
788
|
rankingInfo.rankedValue = rankValue.itemValue;
|
|
796
789
|
}
|
|
797
790
|
}
|
|
798
|
-
|
|
799
791
|
return rankingInfo;
|
|
800
792
|
}
|
|
793
|
+
|
|
801
794
|
/**
|
|
802
795
|
* Gives a rankings score based on how well the two strings match.
|
|
803
796
|
* @param {String} testString - the string to test against
|
|
@@ -805,39 +798,40 @@
|
|
|
805
798
|
* @param {Object} options - options for the match (like keepDiacritics for comparison)
|
|
806
799
|
* @returns {Number} the ranking for how well stringToRank matches testString
|
|
807
800
|
*/
|
|
808
|
-
|
|
809
801
|
function getMatchRanking(testString, stringToRank, options) {
|
|
810
802
|
testString = prepareValueForComparison(testString, options);
|
|
811
|
-
stringToRank = prepareValueForComparison(stringToRank, options);
|
|
803
|
+
stringToRank = prepareValueForComparison(stringToRank, options);
|
|
812
804
|
|
|
805
|
+
// too long
|
|
813
806
|
if (stringToRank.length > testString.length) {
|
|
814
807
|
return rankings.NO_MATCH;
|
|
815
|
-
}
|
|
816
|
-
|
|
808
|
+
}
|
|
817
809
|
|
|
810
|
+
// case sensitive equals
|
|
818
811
|
if (testString === stringToRank) {
|
|
819
812
|
return rankings.CASE_SENSITIVE_EQUAL;
|
|
820
|
-
}
|
|
821
|
-
|
|
813
|
+
}
|
|
822
814
|
|
|
815
|
+
// Lower casing before further comparison
|
|
823
816
|
testString = testString.toLowerCase();
|
|
824
|
-
stringToRank = stringToRank.toLowerCase();
|
|
817
|
+
stringToRank = stringToRank.toLowerCase();
|
|
825
818
|
|
|
819
|
+
// case insensitive equals
|
|
826
820
|
if (testString === stringToRank) {
|
|
827
821
|
return rankings.EQUAL;
|
|
828
|
-
}
|
|
829
|
-
|
|
822
|
+
}
|
|
830
823
|
|
|
824
|
+
// starts with
|
|
831
825
|
if (testString.startsWith(stringToRank)) {
|
|
832
826
|
return rankings.STARTS_WITH;
|
|
833
|
-
}
|
|
834
|
-
|
|
827
|
+
}
|
|
835
828
|
|
|
836
|
-
|
|
829
|
+
// word starts with
|
|
830
|
+
if (testString.includes(` ${stringToRank}`)) {
|
|
837
831
|
return rankings.WORD_STARTS_WITH;
|
|
838
|
-
}
|
|
839
|
-
|
|
832
|
+
}
|
|
840
833
|
|
|
834
|
+
// contains
|
|
841
835
|
if (testString.includes(stringToRank)) {
|
|
842
836
|
return rankings.CONTAINS;
|
|
843
837
|
} else if (stringToRank.length === 1) {
|
|
@@ -845,25 +839,24 @@
|
|
|
845
839
|
// isn't even contained in the testString, then
|
|
846
840
|
// it's definitely not a match.
|
|
847
841
|
return rankings.NO_MATCH;
|
|
848
|
-
}
|
|
849
|
-
|
|
842
|
+
}
|
|
850
843
|
|
|
844
|
+
// acronym
|
|
851
845
|
if (getAcronym(testString).includes(stringToRank)) {
|
|
852
846
|
return rankings.ACRONYM;
|
|
853
|
-
}
|
|
854
|
-
// rankings.MATCHES + 1 depending on how close of a match it is.
|
|
855
|
-
|
|
847
|
+
}
|
|
856
848
|
|
|
849
|
+
// will return a number between rankings.MATCHES and
|
|
850
|
+
// rankings.MATCHES + 1 depending on how close of a match it is.
|
|
857
851
|
return getClosenessRanking(testString, stringToRank);
|
|
858
852
|
}
|
|
853
|
+
|
|
859
854
|
/**
|
|
860
855
|
* Generates an acronym for a string.
|
|
861
856
|
*
|
|
862
857
|
* @param {String} string the string for which to produce the acronym
|
|
863
858
|
* @returns {String} the acronym
|
|
864
859
|
*/
|
|
865
|
-
|
|
866
|
-
|
|
867
860
|
function getAcronym(string) {
|
|
868
861
|
let acronym = '';
|
|
869
862
|
const wordsInString = string.split(' ');
|
|
@@ -875,6 +868,7 @@
|
|
|
875
868
|
});
|
|
876
869
|
return acronym;
|
|
877
870
|
}
|
|
871
|
+
|
|
878
872
|
/**
|
|
879
873
|
* Returns a score based on how spread apart the
|
|
880
874
|
* characters from the stringToRank are within the testString.
|
|
@@ -885,117 +879,96 @@
|
|
|
885
879
|
* @returns {Number} the number between rankings.MATCHES and
|
|
886
880
|
* rankings.MATCHES + 1 for how well stringToRank matches testString
|
|
887
881
|
*/
|
|
888
|
-
|
|
889
|
-
|
|
890
882
|
function getClosenessRanking(testString, stringToRank) {
|
|
891
883
|
let matchingInOrderCharCount = 0;
|
|
892
884
|
let charNumber = 0;
|
|
893
|
-
|
|
894
885
|
function findMatchingCharacter(matchChar, string, index) {
|
|
895
886
|
for (let j = index, J = string.length; j < J; j++) {
|
|
896
887
|
const stringChar = string[j];
|
|
897
|
-
|
|
898
888
|
if (stringChar === matchChar) {
|
|
899
889
|
matchingInOrderCharCount += 1;
|
|
900
890
|
return j + 1;
|
|
901
891
|
}
|
|
902
892
|
}
|
|
903
|
-
|
|
904
893
|
return -1;
|
|
905
894
|
}
|
|
906
|
-
|
|
907
895
|
function getRanking(spread) {
|
|
908
896
|
const spreadPercentage = 1 / spread;
|
|
909
897
|
const inOrderPercentage = matchingInOrderCharCount / stringToRank.length;
|
|
910
898
|
const ranking = rankings.MATCHES + inOrderPercentage * spreadPercentage;
|
|
911
899
|
return ranking;
|
|
912
900
|
}
|
|
913
|
-
|
|
914
901
|
const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0);
|
|
915
|
-
|
|
916
902
|
if (firstIndex < 0) {
|
|
917
903
|
return rankings.NO_MATCH;
|
|
918
904
|
}
|
|
919
|
-
|
|
920
905
|
charNumber = firstIndex;
|
|
921
|
-
|
|
922
906
|
for (let i = 1, I = stringToRank.length; i < I; i++) {
|
|
923
907
|
const matchChar = stringToRank[i];
|
|
924
908
|
charNumber = findMatchingCharacter(matchChar, testString, charNumber);
|
|
925
909
|
const found = charNumber > -1;
|
|
926
|
-
|
|
927
910
|
if (!found) {
|
|
928
911
|
return rankings.NO_MATCH;
|
|
929
912
|
}
|
|
930
913
|
}
|
|
931
|
-
|
|
932
914
|
const spread = charNumber - firstIndex;
|
|
933
915
|
return getRanking(spread);
|
|
934
916
|
}
|
|
917
|
+
|
|
935
918
|
/**
|
|
936
919
|
* Prepares value for comparison by stringifying it, removing diacritics (if specified)
|
|
937
920
|
* @param {String} value - the value to clean
|
|
938
921
|
* @param {Object} options - {keepDiacritics: whether to remove diacritics}
|
|
939
922
|
* @return {String} the prepared value
|
|
940
923
|
*/
|
|
941
|
-
|
|
942
924
|
function prepareValueForComparison(value, _ref) {
|
|
943
925
|
let {
|
|
944
926
|
keepDiacritics
|
|
945
927
|
} = _ref;
|
|
946
928
|
// value might not actually be a string at this point (we don't get to choose)
|
|
947
929
|
// so part of preparing the value for comparison is ensure that it is a string
|
|
948
|
-
value =
|
|
949
|
-
|
|
930
|
+
value = `${value}`; // toString
|
|
950
931
|
if (!keepDiacritics) {
|
|
951
932
|
value = removeAccents(value);
|
|
952
933
|
}
|
|
953
|
-
|
|
954
934
|
return value;
|
|
955
935
|
}
|
|
936
|
+
|
|
956
937
|
/**
|
|
957
938
|
* Gets value for key in item at arbitrarily nested keypath
|
|
958
939
|
* @param {Object} item - the item
|
|
959
940
|
* @param {Object|Function} key - the potentially nested keypath or property callback
|
|
960
941
|
* @return {Array} - an array containing the value(s) at the nested keypath
|
|
961
942
|
*/
|
|
962
|
-
|
|
963
|
-
|
|
964
943
|
function getItemValues(item, accessor) {
|
|
965
944
|
let accessorFn = accessor;
|
|
966
|
-
|
|
967
945
|
if (typeof accessor === 'object') {
|
|
968
946
|
accessorFn = accessor.accessor;
|
|
969
947
|
}
|
|
948
|
+
const value = accessorFn(item);
|
|
970
949
|
|
|
971
|
-
|
|
972
|
-
|
|
950
|
+
// because `value` can also be undefined
|
|
973
951
|
if (value == null) {
|
|
974
952
|
return [];
|
|
975
953
|
}
|
|
976
|
-
|
|
977
954
|
if (Array.isArray(value)) {
|
|
978
955
|
return value;
|
|
979
956
|
}
|
|
980
|
-
|
|
981
957
|
return [String(value)];
|
|
982
958
|
}
|
|
959
|
+
|
|
983
960
|
/**
|
|
984
961
|
* Gets all the values for the given keys in the given item and returns an array of those values
|
|
985
962
|
* @param item - the item from which the values will be retrieved
|
|
986
963
|
* @param keys - the keys to use to retrieve the values
|
|
987
964
|
* @return objects with {itemValue, attributes}
|
|
988
965
|
*/
|
|
989
|
-
|
|
990
|
-
|
|
991
966
|
function getAllValuesToRank(item, accessors) {
|
|
992
967
|
const allValues = [];
|
|
993
|
-
|
|
994
968
|
for (let j = 0, J = accessors.length; j < J; j++) {
|
|
995
969
|
const accessor = accessors[j];
|
|
996
970
|
const attributes = getAccessorAttributes(accessor);
|
|
997
971
|
const itemValues = getItemValues(item, accessor);
|
|
998
|
-
|
|
999
972
|
for (let i = 0, I = itemValues.length; i < I; i++) {
|
|
1000
973
|
allValues.push({
|
|
1001
974
|
itemValue: itemValues[i],
|
|
@@ -1003,10 +976,8 @@
|
|
|
1003
976
|
});
|
|
1004
977
|
}
|
|
1005
978
|
}
|
|
1006
|
-
|
|
1007
979
|
return allValues;
|
|
1008
980
|
}
|
|
1009
|
-
|
|
1010
981
|
const defaultKeyAttributes = {
|
|
1011
982
|
maxRanking: Infinity,
|
|
1012
983
|
minRanking: -Infinity
|
|
@@ -1016,13 +987,12 @@
|
|
|
1016
987
|
* @param accessor - the accessor from which the attributes will be retrieved
|
|
1017
988
|
* @return object containing the accessor's attributes
|
|
1018
989
|
*/
|
|
1019
|
-
|
|
1020
990
|
function getAccessorAttributes(accessor) {
|
|
1021
991
|
if (typeof accessor === 'function') {
|
|
1022
992
|
return defaultKeyAttributes;
|
|
1023
993
|
}
|
|
1024
|
-
|
|
1025
|
-
|
|
994
|
+
return {
|
|
995
|
+
...defaultKeyAttributes,
|
|
1026
996
|
...accessor
|
|
1027
997
|
};
|
|
1028
998
|
}
|