-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dc9f12
commit 9c42653
Showing
6 changed files
with
48 additions
and
19 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
This file was deleted.
Oops, something went wrong.
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,13 +1,35 @@ | ||
import { Arr } from './array'; | ||
|
||
describe('Arr', () => { | ||
let arr = new Arr(); | ||
// let arr: Arr; | ||
|
||
beforeEach(async () => { | ||
// | ||
// arr = new Arr() | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(arr).toBeDefined(); | ||
expect(Arr).toBeDefined(); | ||
}); | ||
|
||
describe('sort', () => { | ||
it('should have sort method', () => { | ||
expect(Arr).toHaveProperty('sort'); | ||
}); | ||
|
||
it('should use sort method with array number', () => { | ||
const array = [2, 1, 4, 10]; | ||
|
||
expect(Arr.sort(array)).toEqual([1, 10, 2, 4]); | ||
expect(Arr.sort(array, (a, b) => a - b)).toEqual([1, 2, 4, 10]); | ||
expect(Arr.sort(array, (a, b) => b - a)).toEqual([10, 4, 2, 1]); | ||
}); | ||
|
||
it('should use sort method with array number', () => { | ||
const string = ['a', 'd', 'b', 'e', 'c']; | ||
|
||
expect(Arr.sort(string)).toEqual(string); | ||
expect(Arr.sort(string, (a, b) => a.localeCompare(b))).toEqual(['a', 'b', 'c', 'd', 'e']); | ||
expect(Arr.sort(string, (a, b) => b.localeCompare(a))).toEqual(['e', 'd', 'c', 'b', 'a']); | ||
}); | ||
}); | ||
}); |
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 +1,14 @@ | ||
export class Arr<T> extends Array<T> {} | ||
import { ArrException } from '../exceptions'; | ||
|
||
export const Arr = class { | ||
/** | ||
* Sort array. | ||
* @param array | ||
* @param fn | ||
*/ | ||
static sort(array: Array<any>, fn?: (a, b) => number) { | ||
if (!Array.isArray(array)) throw new ArrException(`Your argument is not array`); | ||
|
||
return array.sort(fn); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class ArrException extends Error { | ||
name: 'ArrException'; | ||
|
||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from './str.exception'; | ||
export * from './num.exception'; | ||
export * from './arr.exception'; |