1.0.0 • Published 1 year ago

@f1stnpm3/nam-adipisci-repudiandae v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Syntactic sugar wrapper over JS sort function with better readability

Zero-dependency minimal package (two functions) to provide better-readable (albeit more verbose) wrapper over JS default Array.sort(). Fully typed.

Prevents errors when interchanging signs in the native Array.sort() function like this:

foo.sort((a, b) => (a.date.isBefore(b.date) ? -1 : 1)) - at the first glance, is this sorted in ASC or DESC order??

Usage:

import { sortAsc } from "./src/index";

const array = [3, 1, 4, 1, 5, 9, 2, 6];

const sortedArray = sortAsc({
  array,
  firstIsGreater: (a, b) => a > b, // Compare function that returns boolean
});

console.log(sortedArray); // [1, 1, 2, 3, 4, 5, 6, 9]

console.log(array); // [3, 1, 4, 1, 5, 9, 2, 6] - keeps the original array intact

You can provide areEqual function parameter to ensure stability of the sorting function. Otherwise it sorts equal elements in non-deterministic order.

import { sortAsc } from "./src/index";

// The sortAsc function is a stable sort when `areEqual` is provided, meaning that it preserves the original input order of elements that are equal.

const inputArray = [
  { name: "John", age: 25 },
  { name: "Alice", age: 30 },
  { name: "Bob", age: 20 },
  { name: "Mike", age: 25 },
];

const sortedArray = sortAsc({
  array: inputArray,
  firstIsGreater: (a, b) => a > b,
  areEqual: (a, b) => a === b,
});

