@patrtorg/nam-eius-unde

4.10.130 • Public • Published

@patrtorg/nam-eius-unde

License: MIT Build Version Known Vulnerabilities GitHub last commit (branch) npm npm total codecov

Blazing fast, tree-shakeable, type-safe, modern utility library to sort any type of array

Docs : https://@patrtorg/nam-eius-unde.netlify.com/

Getting started

installation

The library is available as a npm package. To install the package, run:

npm install @patrtorg/nam-eius-unde
# or
yarn add @patrtorg/nam-eius-unde

Start using:

import {byString} from '@patrtorg/nam-eius-unde'

const unsorted = ["xxx", "bbbb", "zzz", "cccc", "aaa"];
const sorted = unsorted.sort(byString());

console.log(sorted); //(5) ["aaa", "bbbb", "cccc", "xxx", "zzz"]

Use directly in the browser

<script src='https://cdn.jsdelivr.net/npm/@patrtorg/nam-eius-unde/dist/index.umd.js'></script>
<script>
  const unsorted = ["xxx", "bbbb", "zzz", "cccc", "aaa"];
  const sorted = unsorted.sort(sort.byString());

  console.log(sorted); //(5) ["aaa", "bbbb", "cccc", "xxx", "zzz"]
</script>

//or via browser modules

<script type='module'>
  import {byString} from 'https://cdn.jsdelivr.net/npm/@patrtorg/nam-eius-unde/dist/index.mjs'

  const unsorted = ["xxx", "bbbb", "zzz", "cccc", "aaa"];
  const sorted = unsorted.sort(byString());

  console.log(sorted); //(5) ["aaa", "bbbb", "cccc", "xxx", "zzz"]
</script>

Some mind-blowing example

sort by a single property

//js or ts file
import {byValue, byNumber, byString} from '@patrtorg/nam-eius-unde'

const arrayUnsorted = [
  {prop: "xxx", foo: 34},
  {prop: "aaa", foo: 325},
  {prop: "zzz", foo: 15},
  {prop: "ccc", foo: 340},
  {prop: "bbb", foo: 0}
];

//this sort by the foo property ascending
const sortedByFoo = arrayUnsorted.sort(byValue((i) => i.foo, byNumber()));
console.log(sortedByFoo); //(5) [{prop: "bbb", foo : 0}, {prop: "zzz", foo: 15}, .....];

//this sort by the prop property descending
const sortedByProp = arrayUnsorted.sort(byValue((i) => i.prop, byString({desc: true})));
console.log(sortedByProp); //(5) [{prop: "zzz", foo : 15}, {prop: "xxx", foo: 34}, .....];

sort by a multiple property

//js or ts file
import {byNumber, byString, byValues} from "@patrtorg/nam-eius-unde";

const objsToSort = [
  {id: 2, name: 'teresa'},
  {id: 3, name: 'roberto'},
  {id: 2, name: 'roberto'}
];

//i sort by THEIR NAMES and THEN by their ids
const sortedObject = objsToSort.sort(byValues([
  [(x) => x.name, byString()],
  [(x) => x.id, byNumber()]
]));

console.log(sortedObject); //[{roberto, 2}, {roberto, 3}, {teresa, 2}];

//i sort by THEIR IDS and THEN by their names
const sortedObject2 = objsToSort.sort(byValues([
  [(x) => x.id, byNumber()],
  [(x) => x.name, byString()]
]));
console.log(sortedObject2); //[{roberto, 2}, {teresa, 2}, {roberto, 3}];

//i sort by THEIR IDS and THEN by their names DESCENDING
const sortedObject3 = objsToSort.sort(byValues([
  [(x) => x.id, byNumber()],
  [(x) => x.name, byString({desc: true})],
]));
console.log(sortedObject3); //[{teresa, 2}, {roberto, 2}, {roberto, 3}];

typescript types check

//ts file
import {byValue, byNumber, byString} from '@patrtorg/nam-eius-unde'

const objsArray = [{numbProp: 2, stringProp: 'a'}, {numbProp: 3, stringProp: 'f'}];

//Incorrect sort property 
const incorrectSortedArray = objsArray.sort(byValue(i => i.numbProp, byString()));
//ts check error : Type 'number' is not assignable to type 'string'.

//Correct sort type
const sortedArray = objsArray.sort(byValue(i => i.numbProp, byNumber()))
//ts check ok

See full Docs

License

MIT © Cosimo chellini

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.10.1300latest

Version History

VersionDownloads (Last 7 Days)Published
4.10.1300
4.10.12951
4.10.12854
4.10.12751
4.9.12758
4.9.12654
4.9.12564
4.9.12458
4.9.12312
3.9.1238
3.9.1226
3.9.1213
3.9.1202
3.9.1192
3.8.1192
3.8.1182
3.8.1171
3.8.1160
3.8.1150
3.8.1140
3.8.1130
3.8.1120
3.8.1110
3.8.1100
3.8.1090
3.8.1080
3.8.1070
2.8.1073,050
2.8.1060
2.8.1050
2.8.1040
2.8.1030
2.8.1020
2.8.1010
2.8.1000
2.8.991
2.8.980
2.8.970
2.8.960
2.7.960
2.7.950
2.7.940
2.6.940
2.6.930
2.6.920
2.6.910
2.5.910
2.5.900
2.5.890
2.5.880
2.5.870
2.5.860
2.5.850
2.5.840
2.5.830
2.5.820
2.5.810
2.5.800
2.5.790
2.5.780
2.5.770
2.5.760
2.5.750
2.5.740
2.5.730
2.5.720
2.5.710
2.5.700
2.5.690
2.5.680
2.5.670
2.5.660
2.5.650
2.5.640
2.5.630
2.5.620
2.5.610
2.4.610
2.4.600
2.3.600
2.3.590
2.3.580
2.3.570
2.3.560
2.3.550
2.3.540
2.3.530
2.3.520
2.3.510
2.3.500
2.3.490
2.3.480
2.3.470
2.3.460
2.3.450
2.3.440
2.3.430
2.3.420
2.3.410
2.3.401
2.3.390
2.3.380
2.3.370
2.3.360
2.3.350
2.3.340
2.3.330
2.3.320
2.3.310
2.3.300
2.3.290
2.3.280
1.3.284,706
1.3.270
1.3.260
1.3.251
1.3.240
1.3.230
1.3.220
1.3.210
1.3.200
1.3.190
1.3.185
1.3.17195
1.3.16235
1.3.15217
1.3.140
1.2.140
1.2.130
1.2.120
1.2.110
1.2.100
1.2.90
1.2.80
1.2.70
1.2.60
1.1.60
1.1.50
1.1.40
1.1.30
1.1.20
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @patrtorg/nam-eius-unde

Weekly Downloads

8,839

Version

4.10.130

License

MIT

Unpacked Size

234 kB

Total Files

635

Last publish

Collaborators

  • minhtran645176