Skip to content

Commit

Permalink
Add just-order-by package (#530)
Browse files Browse the repository at this point in the history
* Add `just-order-by` package

* Change api

* Fix tests
  • Loading branch information
klaseca committed Mar 26, 2023
1 parent 6417f86 commit 8f8b2da
Show file tree
Hide file tree
Showing 12 changed files with 786 additions and 6 deletions.
125 changes: 122 additions & 3 deletions README.md
Expand Up @@ -28,13 +28,13 @@ A [REPL](https://anguscroll.com/just) for every utility (powered by [RunKit](htt
All packages support ES module or Common JS syntax without requiring transpilation
```
// esm (node / bundler)
import clone from 'just-clone';
import clone from 'just-clone';
// esm (native browser code)
import clone from './node_modules/just-clone/index.mjs';
import clone from './node_modules/just-clone/index.mjs';
// cjs
const clone = require('just-clone');
const clone = require('just-clone');
```
## TypeScript <img src="images/ts.png" width="18"/>

Expand Down Expand Up @@ -92,6 +92,7 @@ Most utilities still work with any platform that supports ES5, but these are the
- [just-shuffle](#just-shuffle)
- [just-split](#just-split)
- [just-split-at](#just-split-at)
- [just-order-by](#just-order-by)
- [just-sort-by](#just-sort-by)
- [just-partition](#just-partition)
- [just-permutations](#just-permutations)
Expand Down Expand Up @@ -1338,6 +1339,124 @@ splitAt(null, 1); // throws
splitAt(undefined, 1); // throws
```
### [just-order-by](https://www.npmjs.com/package/just-order-by)
[source](https://github.com/angus-c/just/tree/master/packages/array-order-by)
[`🍦 Try it`](https://anguscroll.com/just/just-order-by)
```shell
npm install just-order-by
```
```shell
yarn add just-order-by
```
Produces a new array, sorted in given order
```js
import orderBy from 'just-order-by';

orderBy([10, 1, 5, 20, 15, 35, 30, 6, 8]); // [1, 5, 6, 8, 10, 15, 20, 30, 35]

orderBy(
[
{ user: 'fabio', details: { city: 'Milan', age: 34 } },
{ user: 'max', details: { city: 'Munich', age: 29 } },
{ user: 'zacarias', details: { city: 'Sao Paulo', age: 44 } },
{ user: 'robert', details: { city: 'Manchester', age: 28 } },
{ user: 'max', details: { city: 'Zurich', age: 38 } },
],
[
{
property(v) {
return v.details.age;
},
},
]
);

/*
[
{user: 'robert', age: 28},
{user: 'max', age: 29},
{user: 'fabio', age: 34},
{user: 'klaus', age: 38},
{user: 'zacarias', age: 44},
]
*/

orderBy(
[
{user: 'fabio', age: 34},
{user: 'max', age: 29},
{user: 'zacarias', age: 44},
{user: 'robert', age: 28},
{user: 'klaus', age: 38},
],
[
{
property: 'user',
},
]
);

/*
[
{user: 'fabio', age: 34},
{user: 'klaus', age: 38},
{user: 'max', age: 29},
{user: 'robert', age: 28},
{user: 'zacarias', age: 44},
]
*/

orderBy(
[
{ user: 'fabio', age: 34 },
{ user: 'max', age: 29 },
{ user: 'zacarias', age: 44 },
{ user: 'moris', age: 28 },
{ user: 'max', age: 38 },
],
[
{
property: 'user',
order: 'desc',
},
{
property(v) {
return v.age;
},
},
]
);

/*
[
{
user: 'zacarias',
age: 44
},
{
user: 'moris',
age: 28
},
{
user: 'max',
age: 29
},
{
user: 'max',
age: 38
},
{
user: 'fabio',
age: 34
}
]
*/
```
### [just-sort-by](https://www.npmjs.com/package/just-sort-by)
[source](https://github.com/angus-c/just/tree/master/packages/array-sort-by)
Expand Down
108 changes: 108 additions & 0 deletions md-variables.json
Expand Up @@ -149,6 +149,114 @@
"mode(null); // throws"
]
},
"just-order-by": {
"packageName": "just-order-by",
"dir": "array-order-by",
"description": "Produces a new array, sorted in given order",
"examples": [
"import orderBy from 'just-order-by';",
"",
"orderBy([10, 1, 5, 20, 15, 35, 30, 6, 8]); // [1, 5, 6, 8, 10, 15, 20, 30, 35]",
"",
"orderBy(",
" [",
" { user: 'fabio', details: { city: 'Milan', age: 34 } },",
" { user: 'max', details: { city: 'Munich', age: 29 } },",
" { user: 'zacarias', details: { city: 'Sao Paulo', age: 44 } },",
" { user: 'robert', details: { city: 'Manchester', age: 28 } },",
" { user: 'max', details: { city: 'Zurich', age: 38 } },",
" ],",
" [",
" {",
" property(v) {",
" return v.details.age;",
" },",
" },",
" ]",
");",
"",
"/*",
"[",
" {user: 'robert', age: 28},",
" {user: 'max', age: 29},",
" {user: 'fabio', age: 34},",
" {user: 'klaus', age: 38},",
" {user: 'zacarias', age: 44},",
"]",
"*/",
"",
"orderBy(",
" [",
" {user: 'fabio', age: 34},",
" {user: 'max', age: 29},",
" {user: 'zacarias', age: 44},",
" {user: 'robert', age: 28},",
" {user: 'klaus', age: 38},",
" ],",
" [",
" {",
" property: 'user',",
" },",
" ]",
");",
"",
"/*",
"[",
" {user: 'fabio', age: 34},",
" {user: 'klaus', age: 38},",
" {user: 'max', age: 29},",
" {user: 'robert', age: 28},",
" {user: 'zacarias', age: 44},",
"]",
"*/",
"",
"orderBy(",
" [",
" { user: 'fabio', age: 34 },",
" { user: 'max', age: 29 },",
" { user: 'zacarias', age: 44 },",
" { user: 'moris', age: 28 },",
" { user: 'max', age: 38 },",
" ],",
" [",
" {",
" property: 'user',",
" order: 'desc',",
" },",
" {",
" property(v) {",
" return v.age;",
" },",
" },",
" ]",
");",
"",
"/*",
"[",
" {",
" user: 'zacarias',",
" age: 44",
" },",
" {",
" user: 'moris',",
" age: 28",
" },",
" {",
" user: 'max',",
" age: 29",
" },",
" {",
" user: 'max',",
" age: 38",
" },",
" {",
" user: 'fabio',",
" age: 34",
" }",
"]",
"*/"
]
},
"just-partition": {
"packageName": "just-partition",
"dir": "array-partition",
Expand Down
21 changes: 21 additions & 0 deletions packages/array-order-by/LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Maxim Molochkov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 8f8b2da

Please sign in to comment.