Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/compat/array/orderBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,17 @@ describe('orderBy', () => {
});

it('should work as an iteratee for methods like `_.map`', () => {
// `map` passes the index as `iteratees` and the array as `orders`, so the index is used as a
// criterion. Numbers have no such property, so the collections are returned in their original order.
expect(
[
[2, 1, 3],
[3, 2, 1],
// @ts-expect-error - type mismatch
].map(orderBy)
).toEqual([
[1, 2, 3],
[1, 2, 3],
[2, 1, 3],
[3, 2, 1],
]);
});

Expand Down Expand Up @@ -239,4 +241,15 @@ describe('orderBy', () => {
expect(orderBy(['A', 'a'])).toEqual(['A', 'a']);
expect(orderBy(['ABC', 'abc'])).toEqual(['ABC', 'abc']);
});

it('should read the property of primitive values', () => {
expect(orderBy(['b', 'aa'], 'length')).toEqual(['b', 'aa']);
expect(orderBy(['b', 'aa'], 'length', 'desc')).toEqual(['aa', 'b']);
expect(orderBy(['ba', 'ab', 'b'], 0)).toEqual(['ab', 'ba', 'b']);
});

it('should keep the original order when primitive values lack the property', () => {
expect(orderBy([3, 1, 2], 'b')).toEqual([3, 1, 2]);
expect(orderBy(['c', 'a', 'b'], 'x')).toEqual(['c', 'a', 'b']);
});
});
6 changes: 1 addition & 5 deletions src/compat/array/orderBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
* // { user: 'fred', age: 40 },
* // ]
*/
export function orderBy<T = any>(collection: any, criteria?: any, orders?: any, guard?: unknown): T[] {

Check warning on line 139 in src/compat/array/orderBy.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 139 in src/compat/array/orderBy.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 139 in src/compat/array/orderBy.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 139 in src/compat/array/orderBy.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (collection == null) {
return [];
}
Expand Down Expand Up @@ -192,15 +192,11 @@
return getValueByNestedPath(object, criterion);
}

if (typeof object === 'object') {
return object[criterion as keyof typeof object];
}

return object;
return object[criterion as keyof typeof object];
};

// Prepare all cases for criteria
const preparedCriteria = criteria.map((criterion: any) => {

Check warning on line 199 in src/compat/array/orderBy.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
// lodash handles a array with one element as a single criterion
if (Array.isArray(criterion) && criterion.length === 1) {
criterion = criterion[0];
Expand All @@ -217,7 +213,7 @@
// Array.prototype.sort() always shifts the `undefined` values to the end of the array. So we have to prevent it by using a wrapper object.
const preparedCollection = (collection as T[]).map(item => ({
original: item,
criteria: preparedCriteria.map((criterion: any) => getValueByCriterion(criterion, item)),

Check warning on line 216 in src/compat/array/orderBy.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}));

return preparedCollection
Expand Down
11 changes: 11 additions & 0 deletions src/compat/array/sortBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
});

it('should treat number values for `collection` as empty', () => {
expect(sortBy(1 as any)).toEqual([]);

Check warning on line 161 in src/compat/array/sortBy.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
});

it('should coerce arrays returned from `iteratee`', () => {
Expand Down Expand Up @@ -191,4 +191,15 @@
expect(sortBy(['A', 'a'])).toEqual(['A', 'a']);
expect(sortBy(['ABC', 'abc'])).toEqual(['ABC', 'abc']);
});

it('should read the property of primitive values', () => {
expect(sortBy(['bb', 'a', 'ccc'], 'length')).toEqual(['a', 'bb', 'ccc']);
expect(sortBy(['b', 'aa'], 'length')).toEqual(['b', 'aa']);
expect(sortBy(['ba', 'ab', 'b'], 0)).toEqual(['ab', 'ba', 'b']);
});

it('should keep the original order when primitive values lack the property', () => {
expect(sortBy([3, 1, 2], 'b')).toEqual([3, 1, 2]);
expect(sortBy(['c', 'a', 'b'], 'x')).toEqual(['c', 'a', 'b']);
});
});