From 050bbd41a76db46c382262e2e8bf73f333240aea Mon Sep 17 00:00:00 2001 From: ROBOT Date: Tue, 29 Apr 2025 15:55:02 +0900 Subject: [PATCH 1/3] fix: sort class --- .changeset/fruity-cobras-find.md | 5 +++ src/Sort/SortBase.ts | 64 +++++++++++++------------------- src/index.ts | 10 +++-- 3 files changed, 37 insertions(+), 42 deletions(-) create mode 100644 .changeset/fruity-cobras-find.md diff --git a/.changeset/fruity-cobras-find.md b/.changeset/fruity-cobras-find.md new file mode 100644 index 0000000..4bb957b --- /dev/null +++ b/.changeset/fruity-cobras-find.md @@ -0,0 +1,5 @@ +--- +"sortman": major +--- + +Fix sort args type diff --git a/src/Sort/SortBase.ts b/src/Sort/SortBase.ts index 26f8008..455e1a1 100644 --- a/src/Sort/SortBase.ts +++ b/src/Sort/SortBase.ts @@ -1,28 +1,21 @@ import { z } from "zod"; import { inversion } from "../utils/array"; -export type SortOptions = { - desc?: boolean; -}; - -export type SortNumArr = { - arr: number[]; - options?: SortOptions; -}; - -export type SortNotNumArr = { - arr: T[]; - func: (e: T) => number; - options?: SortOptions; -}; - -export type SortElement = T extends number ? T[] | SortNumArr : SortNotNumArr; - export type SortCoreElement = Array<{ num: number; content: T; }>; +export type SortOptionsFunc = { + func: (e: T) => number; +}; + +export type SortOptions = (T extends number + ? Partial> + : Required>) & { + desc?: boolean; +}; + export abstract class SortBase { private isNumArray(arr: unknown): arr is number[] { try { @@ -33,30 +26,24 @@ export abstract class SortBase { } } - private getOptions(content: SortElement): SortOptions { - if (content instanceof Array) { - return {}; - } else { - return content.options ?? {}; - } - } - - public sort(content: SortElement): T[] { - const options = this.getOptions(content); + public sort(arr: T[]): T[]; + public sort(arr: T[], options: SortOptions): T[]; + public sort(arr: T[], options?: SortOptions): T[] { const elements = (() => { - if (content instanceof Array) { - return content.map((value) => { - return { num: value, content: value as T }; - }); - } - if (this.isNumArray(content.arr)) { - return content.arr.map((value) => { - return { num: value, content: value as T }; + if (this.isNumArray(arr)) { + return arr.map((value) => { + return { + num: (options && options.func ? options.func(value) : value) as number, + content: value + }; }); } else { - const { arr, func } = content as SortNotNumArr; + const { func } = options as SortOptionsFunc; return arr.map((value) => { - return { num: func(value), content: value }; + return { + num: func(value), + content: value + }; }); } })() satisfies SortCoreElement; @@ -67,11 +54,12 @@ export abstract class SortBase { const result = this.core(elements).map(({ content }) => content); - if (options.desc) { + if (options && options.desc) { return inversion(result); } return result; } + public abstract core(content: SortCoreElement): SortCoreElement; } diff --git a/src/index.ts b/src/index.ts index b93bfe2..bb9a8fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { MergeSort } from "./Sort/MergeSort"; import { HeapSort } from "./Sort/HeapSort"; import { ShellSort } from "./Sort/ShellSort"; -import type { SortElement } from "./Sort/SortBase"; +import type { SortOptions } from "./Sort/SortBase"; export class SortMan { public static readonly bogo = new BogoSort(); @@ -19,11 +19,13 @@ export class SortMan { public static readonly insertion = new InsertionSort(); public static readonly selection = new SelectionSort(); - public static sort(content: SortElement): T[] { + public static sort(arr: T[]): T[]; + public static sort(arr: T[], options: SortOptions): T[]; + public static sort(arr: T[], options?: SortOptions): T[] { try { - return this.merge.sort(content); + return this.merge.sort(arr, options as SortOptions); } catch { - return this.insertion.sort(content); + return this.insertion.sort(arr, options as SortOptions); } } } From a9f51ece95d35cf7906ba29d5870d991a6103f72 Mon Sep 17 00:00:00 2001 From: ROBOT Date: Tue, 29 Apr 2025 15:55:45 +0900 Subject: [PATCH 2/3] fix: benchmark algorithm and test code --- .changeset/lucky-results-take.md | 5 +++++ benchmarks/sort.bench.ts | 7 ++++--- tests/sort.test.ts | 14 +++++--------- 3 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 .changeset/lucky-results-take.md diff --git a/.changeset/lucky-results-take.md b/.changeset/lucky-results-take.md new file mode 100644 index 0000000..6d7ea8f --- /dev/null +++ b/.changeset/lucky-results-take.md @@ -0,0 +1,5 @@ +--- +"sortman": major +--- + +Fix benchmark algorithm and test code diff --git a/benchmarks/sort.bench.ts b/benchmarks/sort.bench.ts index 967fbae..b268378 100644 --- a/benchmarks/sort.bench.ts +++ b/benchmarks/sort.bench.ts @@ -13,11 +13,12 @@ for (const { name, algorithm } of sorts) { algorithm.sort(numArrData); }); bench("Number array in option", () => { - algorithm.sort({ arr: numArrData }); + algorithm.sort(objectArrData, { + func: (e) => e.num + }); }); bench("Object array", () => { - algorithm.sort({ - arr: objectArrData, + algorithm.sort(objectArrData, { func: (e) => e.num }); }); diff --git a/tests/sort.test.ts b/tests/sort.test.ts index 091013d..5c6f9a5 100644 --- a/tests/sort.test.ts +++ b/tests/sort.test.ts @@ -11,14 +11,13 @@ for (const { name, algorithm, options } of sorts) { expect(sortCheck(result)).toBe(true); }); test("Number array with options", () => { - const result = algorithm.sort({ - arr: numArrData + const result = algorithm.sort(numArrData, { + func: (e) => e }); expect(sortCheck(result)).toBe(true); }); test("Object array", () => { - const result = algorithm.sort({ - arr: objectArrData, + const result = algorithm.sort(objectArrData, { func: (e) => e.num }); expect(sortCheck(result.map(({ num }) => num))).toBe(true); @@ -26,11 +25,8 @@ for (const { name, algorithm, options } of sorts) { }); describe("Option test", () => { test("Desc", () => { - const result = algorithm.sort({ - arr: numArrData, - options: { - desc: true - } + const result = algorithm.sort(numArrData, { + desc: true }); expect(sortCheck(result, true)).toBe(true); }); From 785547535e87d20398e4a6e98348cefef00277e4 Mon Sep 17 00:00:00 2001 From: ROBOT Date: Tue, 29 Apr 2025 15:57:47 +0900 Subject: [PATCH 3/3] fix: readme's code template --- .changeset/icy-brooms-call.md | 5 +++++ README.md | 10 +++------- 2 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 .changeset/icy-brooms-call.md diff --git a/.changeset/icy-brooms-call.md b/.changeset/icy-brooms-call.md new file mode 100644 index 0000000..c137470 --- /dev/null +++ b/.changeset/icy-brooms-call.md @@ -0,0 +1,5 @@ +--- +"sortman": major +--- + +Fix readme's code template diff --git a/README.md b/README.md index f6fca96..9189b41 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,7 @@ const data = [ } ]; -const sorted = SortMan.sort({ - arr: data, +const sorted = SortMan.sort(data, { func: (e) => e.i }); @@ -67,11 +66,8 @@ import { SortMan } from "sortman"; const data = [200, 300, 100]; -const sorted = SortMan.sort({ - arr: data, - options: { - desc: true - } +const sorted = SortMan.sort(data, { + desc: true }); console.log(sorted);