UNPKG

@mikezimm/npmfunctions

Version:
271 lines (269 loc) 12.2 kB
"use strict"; /*** * .d8b. d8888b. d8888b. .d8b. db db .d8888. * d8' `8b 88 `8D 88 `8D d8' `8b `8b d8' 88' YP * 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8' `8bo. * 88~~~88 88`8b 88`8b 88~~~88 88 `Y8b. * 88 88 88 `88. 88 `88. 88 88 88 db 8D * YP YP 88 YD 88 YD YP YP YP `8888Y' * * //Sorting import { sortStringArray, sortObjectArrayByStringKey, sortNumberArray, sortObjectArrayByNumberKey, sortKeysByOtherKey } from '@mikezimm/npmfunctions/dist/Services/arraySorting'; */ Object.defineProperty(exports, "__esModule", { value: true }); exports.sortKeysByOtherKey = exports.sortObjectArrayByChildNumberKey = exports.sortObjectArrayByNumberKey = exports.sortNumberArray = exports.sortObjectArrayByStringKeyCollator = exports.sortObjectArrayByStringKey = exports.sortStringArrayCollator = exports.sortStringArray = void 0; /*** * .d8888. .d88b. d8888b. d888888b .d8888. d888888b d8888b. d888888b d8b db d888b .d8b. d8888b. d8888b. .d8b. db db * 88' YP .8P Y8. 88 `8D `~~88~~' 88' YP `~~88~~' 88 `8D `88' 888o 88 88' Y8b d8' `8b 88 `8D 88 `8D d8' `8b `8b d8' * `8bo. 88 88 88oobY' 88 `8bo. 88 88oobY' 88 88V8o 88 88 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8' * `Y8b. 88 88 88`8b 88 `Y8b. 88 88`8b 88 88 V8o88 88 ooo 88~~~88 88`8b 88`8b 88~~~88 88 * db 8D `8b d8' 88 `88. 88 db 8D 88 88 `88. .88. 88 V888 88. ~8~ 88 88 88 `88. 88 `88. 88 88 88 * `8888Y' `Y88P' 88 YD YP `8888Y' YP 88 YD Y888888P VP V8P Y888P YP YP 88 YD 88 YD YP YP YP * * 2020-12-14 * sortStringArray was added to remove typescript errors in sortKeysByOtherKey * @param arr * @param order */ function sortStringArray(arr, order, convertNullToEmpty) { if (convertNullToEmpty === void 0) { convertNullToEmpty = false; } //Solve https://github.com/mikezimm/drilldown7/issues/79 //Convert any null values to empty string so they don't break the 'localeCompare' sort below. if (convertNullToEmpty === true) { arr = arr.map(function (str) { return str ? str : ''; }); } if (order === 'asc') { arr.sort(function (a, b) { return a.localeCompare(b); }); } else if (order === 'dec') { arr.sort(function (a, b) { return b.localeCompare(a); }); } else { } return arr; } exports.sortStringArray = sortStringArray; function sortStringArrayCollator(arr, order, convertNullToEmpty, localLanguage) { if (convertNullToEmpty === void 0) { convertNullToEmpty = false; } if (localLanguage === void 0) { localLanguage = 'en'; } //Solve https://github.com/mikezimm/drilldown7/issues/79 //Convert any null values to empty string so they don't break the 'localeCompare' sort below. if (convertNullToEmpty === true) { arr = arr.map(function (str) { return str ? str : ''; }); } //Adding collator per: https://stackoverflow.com/a/52369951 var collator = new Intl.Collator(localLanguage, { numeric: true, sensitivity: 'base' }); if (order === 'asc') { arr.sort(function (a, b) { return collator.compare(a, b); }); } else if (order === 'dec') { arr.sort(function (a, b) { return collator.compare(b, a); }); } else { console.log('SORTING.ts ERROR - no asc or dec ~ 88'); } return arr; } exports.sortStringArrayCollator = sortStringArrayCollator; //2021-01-05: Updated per TrackMyTime7 arrayServices function sortObjectArrayByStringKey(arr, order, key) { if (order === 'asc') { arr.sort(function (a, b) { return a[key].localeCompare(b[key]); }); } else if (order === 'dec') { arr.sort(function (a, b) { return b[key].localeCompare(a[key]); }); } else { } return arr; } exports.sortObjectArrayByStringKey = sortObjectArrayByStringKey; /** * * * NOTE sortObjectArrayByStringKeyCollator HAS NOT BEEN TESTED IN ANY WAY. * JUST MODIFIED based upon sortStringArrayCollator * * @param arr * @param order * @param key * @param convertNullToEmpty * @param localLanguage */ //2021-01-05: Updated per TrackMyTime7 arrayServices function sortObjectArrayByStringKeyCollator(arr, order, key, convertNullToEmpty, localLanguage) { if (convertNullToEmpty === void 0) { convertNullToEmpty = false; } if (localLanguage === void 0) { localLanguage = 'en'; } //Solve https://github.com/mikezimm/drilldown7/issues/79 //Convert any null values to empty string so they don't break the 'localeCompare' sort below. if (convertNullToEmpty === true) { arr = arr.map(function (str) { if (!str[key]) { str[key] = ''; } return str; }); } //Adding collator per: https://stackoverflow.com/a/52369951 var collator = new Intl.Collator(localLanguage, { numeric: true, sensitivity: 'base' }); if (order === 'asc') { arr.sort(function (a, b) { return collator.compare(a[key], b[key]); }); } else if (order === 'dec') { arr.sort(function (a, b) { return collator.compare(b[key], a[key]); }); } else { console.log('SORTING.ts ERROR - no asc or dec ~ 88'); } return arr; } exports.sortObjectArrayByStringKeyCollator = sortObjectArrayByStringKeyCollator; /*** * .d8888. .d88b. d8888b. d888888b d8b db db db .88b d88. d8888b. d88888b d8888b. .d8b. d8888b. d8888b. .d8b. db db * 88' YP .8P Y8. 88 `8D `~~88~~' 888o 88 88 88 88'YbdP`88 88 `8D 88' 88 `8D d8' `8b 88 `8D 88 `8D d8' `8b `8b d8' * `8bo. 88 88 88oobY' 88 88V8o 88 88 88 88 88 88 88oooY' 88ooooo 88oobY' 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8' * `Y8b. 88 88 88`8b 88 88 V8o88 88 88 88 88 88 88~~~b. 88~~~~~ 88`8b 88~~~88 88`8b 88`8b 88~~~88 88 * db 8D `8b d8' 88 `88. 88 88 V888 88b d88 88 88 88 88 8D 88. 88 `88. 88 88 88 `88. 88 `88. 88 88 88 * `8888Y' `Y88P' 88 YD YP VP V8P ~Y8888P' YP YP YP Y8888P' Y88888P 88 YD YP YP 88 YD 88 YD YP YP YP * * * 2020-12-14 * sortNumberArray was added to remove typescript errors in sortKeysByOtherKey * @param arr * @param order * * NOTE PER DOCS: Sorts as strings * The sort() sorts the elements as strings in alphabetical and ascending order. */ function sortNumberArray(arr, order) { if (order === 'asc') { arr.sort(function (a, b) { return a - b; }); } else if (order === 'dec') { arr.sort(function (a, b) { return b - a; }); } else { } return arr; } exports.sortNumberArray = sortNumberArray; //2021-01-05: Updated per TrackMyTime7 arrayServices function sortObjectArrayByNumberKey(arr, order, key) { if (order === 'asc') { arr.sort(function (a, b) { return a[key] - b[key]; }); } else if (order === 'dec') { arr.sort(function (a, b) { return b[key] - a[key]; }); } else { } return arr; } exports.sortObjectArrayByNumberKey = sortObjectArrayByNumberKey; //2021-01-05: Updated per TrackMyTime7 arrayServices /** * This is different from sortObjectArrayByNumberKey in that you can look at nested children. * In the case of ExtremeContents: * There is an array of File types call infoTypes * I wanted to sort by type.summary.keyToSortBy. * This will let you pass in a key string like: key ='summary.count' and it will look at the children objects. * NOTE: The keys are determined using the '.' * * @param arr * @param order * @param key */ function sortObjectArrayByChildNumberKey(arr, order, key) { var keys = key.split('.'); var key1 = keys.length >= 1 ? keys[0] : key; var key2 = keys.length >= 2 ? keys[1] : ''; var key3 = keys.length >= 3 ? keys[2] : ''; if (keys.length === 1) { if (order === 'asc') { arr.sort(function (a, b) { return a[key] - b[key]; }); } else if (order === 'dec') { arr.sort(function (a, b) { return b[key] - a[key]; }); } else { } } else if (keys.length === 2) { if (order === 'asc') { arr.sort(function (a, b) { return a[key1][key2] - b[key1][key2]; }); } else if (order === 'dec') { arr.sort(function (a, b) { return b[key1][key2] - a[key1][key2]; }); } else { } } return arr; } exports.sortObjectArrayByChildNumberKey = sortObjectArrayByChildNumberKey; /*** * .d8888. .d88b. d8888b. d888888b db dD d88888b db db .d8888. d8888b. db db .d88b. d888888b db db d88888b d8888b. db dD d88888b db db * 88' YP .8P Y8. 88 `8D `~~88~~' 88 ,8P' 88' `8b d8' 88' YP 88 `8D `8b d8' .8P Y8. `~~88~~' 88 88 88' 88 `8D 88 ,8P' 88' `8b d8' * `8bo. 88 88 88oobY' 88 88,8P 88ooooo `8bd8' `8bo. 88oooY' `8bd8' 88 88 88 88ooo88 88ooooo 88oobY' 88,8P 88ooooo `8bd8' * `Y8b. 88 88 88`8b 88 88`8b 88~~~~~ 88 `Y8b. 88~~~b. 88 88 88 88 88~~~88 88~~~~~ 88`8b 88`8b 88~~~~~ 88 * db 8D `8b d8' 88 `88. 88 88 `88. 88. 88 db 8D 88 8D 88 `8b d8' 88 88 88 88. 88 `88. 88 `88. 88. 88 * `8888Y' `Y88P' 88 YD YP YP YD Y88888P YP `8888Y' Y8888P' YP `Y88P' YP YP YP Y88888P 88 YD YP YD Y88888P YP * * 2020-12-14 * This function caused errors in TrackMyTime which was based on @yo 1.9.1 but works in Drilldown and ActionNews @yo 1.11.0 * * Cannot invoke an expression whose type lacks a call signature. Type '((compareFn?: (a: string, b: string) => number) => string[]) | ((compareFn?: (a: number, b: numbe...' has no compatible call signatures. * * Rebuilt and added sortNumberArray and sortStringArray and it seems to work ok. */ function sortKeysByOtherKey(obj, sortKey, order, dataType, otherKeys, convertNullToEmpty, localLanguage) { if (convertNullToEmpty === void 0) { convertNullToEmpty = false; } if (localLanguage === void 0) { localLanguage = ''; } var sortKeyAny = sortKey; var sortCopy = JSON.parse(JSON.stringify(obj[sortKeyAny])); var otherKeyArrays = {}; otherKeys.map(function (m) { otherKeyArrays[m] = []; }); if (dataType === 'number') { sortCopy = sortNumberArray(sortCopy, order); } else { if (dataType === 'string' && localLanguage !== '') { sortCopy = sortStringArrayCollator(sortCopy, order, convertNullToEmpty, localLanguage); } else { sortCopy = sortStringArray(sortCopy, order, convertNullToEmpty); } } var x = 0; var _loop_1 = function (v) { var currentIndex = obj[sortKeyAny].indexOf(v); //Get index of the first sortable value in original array var i = 0; otherKeys.map(function (key) { if (obj[key]) { otherKeyArrays[key].push(obj[key][currentIndex]); } else { console.log('sortKeysByOtherKey: Unable to push obj[key][currentIndex] because obj[key] does not exist!', obj, key, currentIndex); } }); obj[sortKeyAny][currentIndex] = null; x++; }; for (var _i = 0, sortCopy_1 = sortCopy; _i < sortCopy_1.length; _i++) { var v = sortCopy_1[_i]; _loop_1(v); } otherKeys.map(function (key) { obj[key] = otherKeyArrays[key]; }); obj[sortKeyAny] = sortCopy; return obj; } exports.sortKeysByOtherKey = sortKeysByOtherKey; //# sourceMappingURL=sorting.js.map