Skip to content

Commit d58a98d

Browse files
committed
refactor(scriptManager): change match and sort function for better readability and futures changes
1 parent c13202f commit d58a98d

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

src/scriptManager.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import type { Metas } from './types.js';
44
import { Script, type ScriptArguments } from './models/Script.js';
5+
import { ScriptExecution } from './models/ScriptExecution.js';
56
import {
67
defaultScriptPath,
78
metaEndTerm,
89
metaStartTerm,
910
NOTIFY,
1011
SCRIPT_EXTENSION
1112
} from './const.js';
12-
import { matchesMetas, stringRange } from './utils/utils.js';
13-
import { ScriptExecution } from './models/ScriptExecution.js';
13+
import { matchesObject, sortByProperty, stringRange } from './utils/utils.js';
1414
import { Clipboard } from './utils/clipboard.js';
1515
import { FlowLauncher } from './utils/flowLauncher.js';
1616

@@ -59,25 +59,23 @@ export class ScriptManager {
5959
const QUERY_CHAR_LIMIT = 20;
6060
const ALL_SCRIPTS_CHAR = '*';
6161
const QUERIES_SEPARATOR = ' ';
62-
63-
const metaNameSort = (a: Script, b: Script): 1 | -1 =>
64-
(a.name || '') < (b.name || '') ? 1 : -1;
62+
const SORT_PROPERTY = 'name';
6563

6664
if (query.length > QUERY_CHAR_LIMIT) return [];
6765

68-
if (query === ALL_SCRIPTS_CHAR) return this.scripts.sort(metaNameSort);
66+
if (query === ALL_SCRIPTS_CHAR)
67+
return sortByProperty(this.scripts, SORT_PROPERTY);
6968

70-
const normalizeQuery = query.toLowerCase();
71-
const queries = new Set(normalizeQuery.split(QUERIES_SEPARATOR));
69+
const queries = query.split(QUERIES_SEPARATOR);
7270

7371
const results = this.scripts.filter(script =>
74-
matchesMetas(Array.from(queries), {
72+
matchesObject(queries, {
7573
name: script.name,
7674
tags: script.tags
7775
})
7876
);
7977

80-
return results.sort(metaNameSort);
78+
return sortByProperty(results, SORT_PROPERTY);
8179
}
8280

8381
runScriptM(scriptArguments: ScriptArguments) {

src/utils/utils.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,33 @@ export function stringRange(str: string, start: string, end: string): string {
2020
/**
2121
* Return match bool based on queries and metas
2222
* @param queries The multiples search term.
23-
* @param metas The searched metas.
23+
* @param object The searched datas.
2424
*/
25-
export function matchesMetas(queries: string[], metas: Metas): boolean {
25+
export function matchesObject(queries: string[], object: Object): boolean {
2626
return queries.every(query =>
27-
Object.values(metas).some(meta => meta.includes(query))
27+
Object.values(object).some(item => item.includes(query))
2828
);
2929
}
30+
31+
/**
32+
* Sorts an array of objects based on a specified property.
33+
* @template T - The type of objects in the array.
34+
* @template K - The key of the property to sort by.
35+
* @param arr - The array of objects to sort.
36+
* @param property - The property key to sort by.
37+
* @param ascending - Whether to sort in ascending order (default: true).
38+
*/
39+
export function sortByProperty<T, K extends keyof T>(
40+
arr: T[],
41+
property: K,
42+
ascending: boolean = true
43+
): T[] {
44+
return arr.sort((a, b) => {
45+
const valueA = a[property];
46+
const valueB = b[property];
47+
48+
if (valueA < valueB) return ascending ? -1 : 1;
49+
if (valueA > valueB) return ascending ? 1 : -1;
50+
return 0;
51+
});
52+
}

0 commit comments

Comments
 (0)