From 9c426530f8d64d51ccea027c92dec9b7d054a1c9 Mon Sep 17 00:00:00 2001 From: Houssene Dao Date: Thu, 27 Jan 2022 12:09:33 +0000 Subject: [PATCH] feat(): add new arr method sort --- .commitlintrc.json | 15 +-------------- lib/arr/array.interface.ts | 1 - lib/arr/array.spec.ts | 28 +++++++++++++++++++++++++--- lib/arr/array.ts | 15 ++++++++++++++- lib/exceptions/arr.exception.ts | 7 +++++++ lib/exceptions/index.ts | 1 + 6 files changed, 48 insertions(+), 19 deletions(-) delete mode 100644 lib/arr/array.interface.ts create mode 100644 lib/exceptions/arr.exception.ts diff --git a/.commitlintrc.json b/.commitlintrc.json index d5e0434..e39932e 100644 --- a/.commitlintrc.json +++ b/.commitlintrc.json @@ -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"] ] } } diff --git a/lib/arr/array.interface.ts b/lib/arr/array.interface.ts deleted file mode 100644 index 8337712..0000000 --- a/lib/arr/array.interface.ts +++ /dev/null @@ -1 +0,0 @@ -// diff --git a/lib/arr/array.spec.ts b/lib/arr/array.spec.ts index 5a462e9..fe0801c 100644 --- a/lib/arr/array.spec.ts +++ b/lib/arr/array.spec.ts @@ -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']); + }); }); }); diff --git a/lib/arr/array.ts b/lib/arr/array.ts index b55b685..56920ed 100644 --- a/lib/arr/array.ts +++ b/lib/arr/array.ts @@ -1 +1,14 @@ -export class Arr extends Array {} +import { ArrException } from '../exceptions'; + +export const Arr = class { + /** + * Sort array. + * @param array + * @param fn + */ + static sort(array: Array, fn?: (a, b) => number) { + if (!Array.isArray(array)) throw new ArrException(`Your argument is not array`); + + return array.sort(fn); + } +}; diff --git a/lib/exceptions/arr.exception.ts b/lib/exceptions/arr.exception.ts new file mode 100644 index 0000000..b7398f5 --- /dev/null +++ b/lib/exceptions/arr.exception.ts @@ -0,0 +1,7 @@ +export class ArrException extends Error { + name: 'ArrException'; + + constructor(message: string) { + super(message); + } +} diff --git a/lib/exceptions/index.ts b/lib/exceptions/index.ts index 870fef3..3718fea 100644 --- a/lib/exceptions/index.ts +++ b/lib/exceptions/index.ts @@ -1,2 +1,3 @@ export * from './str.exception'; export * from './num.exception'; +export * from './arr.exception';