Skip to content

Commit

Permalink
relax sortBy signature in Array, ReadonlyArray
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 22, 2020
1 parent f8c3423 commit 003028c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
> - [Experimental]
> - [Deprecation]
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
**Note**: Gaps between patch versions are faulty/broken releases.
**Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 2.7.1
Expand All @@ -23,10 +24,12 @@ high state of flux, you're at risk of it changing without notice.
- **Polish**
- `Array`
- relax `sort` signature (@gcanti)
- relax `sortBy` signature (@gcanti)
- `Map`
- export `mapWithIndex` (@gcanti)
- `ReadonlyArray`
- relax `sort` signature (@gcanti)
- relax `sortBy` signature (@gcanti)
- `ReadonlyMap`
- export `mapWithIndex` (@gcanti)

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Array.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ etc...
**Signature**

```ts
export declare const sortBy: <A>(ords: Ord<A>[]) => (as: A[]) => A[]
export declare const sortBy: <B>(ords: Ord<B>[]) => <A extends B>(as: A[]) => A[]
```
**Example**
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ReadonlyArray.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ etc...
**Signature**

```ts
export declare function sortBy<A>(ords: ReadonlyArray<Ord<A>>): (as: ReadonlyArray<A>) => ReadonlyArray<A>
export declare function sortBy<B>(ords: ReadonlyArray<Ord<B>>): <A extends B>(as: ReadonlyArray<A>) => ReadonlyArray<A>
```

**Example**
Expand Down
29 changes: 29 additions & 0 deletions dtslint/ts3.5/Array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as _ from '../../src/Array'
import { pipe } from '../../src/pipeable'
import { eqNumber } from '../../src/Eq'
import { Ord } from '../../src/Ord'

declare const us: Array<unknown>
declare const ns: Array<number>
Expand Down Expand Up @@ -133,3 +134,31 @@ _.zip(['a', 'b']) // $ExpectType <A>(as: A[]) => [A, string][]

_.cons(0, [1, 2]) // $ExpectType NonEmptyArray<number>
_.cons(0) // $ExpectType (tail: number[]) => NonEmptyArray<number>

//
// sort
//

declare const ord1: Ord<{ readonly a: string }>
interface X1 {
readonly a: string
readonly b: number
}
declare const x1s: Array<X1>

_.sort(ord1)(x1s) // $ExpectType X1[]

//
// sortBy
//

declare const ord2: Ord<X1>
declare const ord3: Ord<X1>
interface X2 {
readonly a: string
readonly b: number
readonly c: boolean
}
declare const x2s: Array<X2>

_.sortBy([ord2, ord3])(x2s) // $ExpectType X2[]
29 changes: 29 additions & 0 deletions dtslint/ts3.5/ReadonlyArray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as _ from '../../src/ReadonlyArray'
import { pipe } from '../../src/pipeable'
import { eqNumber } from '../../src/Eq'
import { Ord } from '../../src/Ord'

declare const rus: ReadonlyArray<unknown>
declare const rns: ReadonlyArray<number>
Expand Down Expand Up @@ -124,3 +125,31 @@ _.zip(['a', 'b']) // $ExpectType <A>(as: readonly A[]) => readonly (readonly [A,

_.cons(0, [1, 2]) // $ExpectType ReadonlyNonEmptyArray<number>
_.cons(0) // $ExpectType (tail: readonly number[]) => ReadonlyNonEmptyArray<number>

//
// sort
//

declare const ord1: Ord<{ readonly a: string }>
interface X1 {
readonly a: string
readonly b: number
}
declare const x1s: ReadonlyArray<X1>

_.sort(ord1)(x1s) // $ExpectType ReadonlyArray<X1>

//
// sortBy
//

declare const ord2: Ord<X1>
declare const ord3: Ord<X1>
interface X2 {
readonly a: string
readonly b: number
readonly c: boolean
}
declare const x2s: ReadonlyArray<X2>

_.sortBy([ord2, ord3])(x2s) // $ExpectType ReadonlyArray<X2>
2 changes: 1 addition & 1 deletion src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ export const uniq: <A>(E: Eq<A>) => (as: Array<A>) => Array<A> = RA.uniq as any
* @category combinators
* @since 2.0.0
*/
export const sortBy: <A>(ords: Array<Ord<A>>) => (as: Array<A>) => Array<A> = RA.sortBy as any
export const sortBy: <B>(ords: Array<Ord<B>>) => <A extends B>(as: Array<A>) => Array<A> = RA.sortBy as any

/**
* A useful recursion pattern for processing an array to produce a new array, often used for "chopping" up the input
Expand Down
4 changes: 2 additions & 2 deletions src/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,8 @@ export function uniq<A>(E: Eq<A>): (as: ReadonlyArray<A>) => ReadonlyArray<A> {
* @category combinators
* @since 2.5.0
*/
export function sortBy<A>(ords: ReadonlyArray<Ord<A>>): (as: ReadonlyArray<A>) => ReadonlyArray<A> {
const M = getOrdMonoid<A>()
export function sortBy<B>(ords: ReadonlyArray<Ord<B>>): <A extends B>(as: ReadonlyArray<A>) => ReadonlyArray<A> {
const M = getOrdMonoid<B>()
return sort(ords.reduce(M.concat, M.empty))
}

Expand Down

0 comments on commit 003028c

Please sign in to comment.