Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Created November 16, 2022 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/07128ab3095541ad0b7ce42be60cb17e to your computer and use it in GitHub Desktop.
Save DarrenSem/07128ab3095541ad0b7ce42be60cb17e to your computer and use it in GitHub Desktop.
sorted.js (array, strFlags) returns CLONE (not in-place sort!) -- flags 'uid': u=Unique original values, i=case Insensitive compare function, d=Descending (reverse) order
// sorted.js (array, strFlags) returns CLONE (not in-place sort!) -- flags 'uid': u=Unique original values, i=case Insensitive compare function, d=Descending (reverse) order
// minified = 179 chars s=(a,f="",c=a=>f.includes(a),d=[...(c("u")?[...new Set(a)]:a)])=>(d=c("i")?d.sort((a,b,c=(a+"").toUpperCase(),d=(b+"").toUpperCase())=>c<d?-1:c===d):d.sort(),c("d")?d.reverse():d)
let sorted = (array, strFlags = "",
// friendlier syntax like RegExp( _, strFlags ) rather than fetch( _, {options} )
_tmp_HasFlag = flag => strFlags.includes(flag),
// work with CLONE of original array (or Set made from it) -- we do NOT want to do IN-PLACE sort
_tmp_Result = [...
// "u" = [U]nique values via Set(original) -- only removes dupes; has NO impact on compare function
(_tmp_HasFlag("u") ? [...new Set(array)] : array)
]
) => (
// "i" = case [I]nsensitive during compare function (has NO impact on "u" flag -- which only removes dupes)
_tmp_Result = _tmp_HasFlag("i")
? _tmp_Result.sort((a, b,
_tmp_str1 = String(a).toUpperCase(),
_tmp_str2 = String(b).toUpperCase()
) => _tmp_str1 < _tmp_str2 ? -1 : _tmp_str1 === _tmp_str2)
: _tmp_Result.sort(),
// "d" = [D]escending order ("z..aZ..A" instead of "A..Za..z")
_tmp_HasFlag("d") ? _tmp_Result.reverse() : _tmp_Result
);
let assert = (...tests) => !!tests.reduce(
(pass, func, i, ar, messages = Array.isArray(func) ? func : [func]) => (
typeof messages[0] === "function" ? messages[0]() : messages[0]
) ? pass : console.assert(0, ...messages)
, tests.length
);
console.clear();
let orig = ['AB', 'ac', 'ab', 'ac', 'AB', 'ab', 'AC', 'AC'];
let pass = true;
pass = assert(
// (intentional_1, to_confirm, assert_works_correctly) => false,
(orig_1) => JSON.stringify(orig) === '["AB","ac","ab","ac","AB","ab","AC","AC"]',
(sorted_no_flags) => JSON.stringify(sorted(orig)) === '["AB","AB","AC","AC","ab","ab","ac","ac"]',
(caseInsensitive) => JSON.stringify(sorted(orig, "i")) === '["AB","ab","AB","ab","ac","ac","AC","AC"]',
(Descending) => JSON.stringify(sorted(orig, "d")) === '["ac","ac","ab","ab","AC","AC","AB","AB"]',
(cI_Descending) => JSON.stringify(sorted(orig, "di")) === '["AC","AC","ac","ac","ab","AB","ab","AB"]',
(orig_2, was, not_changed) => JSON.stringify(orig) === '["AB","ac","ab","ac","AB","ab","AC","AC"]',
(Unique) => JSON.stringify(sorted(orig, "u")) === '["AB","AC","ab","ac"]',
(Unique_Descending) => JSON.stringify(sorted(orig, "ud")) === '["ac","ab","AC","AB"]',
(Unique_cI) => JSON.stringify(sorted(orig, "iu")) === '["AB","ab","ac","AC"]',
(Unique_Descending_cI) => JSON.stringify(sorted(orig, "dui")) === '["AC","ac","ab","AB"]',
// (intentional_2, to_confirm, assert_works_correctly) => false,
(orig_3, was, not_changed) => JSON.stringify(orig) === '["AB","ac","ab","ac","AB","ab","AC","AC"]',
// (intentional_3, to_confirm, assert_works_correctly) => false,
);
pass;
// true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment