-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Comparator and PairSorting (#1937)
Co-authored-by: Julien Deniau <julien@deniau.me>
- Loading branch information
1 parent
376944d
commit f0f3932
Showing
5 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { List, OrderedSet, Seq, Comparator, PairSorting } from 'immutable'; | ||
|
||
const sourceNumbers: readonly number[] = [3, 4, 5, 6, 7, 9, 10, 12, 90, 92, 95]; | ||
|
||
const expectedSortedNumbers: readonly number[] = [ | ||
7, 95, 90, 92, 3, 5, 9, 4, 6, 10, 12, | ||
]; | ||
|
||
const testComparator: Comparator<number> = (left, right) => { | ||
//The number 7 always goes first... | ||
if (left == 7) { | ||
return PairSorting.LeftThenRight; | ||
} else if (right == 7) { | ||
return PairSorting.RightThenLeft; | ||
} | ||
|
||
//...followed by numbers >= 90, then by all the others. | ||
if (left >= 90 && right < 90) { | ||
return PairSorting.LeftThenRight; | ||
} else if (left < 90 && right >= 90) { | ||
return PairSorting.RightThenLeft; | ||
} | ||
|
||
//Within each group, even numbers go first... | ||
if (left % 2 && !(right % 2)) { | ||
return PairSorting.LeftThenRight; | ||
} else if (!(left % 2) && right % 2) { | ||
return PairSorting.RightThenLeft; | ||
} | ||
|
||
//...and, finally, sort the numbers of each subgroup in ascending order. | ||
return left - right; | ||
}; | ||
|
||
describe.each([ | ||
['List', List], | ||
['OrderedSet', OrderedSet], | ||
['Seq.Indexed', Seq.Indexed], | ||
])('Comparator applied to %s', (_collectionName, testCollectionConstructor) => { | ||
const sourceCollection = testCollectionConstructor(sourceNumbers); | ||
|
||
const expectedSortedCollection = testCollectionConstructor( | ||
expectedSortedNumbers | ||
); | ||
|
||
describe('when sorting', () => { | ||
it('should support the enum as well as numeric return values', () => { | ||
const actualCollection = sourceCollection.sort(testComparator); | ||
expect(actualCollection).toEqual(expectedSortedCollection); | ||
}); | ||
}); | ||
|
||
describe('when retrieving the max value', () => { | ||
it('should support the enum as well as numeric return values', () => { | ||
const actualMax = sourceCollection.max(testComparator); | ||
expect(actualMax).toBe(12); | ||
}); | ||
}); | ||
|
||
describe('when retrieving the min value', () => { | ||
it('should support the enum as well as numeric return values', () => { | ||
const actualMin = sourceCollection.min(testComparator); | ||
expect(actualMin).toBe(7); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const PairSorting = { | ||
LeftThenRight: -1, | ||
RightThenLeft: +1, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters