-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor dataSortBy and dataReverse functions
- Loading branch information
Showing
2 changed files
with
73 additions
and
52 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 |
---|---|---|
@@ -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 GitHub Actions / test (ubuntu-latest)src/data.test.ts > dataFlatten
Check failure on line 72 in src/data.test.ts GitHub Actions / test (macos-latest)src/data.test.ts > dataFlatten
|
||
|
||
// 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]); | ||
}); |
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