Skip to content

Commit

Permalink
descend
Browse files Browse the repository at this point in the history
  • Loading branch information
Deyan Totev committed Jul 18, 2023
1 parent a516fcc commit bac9350
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
21 changes: 21 additions & 0 deletions files/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5340,6 +5340,7 @@ Notes: TS typings are oversimplified
*/
// @SINGLE_MARKER
export function addIndex(originalFn: any): (fn: any) => (list: any[]) => any[];
export function addIndex(originalFn: any): (fn: any, list: any[]) => any[];

/*
Method: ap
Expand Down Expand Up @@ -5378,6 +5379,7 @@ Notes: TS typings are oversimplified
*/
// @SINGLE_MARKER
export function addIndexRight(originalFn: any): (fn: any) => (list: any[]) => any[];
export function addIndexRight(originalFn: any): (fn: any, list: any[]) => any[];

/*
Method: aperture
Expand Down Expand Up @@ -5437,6 +5439,25 @@ Notes:
export function ascend<T>(fn: (obj: T) => Ord, a: T, b: T): Ordering;
export function ascend<T>(fn: (obj: T) => Ord): (a: T, b: T) => Ordering;

/*
Method: descend
Explanation:
Example:
```
```
Categories:
Notes:
*/
// @SINGLE_MARKER
export function descend<T>(fn: (obj: T) => Ord, a: T, b: T): Ordering;
export function descend<T>(fn: (obj: T) => Ord): (a: T, b: T) => Ordering;

// RAMBDAX_MARKER_START

/*
Expand Down
4 changes: 1 addition & 3 deletions source/ascend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ export function createCompareFunction(
a, b, winner, loser
){
if (a === b) return 0
if (a == null) return winner
if (a == null) return loser

return a > b ? loser : winner
return a < b ? winner : loser
}

export function ascend(
Expand Down
26 changes: 23 additions & 3 deletions source/ascend.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ascend } from './ascend.js'
import { descend } from './ascend.js'
import { descend } from './descend.js'
import { sort } from './sort.js'

const people = [
{
name : 'Emma',
age : 70,
},
{name: 'noage'},
{
name : 'Peter',
age : 78,
Expand All @@ -19,7 +18,7 @@ const people = [
]

test('ascend', () => {
const result = sort(ascend(x => x.age),
const result = sort(ascend(x => x?.age),
people)
const expected = [
{
Expand All @@ -37,3 +36,24 @@ test('ascend', () => {
]
expect(result).toEqual(expected)
})

test('descend', () => {
const result = sort(descend(x => x?.age),
people)
const expected = [
{
name : 'Peter',
age : 78,
},
{
name : 'Emma',
age : 70,
},
{
name : 'Mikhail',
age : 62,
},
]

expect(result).toEqual(expected)
})
17 changes: 17 additions & 0 deletions source/descend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createCompareFunction } from './ascend.js'

export function descend(
getFunction, a, b
){
if (arguments.length === 1){
return (_a, _b) => descend(
getFunction, _a, _b
)
}
const aValue = getFunction(a)
const bValue = getFunction(b)

return createCompareFunction(
aValue, bValue, 1, -1
)
}

0 comments on commit bac9350

Please sign in to comment.