Skip to content

Commit 4184683

Browse files
committed
fix: sortBy parameter parse error
1 parent 614b3d2 commit 4184683

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

android/src/main/java/com/musiclibrary/tracks/GetTracksQuery.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ object GetTracksQuery {
185185

186186
private fun buildSortOrder(sortBy: List<String>): String {
187187
if (sortBy.isEmpty()) {
188-
return "${MediaStore.Audio.Media.DATE_ADDED} DESC"
188+
return "${MediaStore.Audio.Media.DATE_ADDED} ASC"
189189
}
190190

191191
return sortBy.joinToString(", ") { sortOption ->

example/src/TrackList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default function TrackList() {
5959
first: 100,
6060
...options,
6161
after: cursor,
62+
sortBy: ['artist', true],
6263
});
6364

6465
allTracks = [...allTracks, ...result.items];

src/__tests__/index.test.tsx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
it.todo('write a test');
1+
import { normalizeSortBy } from '../utils';
2+
3+
// Mock the native module
4+
jest.mock('../NativeMusicLibrary', () => ({}));
5+
6+
describe('normalizeSortBy', () => {
7+
it('should return empty array when input is undefined', () => {
8+
expect(normalizeSortBy(undefined)).toEqual([]);
9+
});
10+
11+
it('should handle single SortByKey', () => {
12+
expect(normalizeSortBy('default')).toEqual(['default']);
13+
expect(normalizeSortBy('artist')).toEqual(['artist']);
14+
});
15+
16+
it('should handle [SortByKey, boolean] tuple', () => {
17+
expect(normalizeSortBy(['default', true])).toEqual([['default', true]]);
18+
expect(normalizeSortBy(['artist', false])).toEqual([['artist', false]]);
19+
});
20+
21+
it('should handle array of SortByValue', () => {
22+
expect(normalizeSortBy(['default', 'artist'])).toEqual([
23+
'default',
24+
'artist',
25+
]);
26+
expect(
27+
normalizeSortBy([
28+
['default', true],
29+
['artist', false],
30+
])
31+
).toEqual([
32+
['default', true],
33+
['artist', false],
34+
]);
35+
});
36+
37+
it('should handle mixed array of SortByValue', () => {
38+
expect(normalizeSortBy(['default', ['artist', true]])).toEqual([
39+
'default',
40+
['artist', true],
41+
]);
42+
});
43+
});

src/utils.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type AssetsOptions,
55
type InternalAssetsOptions,
66
type InternalSortByValue,
7+
type SortByKey,
78
type SortByValue,
89
} from './NativeMusicLibrary';
910

@@ -53,6 +54,32 @@ export function getId(
5354
return ref ? ref.id : undefined;
5455
}
5556

57+
/**
58+
* Parse sortBy to SortByValue[]
59+
* @param sortBy - SortByValue or SortByValue[]
60+
* @returns SortByValue[]
61+
*/
62+
export function normalizeSortBy(
63+
input: SortByValue | SortByValue[] | undefined
64+
): SortByValue[] {
65+
if (!input) return [['default', true]];
66+
67+
// If input is an array, check if it's a SortByValue[] or [SortByKey, boolean]
68+
if (Array.isArray(input)) {
69+
const isTuple =
70+
input.length === 2 &&
71+
typeof input[0] === 'string' &&
72+
typeof input[1] === 'boolean';
73+
if (isTuple) {
74+
return [input as [SortByKey, boolean]];
75+
} else {
76+
return input as SortByValue[];
77+
}
78+
}
79+
80+
return [input];
81+
}
82+
5683
export function getOptions(
5784
assetsOptions: AssetsOptions
5885
): InternalAssetsOptions {
@@ -62,7 +89,7 @@ export function getOptions(
6289
after: getId(after),
6390
first: first == null ? 20 : first,
6491
directory,
65-
sortBy: arrayize(sortBy),
92+
sortBy: normalizeSortBy(sortBy),
6693
};
6794

6895
if (after != null && typeof options.after !== 'string') {

0 commit comments

Comments
 (0)