@tilde-nlp/ngx-common 8.0.5 → 8.0.6
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/fesm2022/tilde-nlp-ngx-common.mjs +52 -19
- package/fesm2022/tilde-nlp-ngx-common.mjs.map +1 -1
- package/index.d.ts +22 -15
- package/package.json +1 -1
|
@@ -670,10 +670,10 @@ class SortHelper {
|
|
|
670
670
|
});
|
|
671
671
|
}
|
|
672
672
|
/**
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
673
|
+
* Transforms the input array of objects by sorting them based on the specified sortingValueKey.
|
|
674
|
+
* @param values - An array of objects to be sorted.
|
|
675
|
+
* @param sortingValueKey - The key by which the provided values should be sorted, leave it empty if you want to sort string[].
|
|
676
|
+
*/
|
|
677
677
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
678
678
|
static sortAlphabetically(values, sortingValueKey) {
|
|
679
679
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -688,27 +688,60 @@ class SortHelper {
|
|
|
688
688
|
return values;
|
|
689
689
|
}
|
|
690
690
|
/**
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
691
|
+
* Sorts an array of values by a specified numeric property or by their numeric value if no property is specified.
|
|
692
|
+
* @param values - The array of values to be sorted. Can be an array of objects or primitive numeric values.
|
|
693
|
+
* @param sortingValueKey - The key of the numeric property in the objects to sort by.
|
|
694
|
+
* @param desc - Flag indicating whether to sort in descending (true) or ascending (false) order.
|
|
695
|
+
*/
|
|
697
696
|
static sortByNumber(values, sortingValueKey, desc = true) {
|
|
698
|
-
|
|
697
|
+
const getValueByKey = (obj, keyPath) => {
|
|
698
|
+
if (!keyPath)
|
|
699
|
+
return Number(obj ?? 0);
|
|
700
|
+
const keys = keyPath.split('.');
|
|
701
|
+
let value = obj;
|
|
702
|
+
for (const key of keys) {
|
|
703
|
+
if (value && typeof value === 'object' && key in value) {
|
|
704
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
705
|
+
value = value[key];
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return Number(value ?? 0);
|
|
709
|
+
};
|
|
699
710
|
values.sort((a, b) => {
|
|
700
|
-
const numberA =
|
|
701
|
-
const numberB =
|
|
702
|
-
return desc ?
|
|
711
|
+
const numberA = getValueByKey(a, sortingValueKey);
|
|
712
|
+
const numberB = getValueByKey(b, sortingValueKey);
|
|
713
|
+
return desc ? numberB - numberA : numberA - numberB;
|
|
703
714
|
});
|
|
704
715
|
return values;
|
|
705
716
|
}
|
|
706
717
|
/**
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
718
|
+
* Sorts an array of values by a specified boolean property or by their boolean value if no property is specified.
|
|
719
|
+
* @param values - The array of values to be sorted. Can be an array of objects or primitive boolean values.
|
|
720
|
+
* @param sortingValueKey - The key of the boolean property in the objects to sort by.
|
|
721
|
+
* @param desc - Flag indicating whether to sort in descending (true) or ascending (false) order.
|
|
722
|
+
*/
|
|
723
|
+
static sortByBoolean(values, sortingValueKey, desc = true) {
|
|
724
|
+
const getValue = (item) => {
|
|
725
|
+
if (sortingValueKey) {
|
|
726
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
727
|
+
const value = item[sortingValueKey];
|
|
728
|
+
return Boolean(value);
|
|
729
|
+
}
|
|
730
|
+
return Boolean(item);
|
|
731
|
+
};
|
|
732
|
+
values.sort((a, b) => {
|
|
733
|
+
const valA = getValue(a) ? 1 : 0;
|
|
734
|
+
const valB = getValue(b) ? 1 : 0;
|
|
735
|
+
return desc ? valB - valA : valA - valB;
|
|
736
|
+
});
|
|
737
|
+
return values;
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* This method is intended to be used with sorting methods like Array.sort(). It compares two strings, a and b, against a reference substring input.
|
|
741
|
+
* @param a - The first string to be compared.
|
|
742
|
+
* @param b - The second string to be compared.
|
|
743
|
+
* @param input - The reference substring used to influence the sorting order.
|
|
744
|
+
*/
|
|
712
745
|
static sortByStartingLetter(a, b, input) {
|
|
713
746
|
const startsWithInputA = a.toLowerCase().startsWith(input.toLowerCase());
|
|
714
747
|
const startsWithInputB = b.toLowerCase().startsWith(input.toLowerCase());
|