Skip to content

Commit

Permalink
Refactor dataSortBy and dataReverse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmymbtlr committed May 22, 2024
1 parent c8e4dfd commit b3f8fe1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 52 deletions.
92 changes: 54 additions & 38 deletions src/data.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,81 @@
import { expect, test } from 'vitest'
import * as mod from './data'
import { expect, test } from 'vitest';
import * as mod from './data';

const arrayExample = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 },
{ name: 'Jake', age: 20 }
]
const arrayExample = [{ name: 'John', age: 25 },{ name: 'Jane', age: 30 },{ name: 'Jake', age: 20 }];

const objectExample = {
const objectExample: { name: string; age: number; country: string } = {
name: 'John',
age: 25,
country: 'USA'
}
};

// test('dataShuffle', () => {
// expect(mod.dataShuffle(items)).not.toStrictEqual(items)
// })
const nestedArray = [1, [2, 3], [4, [5, 6]]];
const duplicateArray = [1, 2, 2, 3, 4, 5, 5, 5];
const stringArray = ['apple', 'banana', 'apple', 'cherry', 'banana'];
const mixedArray = [1, 2, 3, 4, 5];

test('dataReverse', () => {
expect(mod.dataReverse(arrayExample)).toStrictEqual([
{ name: 'Jake', age: 20 },
test('dataSortBy', () => {
// Empty Property
expect(mod.dataSortBy(arrayExample)).toStrictEqual([
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 },
{ name: 'John', age: 25 }
])
expect(mod.dataReverse(objectExample)).toStrictEqual({
country: 'USA',
age: 25,
name: 'John',
})
})

test('dataRemoveDuplicates', () => {
expect(mod.dataRemoveDuplicates([1, 2, 2, 3, 4, 5, 5, 5])).toStrictEqual([1, 2, 3, 4, 5])
expect(mod.dataRemoveDuplicates(['apple', 'banana', 'apple', 'cherry', 'banana'])).toStrictEqual(['apple', 'banana', 'cherry'])
})
{ name: 'Jake', age: 20 }
]);

test('dataSortBy', () => {
// Sort by age
expect(mod.dataSortBy(arrayExample, { property: 'age' })).toStrictEqual([
{ name: 'Jake', age: 20 },
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 }
])
]);

// Sort by age desc
expect(mod.dataSortBy(arrayExample, { property: 'age', order: 'desc' })).toStrictEqual([
{ name: 'Jane', age: 30 },
{ name: 'John', age: 25 },
{ name: 'Jake', age: 20 }
])
]);

expect(mod.dataSortBy(arrayExample, { property: 'name' })).toStrictEqual([
{ name: 'Jake', age: 20 },
{ name: 'Jane', age: 30 },
{ name: 'John', age: 25 }
])
})
]);
});

test('dataReverse', () => {
const arrayExample = [{ name: 'John', age: 25 },{ name: 'Jane', age: 30 },{ name: 'Jake', age: 20 }];
expect(mod.dataReverse(arrayExample)).toStrictEqual([
{ name: 'Jake', age: 20 },
{ name: 'Jane', age: 30 },
{ name: 'John', age: 25 }
]);
expect(mod.dataReverse(objectExample)).toMatchObject({
country: 'USA',
age: 25,
name: 'John',
});

// Empty Items
expect(mod.dataReverse([])).toStrictEqual([]);
});

test('dataRemoveDuplicates', () => {
expect(mod.dataRemoveDuplicates(duplicateArray)).toStrictEqual([1, 2, 3, 4, 5]);
expect(mod.dataRemoveDuplicates(stringArray)).toStrictEqual(['apple', 'banana', 'cherry']);
});

test('dataFlatten', () => {
expect(mod.dataFlatten([1, [2, 3], [4, [5, 6]]])).toStrictEqual([1, 2, 3, 4, 5, 6])
})
expect(mod.dataFlatten(nestedArray)).toStrictEqual([1, 2, 3, 4, 5, 6]);

// Objects
expect(mod.dataFlatten({ a: { b: 1 }, c: { d: 2 } })).toStrictEqual({ b: 1, d: 2 });

Check failure on line 72 in src/data.test.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

src/data.test.ts > dataFlatten

AssertionError: expected { a: { b: 1 }, c: { d: 2 } } to strictly equal { b: 1, d: 2 } - Expected + Received Object { + "a": Object { "b": 1, + }, + "c": Object { "d": 2, + }, } ❯ src/data.test.ts:72:57

Check failure on line 72 in src/data.test.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

src/data.test.ts > dataFlatten

AssertionError: expected { a: { b: 1 }, c: { d: 2 } } to strictly equal { b: 1, d: 2 } - Expected + Received Object { + "a": Object { "b": 1, + }, + "c": Object { "d": 2, + }, } ❯ src/data.test.ts:72:57

// Empty Items
expect(mod.dataFlatten([])).toStrictEqual([]);
});

test('dataWithout', () => {
expect(mod.dataWithout([1, 2, 3, 4, 5], [2, 4])).toStrictEqual([1, 3, 5])
expect(mod.dataWithout([1, 2, 3, 4, 5], 2)).toStrictEqual([1, 3, 4, 5])
})
expect(mod.dataWithout(mixedArray, [2, 4])).toStrictEqual([1, 3, 5]);
expect(mod.dataWithout(mixedArray, 2)).toStrictEqual([1, 3, 4, 5]);
});
33 changes: 19 additions & 14 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@ import { isObject } from './validators'
* Sort an array or object by a property.
*/
export function dataSortBy(items: object | string[] | number[], options?: { property?: string; order?: 'asc' | 'desc' }): object | string[] | number[] {
const comparator = (a: string | number, b: string | number) => {
const property = options?.property
const order = options?.order ?? 'asc'
if (!property) return 0

if (a[property] < b[property]) return order === 'asc' ? -1 : 1
if (a[property] > b[property]) return order === 'asc' ? 1 : -1
return 0
}
const { property, order = 'asc' } = options || {};
if (!property) return items;

const comparator = (a: any, b: any) => {
const aValue = a[property];
const bValue = b[property];

if (Array.isArray(aValue) && Array.isArray(bValue)) {
return (aValue.length - bValue.length) * (order === 'asc' ? 1 : -1);
} else if (typeof aValue === 'string' && typeof bValue === 'string') {
return aValue.localeCompare(bValue) * (order === 'asc' ? 1 : -1);
} else {
return (aValue - bValue) * (order === 'asc' ? 1 : -1);
}
};

if (isObject(items)) {
const entries = Object.entries(items)
return Object.fromEntries(entries.sort((a, b) => comparator(a[1], b[1])) as [string, string | number | object | string[] | number[]][])
return Object.fromEntries(Object.entries(items).sort((a, b) => comparator(a[1], b[1])));
} else {
return (items as string[] | number[]).sort(comparator)
return (items as string[] | number[]).sort(comparator);
}
}

/**
* Reverse an array or object.
*/
export function dataReverse(items: object | string[] | number[]): object | string[] | number[] {
if (!items) {
if ((Array.isArray(items) && items.length === 0) || (!Array.isArray(items) && Object.keys(items).length === 0)) {
console.warn('[MODS] Warning: dataReverse() expects an object or array as the first argument.')
return items
return items;
}

if (isObject(items)) {
Expand Down

0 comments on commit b3f8fe1

Please sign in to comment.