8000 Fix Map.sort and Set.sort TS return type · immutable-js/immutable-js@1cae0b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cae0b5

Browse files
committed
Fix Map.sort and Set.sort TS return type
1 parent bde3119 commit 1cae0b5

File tree

5 files changed

+167
-10
lines changed

5 files changed

+167
-10
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"rollup": "3.28.1",
128128
"size-limit": "^8.2.6",
129129
"transducers-js": "0.4.174",
130-
"tstyche": "^2.0.0",
130+
"tstyche": "^2.1.1",
131131
"typescript": "5.1"
132132
},
133133
"size-limit": [

type-definitions/immutable.d.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,65 @@ declare namespace Immutable {
15911591
* @see Collection.Keyed.flip
15921592
*/
15931593
flip(): Map<V, K>;
1594+
1595+
/**
1596+
* Returns an OrderedMap of the same type which includes the same entries,
1597+
* stably sorted by using a `comparator`.
1598+
*
1599+
* If a `comparator` is not provided, a default comparator uses `<` and `>`.
1600+
*
1601+
* `comparator(valueA, valueB)`:
1602+
*
1603+
* * Returns `0` if the elements should not be swapped.
1604+
* * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
1605+
* * Returns `1` (or any positive number) if `valueA` comes after `valueB`
1606+
* * Alternatively, can return a value of the `PairSorting` enum type
1607+
* * Is pure, i.e. it must always return the same value for the same pair
1608+
* of values.
1609+
*
1610+
* <!-- runkit:activate -->
1611+
* ```js
1612+
* const { Map } = require('immutable')
1613+
* Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
1614+
* if (a < b) { return -1; }
1615+
* if (a > b) { return 1; }
1616+
* if (a === b) { return 0; }
1617+
* });
1618+
* // OrderedMap { "a": 1, "b": 2, "c": 3 }
1619+
* ```
1620+
*
1621+
* Note: `sort()` Always returns a new instance, even if the original was
1622+
* already sorted.
1623+
*
1624+
* Note: This is always an eager operation.
1625+
*/
1626+
sort(comparator?: Comparator<V>): OrderedMap<K, V>;
1627+
1628+
/**
1629+
* Like `sort`, but also accepts a `comparatorValueMapper` which allows for
1630+
* sorting by more sophisticated means:
1631+
*
1632+
* <!-- runkit:activate -->
1633+
* ```js
1634+
* const { Map } = require('immutable')
1635+
* const beattles = Map({
1636+
* John: { name: "Lennon" },
1637+
* Paul: { name: "McCartney" },
1638+
* George: { name: "Harrison" },
1639+
* Ringo: { name: "Starr" },
1640+
* });
1641+
* beattles.sortBy(member => member.name);
1642+
* ```
1643+
*
1644+
* Note: `sortBy()` Always returns a new instance, even if the original was
1645+
* already sorted.
1646+
*
1647+
* Note: This is always an eager operation.
1648+
*/
1649+
sortBy<C>(
1650+
comparatorValueMapper: (value: V, key: K, iter: this) => C,
1651+
comparator?: (valueA: C, valueB: C) => number
1652+
): OrderedMap<K, V>;
15941653
}
15951654

15961655
/**
@@ -2012,6 +2071,65 @@ declare namespace Immutable {
20122071
predicate: (this: C, value: T, key: T, iter: this) => unknown,
20132072
context?: C
20142073
): [this, this];
2074+
2075+
/**
2076+
* Returns an OrderedSet of the same type which includes the same entries,
2077+
* stably sorted by using a `comparator`.
2078+
*
2079+
* If a `comparator` is not provided, a default comparator uses `<` and `>`.
2080+
*
2081+
* `comparator(valueA, valueB)`:
2082+
*
2083+
* * Returns `0` if the elements should not be swapped.
2084+
* * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
2085+
* * Returns `1` (or any positive number) if `valueA` comes after `valueB`
2086+
* * Alternatively, can return a value of the `PairSorting` enum type
2087+
* * Is pure, i.e. it must always return the same value for the same pair
2088+
* of values.
2089+
*
2090+
* <!-- runkit:activate -->
2091+
* ```js
2092+
* const { Set } = require('immutable')
2093+
* Set(['b', 'a', 'c']).sort((a, b) => {
2094+
* if (a < b) { return -1; }
2095+
* if (a > b) { return 1; }
2096+
* if (a === b) { return 0; }
2097+
* });
2098+
* // OrderedSet { "a":, "b", "c" }
2099+
* ```
2100+
*
2101+
* Note: `sort()` Always returns a new instance, even if the original was
2102+
* already sorted.
2103+
*
2104+
* Note: This is always an eager operation.
2105+
*/
2106+
sort(comparator?: Comparator<T>): OrderedSet<T>;
2107+
2108+
/**
2109+
* Like `sort`, but also accepts a `comparatorValueMapper` which allows for
2110+
* sorting by more sophisticated means:
2111+
*
2112+
* <!-- runkit:activate -->
2113+
* ```js
2114+
* const { Set } = require('immutable')
2115+
* const beattles = Set([
2116+
* { name: "Lennon" },
2117+
* { name: "McCartney" },
2118+
* { name: "Harrison" },
2119+
* { name: "Starr" },
2120+
* ]);
2121+
* beattles.sortBy(member => member.name);
2122+
* ```
2123+
*
2124+
* Note: `sortBy()` Always returns a new instance, even if the original was
2125+
* already sorted.
2126+
*
2127+
* Note: This is always an eager operation.
2128+
*/
2129+
sortBy<C>(
2130+
comparatorValueMapper: (value: T, key: T, iter: this) => C,
2131+
comparator?: (valueA: C, valueB: C) => number
2132+
): OrderedSet<T>;
20152133
}
20162134

