Skip to content

Commit

Permalink
feat(): add new arr method sort
Browse files Browse the repository at this point in the history
  • Loading branch information
houssenedao committed Jan 27, 2022
1 parent 5dc9f12 commit 9c42653
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
15 changes: 1 addition & 14 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@
"type-enum": [
2,
"always",
[
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"style",
"test",
"revert",
"BREAKING CHANGE"
]
["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test", "revert"]
]
}
}
1 change: 0 additions & 1 deletion lib/arr/array.interface.ts

This file was deleted.

28 changes: 25 additions & 3 deletions lib/arr/array.spec.ts
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']);
});
});
});
15 changes: 14 additions & 1 deletion lib/arr/array.ts
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);
}
};
7 changes: 7 additions & 0 deletions lib/exceptions/arr.exception.ts
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);
}
}
1 change: 1 addition & 0 deletions lib/exceptions/index.ts
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';

0 comments on commit 9c42653

Please sign in to comment.