sortism 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export type InternalSortFunction = <T>(arr: T[], func: (v: T) => number, order: "ascending" | "descending") => T[];
2
- export type ExportSortFunction<T> = (order: "ascending" | "descending", func?: (v: T) => number) => T[];
1
+ export type InternalSortFunction = <T>(arr: T[], func: (v: T) => number, order: "ascending" | "descending") => T[] | Promise<T[]>;
2
+ export type SortFunction<T> = (order: "ascending" | "descending", func?: (v: T) => number) => T[] | Promise<T[]>;
3
3
  /**
4
4
  * You can use sortism through this function.
5
5
  */
@@ -7,33 +7,37 @@ export default function sortism<T = any>(array: T[], func?: (v: T) => number): {
7
7
  /**
8
8
  * Time: O(n^2)
9
9
  */
10
- bubble: ExportSortFunction<T>;
10
+ bubble: SortFunction<T>;
11
11
  /**
12
12
  * Time: O(n^2)
13
13
  */
14
- selection: ExportSortFunction<T>;
14
+ selection: SortFunction<T>;
15
15
  /**
16
16
  * Time: O(n log n)
17
17
  */
18
- merge: ExportSortFunction<T>;
18
+ merge: SortFunction<T>;
19
19
  /**
20
20
  * Time: O(n log n)
21
21
  */
22
- quick: ExportSortFunction<T>;
22
+ quick: SortFunction<T>;
23
23
  /**
24
24
  * Time: O(n * n!)
25
25
  */
26
- bogo: ExportSortFunction<T>;
26
+ bogo: SortFunction<T>;
27
27
  /**
28
28
  * Time: O(N)
29
29
  */
30
- stalin: ExportSortFunction<T>;
30
+ stalin: SortFunction<T>;
31
31
  /**
32
32
  * Time: O(Infinity)
33
33
  */
34
- miracle: ExportSortFunction<T>;
34
+ miracle: SortFunction<T>;
35
35
  /**
36
36
  * Time: O(N)
37
37
  */
38
- thanos: ExportSortFunction<T>;
38
+ thanos: SortFunction<T>;
39
+ /**
40
+ * Time: O(max(N, maxValue))
41
+ */
42
+ sleep: SortFunction<T>;
39
43
  };
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import merge from "./sorts/merge";
4
4
  import miracle from "./sorts/miracle";
5
5
  import quick from "./sorts/quick";
6
6
  import selection from "./sorts/selection";
7
+ import sleep from "./sorts/sleep";
7
8
  import stalin from "./sorts/stalin";
8
9
  import thanos from "./sorts/thanos";
9
10
  /**
@@ -44,5 +45,9 @@ export default function sortism(array, func) {
44
45
  * Time: O(N)
45
46
  */
46
47
  "thanos": (order => thanos(array, f, order)),
48
+ /**
49
+ * Time: O(max(N, maxValue))
50
+ */
51
+ "sleep": (order => sleep(array, f, order))
47
52
  };
48
53
  }
@@ -0,0 +1,3 @@
1
+ import type { InternalSortFunction } from "..";
2
+ declare const sleep: InternalSortFunction;
3
+ export default sleep;
@@ -0,0 +1,18 @@
1
+ const sleep = async (arr, func, order) => {
2
+ const indexMap = arr.map(v => ({
3
+ index: func(v),
4
+ value: v
5
+ }));
6
+ const isAsc = order === "ascending";
7
+ const promises = indexMap.map(v => new Promise(resolve => {
8
+ const delay = Math.max(0, v.index);
9
+ setTimeout(() => {
10
+ resolve(v);
11
+ }, delay);
12
+ }));
13
+ const result = await Promise.all(promises);
14
+ return isAsc
15
+ ? result.map(v => v.value)
16
+ : result.reverse().map(v => v.value);
17
+ };
18
+ export default sleep;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "sortism",
3
3
  "description": "The easiest way to sort an array.",
4
4
  "main": "./dist/index.js",
5
- "version": "1.2.0",
5
+ "version": "1.3.0",
6
6
  "type": "module",
7
7
  "keywords": [
8
8
  "sort",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import merge from "./sorts/merge";
4
4
  import miracle from "./sorts/miracle";
5
5
  import quick from "./sorts/quick";
6
6
  import selection from "./sorts/selection";
7
+ import sleep from "./sorts/sleep";
7
8
  import stalin from "./sorts/stalin";
8
9
  import thanos from "./sorts/thanos";
9
10
 
@@ -11,12 +12,12 @@ export type InternalSortFunction = <T>(
11
12
  arr: T[],
12
13
  func: (v: T) => number,
13
14
  order: "ascending" | "descending",
14
- ) => T[];
15
+ ) => T[] | Promise<T[]>;
15
16
 
16
- export type ExportSortFunction<T> = (
17
+ export type SortFunction<T> = (
17
18
  order: "ascending" | "descending",
18
19
  func?: (v: T) => number,
19
- ) => T[];
20
+ ) => T[] | Promise<T[]>;
20
21
 