20172135
/**

type-definitions/ts-tests/map.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'tstyche';
2-
import { Map, List, MapOf } from 'immutable';
2+
import { Map, List, MapOf, OrderedMap } from 'immutable';
33

44
test('#constructor', () => {
55
expect(Map()).type.toBe<Map<unknown, unknown>>();
@@ -602,6 +602,29 @@ test('#flip', () => {
602602
expect(Map<number, string>().flip()).type.toBe<Map<string, number>>();
603603
});
604604

605+
test('#sort', () => {
606+
expect(Map<string, string>().sort()).type.toBe<OrderedMap<string, string>>();
607+
expect(Map<string, string>().sort((a, b) => 1)).type.toBe<
608+
OrderedMap<string, string>
609+
>();
610+
611+
expect(Map({ a: 'a' }).sort()).type.toBe<OrderedMap<'a', string>>();
612+
});
613+
614+
test('#sortBy', () => {
615+
expect(Map<string, string>().sortBy(v => v)).type.toBe<
616+
OrderedMap<string, string>
617+
>();
618+
619+
expect(
620+
Map<string, string>().sortBy(
621+
v => v,
622+
(a, b) => 1
623+
)
624+
).type.toBe<OrderedMap<string, string>>();
625+
expect(Map({ a: 'a' }).sortBy(v => v)).type.toBe<OrderedMap<'a', string>>();
626+
});
627+
605628
test('#withMutations', () => {
606629
expect(Map<number, number>().withMutations(mutable => mutable)).type.toBe<
607630
Map<number, number>

type-definitions/ts-tests/set.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'tstyche';
2-
import { Set, Map, Collection } from 'immutable';
2+
import { Set, Map, Collection, OrderedSet } from 'immutable';
33

44
test('#constructor', () => {
55
expect(Set()).type.toBe<Set<unknown>>();
@@ -229,6 +229,22 @@ test('#flatten', () => {
229229
expect(Set<number>().flatten('a')).type.toRaiseError();
230230
});
231231

232+
test('#sort', () => {
233+
expect(Set<string>().sort()).type.toBe<OrderedSet<string>>();
234+
expect(Set<string>().sort((a, b) => 1)).type.toBe<OrderedSet<string>>();
235+
});
236+
237+
test('#sortBy', () => {
238+
expect(Set<string>().sortBy(v => v)).type.toBe<OrderedSet<string>>();
239+
240+
expect(
241+
Set<string>().sortBy(
242+
v => v,
243+
(a, b) => 1
244+
)
245+
).type.toBe<OrderedSet<string>>();
246+
});
247+
232248
test('#withMutations', () => {
233249
expect(Set<number>().withMutations(mutable => mutable)).type.toBe<
234250
Set<number>

0 commit comments

Comments
 (0)
0