console.log(sortedArray); // [{ name: "Bob", age: 20 }, { name: "John", age: 25 }, { name: "Mike", age: 25 }, { name: "Alice", age: 30 }] - keeps equal elements in order they appear in input
validationuuidECMAScript 2021browsershrinkwrapES2021protoECMAScript 3arraysdataViewbatcherror-handlingkarmacmdmatchesjQuerybounddefinePropertytypesstylesheetiamemrworkspace:*workflowwidthtapvaluethroatcoerciblepackage managerloggerreact-hookscolumnsdependenciesObservableapifastcopytostringtagcolorweakmaprgbfast-copycopyparseownfileObject.fromEntriesreact-testing-librarytypedarraysreadablecss variableeverydataviewfastcloneformspawncommand-linedefinemixinscolorsparentscloudsearchqueryless cssexit-codeextendform-validationgdprexitgroupeventDispatcherString.prototype.matchAllflattenaccessibilityfunctionsmkdirsObject.entriesreal-timerouterWeakMapefficientprivateworkerYAMLminimalreduxinputappes8settingsmergemulti-packagedayjsObject.getPrototypeOffunctionalpicomatchless mixinsWebSocketsroute53circularhardlinkscloudwatchfetchRFC-6455performanteventEmittermoduletaskslotkoreannativetypanioninterruptscloudtraildatedeepcloneserializerrm -rfweaksetfigletjshinteslintconfigrequestjwtmkdirimportexportsearchfindupoutputmetadatainstallvariablesquoteObject.keyswritableajvinstallerstatelesssinatraexefast-deep-copyES2022connectsyntaxerrordefaultpluginbluebirdrateconfigpromise[[Prototype]]speedrangeerrorgettersymlinksdiffes-abstractvalidmacosstartprettyfseventswarningjsonpathmimechromechromiumoperating-systemgradients csshandlersexecutabletrimeslint-pluginObservabless3arraydescriptionbreakauthtakereact-hook-formStyleSheetjsxformattingpolyfillargparsehas-ownnumberpropertiessetImmediatefssimpledbmiddlewareagentES6ES2020wgetreducerdropreadablestreamsameValueZerobyteOffsetArray.prototype.findLastIndexsequenceserializationtoolseast-asian-widthReactiveExtensionsprefixrecursiveyuplimiturlssymbolstreams2movemochaassignoptionECMAScript 2017restfulhelperswindowsvpcSymbol.toStringTagECMAScript 2019datastructurelookcompile lessES5environmentsshellremoveECMAScript 2016shammime-dbtimeprivate datadeterministicArray.prototype.flatMapArray.prototype.findLastRxless compilerFloat64ArrayvisualESjoifolderhotentriesESnexterrordeleteconfigurablexhrbyteguidECMAScript 2015es2018bindUint8ClampedArrayString.prototype.trimsettercore-jsnodePromisepipefullurldomECMAScript 7sortbusyjstouchshebangsigtermelmwriterandomquerystringfastcreateArrayBuffersignaloffsetbrowserslisti18nwhichtoStringTagpredictablesqscss lessimportObject.assignxtermflatMapredux-toolkitprocessl10nsigintfindCSS@@toStringTagyamlbyteLengthsortedaccessorexectypedarrayawaitimmeres7propqsSettelephonelruassertsstringfunctioncss nestinglinuxspinnerec2findLastformatcollection.es6xdgECMAScript 6csscall-bindECMAScript 5replayregular expressioncacheenumerablewatchertypedfull-widthobjecthasOwnPropertylesscssObject.isconsoleless.jswrapeslintplugines5wordbreakflagmobilecomputed-typestsvarsURLstoragegatewayAsyncIteratorliveObject.valueshttpsduplexMapidentifiers_.extendtrimRighttestingairbnbbundlerECMAScript 2020regexcorssnssignalsclassesURLSearchParamsstyled-componentsjestMicrosoftscheme-validationmimetypesiteratemonorepoirqlistenersReflect.getPrototypeOfsafecss-in-jskeypostcss-pluginpreserve-symlinksindicatorIteratorfluxutilityECMAScript 2022passwordparsingArraycloudfrontArrayBuffer.prototype.slicehashfilteres6globalspatchintslibdebuggerfind-upio-tsautoprefixerTypeBoxES2017endpointtypestylecharacterscoretestflatTypedArraytoArrayreadassertionpropertylesses2015lengthenvcommandtextredactWeakSet-0stableES2015watchFileoptimizerpromisesgradients css3chaibannertc39ReactiveXlimitedlastprogressPushresolvegetoptimistfast-clonepnpm9whatwgtrimLeftelblogopenloggingRegExp.prototype.flagsmake dirjasmineasyncstructuredClonetrimStartes2017runtimespectapedotenvES2019stringifiersharedpackage.jsonArray.prototype.flatspinnersstreamsformsansiessliceargumentses-shimsrmdiriteratorArrayBuffer#sliceperformancesesBigInt64Arraymodulescallboundecmascriptttyqueuebufferprototypestyleswait0elasticache256checkfromcall-bounddataTypeScriptconcatObjectUint16Arraycurlregular expressionsrequireUint32Arraybootstrap cssnodejsbundlingzodshimchildupenvironmentcodesstatusmkdirpconcatMaprm -frescapeprunemapreduceopenerpersistentauthenticationtoobjecthooksjapanesehigher-orderhttpunicodekeyselectron__proto__superstructutilitiescryptostylingprotobufRxJSthrottleschemerestdirectoryawesomesaucephonereactforEachenderArray.prototype.containscallbackiterationestreeUnderscoremakeidargumentconcurrencyES2016file systemstreamjsdomrdstypeerrorassertimmutablekinesisqueueMicrotasklinkarraybuffertddcliECMAScript 2018walkhasOwngetoptloadingWebSocketoncewebsiteArray.prototype.filterFloat32Arraydebugbootstrap lesslinewrapextrajavascriptcollectiondeep-copypatheventshookformcompilerpushcolourmatchAllsuperagentfpspreprocessorxdg-opensetPrototypeOfopensinspectsidearktypepositivezeroomitprotocol-buffersinternalcommanderexpresscjkinferencegroupByInt32ArrayflagsmapRegExp#flagswafeslintgetOwnPropertyDescriptornested cssvariables in cssargsserializeECMAScript 2023toSortedsymbolsCSSStyleDeclarationvaluesstateebssetbddSymbolregexpbeanstalkamazontoolkitisclassnameclonerfc4122bcryptchannelStreamstringifypyyamlwordwrapwalkingequalextensioncensorcryptlockfilebuffers$.extendcontainsrobustclass-validatorreuselook-upnopeJSONnegativeloadbalancingrmbrowserlistisConcatSpreadableES2023fixed-widthfastifygetPrototypeOfcharacteres-shim APIArray.prototype.flattenemojiutil.inspectstyleguideBigUint64ArrayeditoridlemomentwebInt16ArraywatchingsomeStreamsframeworkponyfillcolumnInt8Arrayes2016globalintrinsicdescriptorsstartertraversedependency managerlazyvalidatematchartJSON-Schemaa11yreduceexpressiontermObject.definePropertyjsdiffnamesharedarraybufferglobtypescriptcomparestdlibavaconsumetester
1.0.0

1 year ago