21
22
  /**
22
23
  * You can use sortism through this function.
@@ -28,34 +29,38 @@ export default function sortism<T = any>(array: T[], func?: (v: T) => number) {
28
29
  /**
29
30
  * Time: O(n^2)
30
31
  */
31
- "bubble": (order => bubble(array, f, order)) as ExportSortFunction<T>,
32
+ "bubble": (order => bubble(array, f, order)) as SortFunction<T>,
32
33
  /**
33
34
  * Time: O(n^2)
34
35
  */
35
- "selection": (order => selection(array, f, order)) as ExportSortFunction<T>,
36
+ "selection": (order => selection(array, f, order)) as SortFunction<T>,
36
37
  /**
37
38
  * Time: O(n log n)
38
39
  */
39
- "merge": (order => merge(array, f, order)) as ExportSortFunction<T>,
40
+ "merge": (order => merge(array, f, order)) as SortFunction<T>,
40
41
  /**
41
42
  * Time: O(n log n)
42
43
  */
43
- "quick": (order => quick(array, f, order)) as ExportSortFunction<T>,
44
+ "quick": (order => quick(array, f, order)) as SortFunction<T>,
44
45
  /**
45
46
  * Time: O(n * n!)
46
47
  */
47
- "bogo": (order => bogo(array, f, order)) as ExportSortFunction<T>,
48
+ "bogo": (order => bogo(array, f, order)) as SortFunction<T>,
48
49
  /**
49
50
  * Time: O(N)
50
51
  */
51
- "stalin": (order => stalin(array, f, order)) as ExportSortFunction<T>,
52
+ "stalin": (order => stalin(array, f, order)) as SortFunction<T>,
52
53
  /**
53
54
  * Time: O(Infinity)
54
55
  */
55
- "miracle": (order => miracle(array, f, order)) as ExportSortFunction<T>,
56
+ "miracle": (order => miracle(array, f, order)) as SortFunction<T>,
56
57
  /**
57
58
  * Time: O(N)
58
59
  */
59
- "thanos": (order => thanos(array, f, order)) as ExportSortFunction<T>,
60
+ "thanos": (order => thanos(array, f, order)) as SortFunction<T>,
61
+ /**
62
+ * Time: O(max(N, maxValue))
63
+ */
64
+ "sleep": (order => sleep(array, f, order)) as SortFunction<T>
60
65
  };
61
66
  }
@@ -0,0 +1,28 @@
1
+ import type { InternalSortFunction } from "..";
2
+
3
+ const sleep: InternalSortFunction = async (arr, func, order) => {
4
+ const indexMap = arr.map(v => ({
5
+ index: func(v),
6
+ value: v
7
+ }));
8
+
9
+ const isAsc = order === "ascending";
10
+
11
+ const promises = indexMap.map(v =>
12
+ new Promise<typeof indexMap[number]>(resolve => {
13
+ const delay = Math.max(0, v.index);
14
+
15
+ setTimeout(() => {
16
+ resolve(v);
17
+ }, delay);
18
+ })
19
+ );
20
+
21
+ const result = await Promise.all(promises);
22
+
23
+ return isAsc
24
+ ? result.map(v => v.value)
25
+ : result.reverse().map(v => v.value);
26
+ };
27
+
28
+ export default sleep;
package/tests/index.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import sortism from './../dist';
2
2
 
3
- const arr = [0,15,126,74,4,2234,235,23,574,4578,2];
3
+ const arr = [0, 15, 126, 74, 4, 2234, 235, 23, 574, 4578, 2];
4
4
  const sort = sortism(arr);
5
5
 
6
- const result = sort.thanos("ascending");
6
+ (async () => {
7
+ const result = await sort.sleep("ascending");
7
8
 
8
- console.log(result);
9
+ console.log(result);
10
+ })();
package/bun.lock DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "configVersion": 1,
4
- "workspaces": {
5
- "": {
6
- "name": "sortism",
7
- "devDependencies": {
8
- "@types/bun": "latest",
9
- },
10
- "peerDependencies": {
11
- "typescript": "^5",
12
- },
13
- },
14
- },
15
- "packages": {
16
- "@types/bun": ["@types/bun@1.3.11", "", { "dependencies": { "bun-types": "1.3.11" } }, "sha512-5vPne5QvtpjGpsGYXiFyycfpDF2ECyPcTSsFBMa0fraoxiQyMJ3SmuQIGhzPg2WJuWxVBoxWJ2kClYTcw/4fAg=="],
17
-
18
- "@types/node": ["@types/node@25.5.0", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw=="],
19
-
20
- "bun-types": ["bun-types@1.3.11", "", { "dependencies": { "@types/node": "*" } }, "sha512-1KGPpoxQWl9f6wcZh57LvrPIInQMn2TQ7jsgxqpRzg+l0QPOFvJVH7HmvHo/AiPgwXy+/Thf6Ov3EdVn1vOabg=="],
21
-
22
- "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
23
-
24
- "undici-types": ["undici-types@7.18.2", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],
25
- }
26
- }