deno.land / x / rambda@v9.1.1 / source / sortWith.spec.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import { ascend, prop } from '../rambda.js'import { sortWith } from './sortWith.js'
const albums = [ { artist : 'Rush', genre : 'Rock', score : 3, title : 'A Farewell to Kings', }, { artist : 'Dave Brubeck Quartet', genre : 'Jazz', score : 3, title : 'Timeout', }, { artist : 'Rush', genre : 'Rock', score : 5, title : 'Fly By Night', }, { artist : 'Daniel Barenboim', genre : 'Baroque', score : 3, title : 'Goldberg Variations', }, { artist : 'Glenn Gould', genre : 'Baroque', score : 3, title : 'Art of the Fugue', }, { artist : 'Leonard Bernstein', genre : 'Romantic', score : 4, title : 'New World Symphony', }, { artist : 'Don Byron', genre : 'Jazz', score : 5, title : 'Romance with the Unseen', }, { artist : 'Iron Maiden', genre : 'Metal', score : 2, title : 'Somewhere In Time', }, { artist : 'Danny Holt', genre : 'Modern', score : 1, title : 'In Times of Desparation', }, { artist : 'Various', genre : 'Broadway', score : 3, title : 'Evita', }, { artist : 'Nick Drake', genre : 'Folk', score : 1, title : 'Five Leaves Left', }, { artist : 'John Eliot Gardiner', genre : 'Classical', score : 4, title : 'The Magic Flute', },]
test('sorts by a simple property of the objects', () => { const sortedAlbums = sortWith([ ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('A Farewell to Kings') expect(sortedAlbums[ 11 ].title).toBe('Timeout')})
test('sorts by multiple properties of the objects', () => { const sortedAlbums = sortWith([ ascend(prop('score')), ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('Five Leaves Left') expect(sortedAlbums[ 1 ].title).toBe('In Times of Desparation') expect(sortedAlbums[ 11 ].title).toBe('Romance with the Unseen')})
test('sorts by 3 properties of the objects', () => { const sortedAlbums = sortWith([ ascend(prop('genre')), ascend(prop('score')), ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('Art of the Fugue') expect(sortedAlbums[ 1 ].title).toBe('Goldberg Variations') expect(sortedAlbums[ 11 ].title).toBe('New World Symphony')})
test('sorts by multiple properties using ascend and descend', () => { const sortedAlbums = sortWith([ ascend(prop('score')), ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('Five Leaves Left') expect(sortedAlbums[ 1 ].title).toBe('In Times of Desparation') expect(sortedAlbums[ 11 ].title).toBe('Romance with the Unseen')})
test('sorts only arrays not array-like object', () => { const args = (function (){ return arguments })( 'c', 'a', 'b' ) expect(sortWith([ ascend(prop('value')) ], args)).toEqual([])})
test('sorts only arrays not primitives', () => { const result =sortWith([ (a, b) => a.a === b.a ? 0 : a.a > b.a ? 1 : -1, (a, b) => a.b === b.b ? 0 : a.b > b.b ? 1 : -1, ], [ {a: 1, b: 2}, {a: 2, b: 1}, {a: 2, b: 2}, {a: 1, b: 1}, ]) const expected = [ {a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}, {a: 2, b: 2}, ] expect(result).toEqual(expected)})
Version Info