diff --git a/src/index.ts b/src/index.ts index 3537c5f..af4126b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,21 +165,23 @@ namespace ScanCategorizer { Logger.log('Apply category: ' + sorted[0].name); handleMatch(file, sorted[0]); - for (let i = 1; i < sorted.length; i++) { - const category = sorted[i]; - - // Check if category allows for secondary categorization. - if (!category.allowSecondary) { - Logger.log( - `Category ${category.name} is not allowed to be a secondary category and will be skipped.`, - ); - - // Skip - continue; + if (sorted.length > 1) { + for (let i = 1; i < sorted.length; i++) { + const category = sorted[i]; + + // Check if category allows for secondary categorization. + if (!category.allowSecondary) { + Logger.log( + `Category ${category.name} is not allowed to be a secondary category and will be skipped.`, + ); + + // Skip + continue; + } + + Logger.log('Creating shortcuts for category: ' + category.name); + handleMatch(file, category, true); } - - Logger.log('Creating shortcuts for category: ' + category.name); - handleMatch(file, category, true); } Logger.log('Finished processing file: ' + file.getName()); diff --git a/src/query.ts b/src/query.ts index c74b9d6..cb36673 100644 --- a/src/query.ts +++ b/src/query.ts @@ -2,12 +2,6 @@ import {Helpers} from './helpers'; /* eslint-disable no-unused-vars */ export namespace Query { - export const enum Priority { - LOW = -1, - DEFAULT = 0, - HIGH = 1, - } - export interface Condition { type: 'and' | 'or'; values: (RegExp | string)[]; @@ -190,10 +184,10 @@ export namespace Query { export const sortMatchesByPriority = (matches: Category[]): Category[] => { return matches.sort((a: Category, b: Category) => { if (!a.priority) { - a.priority = Priority.DEFAULT; + a.priority = 0; } if (!b.priority) { - b.priority = Priority.DEFAULT; + b.priority = 0; } if (a.priority > b.priority) { return -1; diff --git a/tests/query.test.ts b/tests/query.test.ts index 77de3bf..adcb4c5 100644 --- a/tests/query.test.ts +++ b/tests/query.test.ts @@ -1,16 +1,16 @@ -import { Query } from "../src/query"; +import {Query} from '../src/query'; -describe("Query", () => { - describe("Query.and()", () => { - it("should return correct condition for Query.and()", () => { +describe('Query', () => { + describe('Query.and()', () => { + it('should return correct condition for Query.and()', () => { const tests = [ { - v: Query.and("test1", "test2"), - e: { type: "and", values: ["test1", "test2"] }, + v: Query.and('test1', 'test2'), + e: {type: 'and', values: ['test1', 'test2']}, }, { - v: Query.and("test1", /d/), - e: { type: "and", values: ["test1", /d/] }, + v: Query.and('test1', /d/), + e: {type: 'and', values: ['test1', /d/]}, }, ]; @@ -20,16 +20,16 @@ describe("Query", () => { }); }); - describe("Query.or()", () => { - it("should return correct condition for Query.or()", () => { + describe('Query.or()', () => { + it('should return correct condition for Query.or()', () => { const tests = [ { - v: Query.or("test1", "test2"), - e: { type: "or", values: ["test1", "test2"] }, + v: Query.or('test1', 'test2'), + e: {type: 'or', values: ['test1', 'test2']}, }, { - v: Query.or("test1", /d/), - e: { type: "or", values: ["test1", /d/] }, + v: Query.or('test1', /d/), + e: {type: 'or', values: ['test1', /d/]}, }, ]; @@ -39,24 +39,24 @@ describe("Query", () => { }); }); - describe("Query.cleanWords()", () => { - it("should return correct array for Query.cleanWords()", () => { + describe('Query.cleanWords()', () => { + it('should return correct array for Query.cleanWords()', () => { const tests = [ { - v: Query.cleanWords(["test1", "test2"]), - e: ["test1", "test2"], + v: Query.cleanWords(['test1', 'test2']), + e: ['test1', 'test2'], }, { - v: Query.cleanWords(["Test1", "tEst2"]), - e: ["test1", "test2"], + v: Query.cleanWords(['Test1', 'tEst2']), + e: ['test1', 'test2'], }, { - v: Query.cleanWords(["Test1 ", " tEst2", " d "]), - e: ["test1", "test2", "d"], + v: Query.cleanWords(['Test1 ', ' tEst2', ' d ']), + e: ['test1', 'test2', 'd'], }, { - v: Query.cleanWords(["Test1 ", " tEst2", ""]), - e: ["test1", "test2"], + v: Query.cleanWords(['Test1 ', ' tEst2', '']), + e: ['test1', 'test2'], }, ]; @@ -66,15 +66,15 @@ describe("Query", () => { }); }); - describe("Query.applies()", () => { - it("should return true for Query.applies() with Query.and() conditions", () => { + describe('Query.applies()', () => { + it('should return true for Query.applies() with Query.and() conditions', () => { const tests = [ - Query.applies(["test1", "test2"], Query.and("test1", "test2")), - Query.applies(["test1", "test2", "test3"], Query.and("test1", "test2")), - Query.applies(["test1", "test2", "test3"], Query.and("test1")), + Query.applies(['test1', 'test2'], Query.and('test1', 'test2')), + Query.applies(['test1', 'test2', 'test3'], Query.and('test1', 'test2')), + Query.applies(['test1', 'test2', 'test3'], Query.and('test1')), Query.applies( - ["test1", "test2", "test3"], - Query.and("test1", /^test1/) + ['test1', 'test2', 'test3'], + Query.and('test1', /^test1/), ), ]; @@ -83,13 +83,13 @@ describe("Query", () => { }); }); - it("should return true for Query.applies() with Query.or() conditions", () => { + it('should return true for Query.applies() with Query.or() conditions', () => { const tests = [ - Query.applies(["test1", "test2"], Query.or("test1", "test2")), - Query.applies(["test1", "test3", "test3"], Query.or("test1", "test4")), - Query.applies(["test1", "test2", "test3"], Query.or("test2")), - Query.applies(["test1", "test2", "test3"], Query.or(/^test1/)), - Query.applies(["test1", "test2", "test3"], Query.or(/test2/, /^test1/)), + Query.applies(['test1', 'test2'], Query.or('test1', 'test2')), + Query.applies(['test1', 'test3', 'test3'], Query.or('test1', 'test4')), + Query.applies(['test1', 'test2', 'test3'], Query.or('test2')), + Query.applies(['test1', 'test2', 'test3'], Query.or(/^test1/)), + Query.applies(['test1', 'test2', 'test3'], Query.or(/test2/, /^test1/)), ]; tests.forEach((test) => { @@ -97,17 +97,17 @@ describe("Query", () => { }); }); - it("should return false for Query.applies() with Query.and() conditions", () => { + it('should return false for Query.applies() with Query.and() conditions', () => { const tests = [ - Query.applies(["test1", "test2"], Query.and("test1", "test3")), + Query.applies(['test1', 'test2'], Query.and('test1', 'test3')), Query.applies( - ["test1", "test2", "test3"], - Query.and("test1", "test2", "test4") + ['test1', 'test2', 'test3'], + Query.and('test1', 'test2', 'test4'), ), - Query.applies(["test1", "test2", "test3"], Query.and("test4")), + Query.applies(['test1', 'test2', 'test3'], Query.and('test4')), Query.applies( - ["test1", "test2", "test3"], - Query.and("test1", /^test2/) + ['test1', 'test2', 'test3'], + Query.and('test1', /^test2/), ), ]; @@ -116,15 +116,15 @@ describe("Query", () => { }); }); - it("should return false for Query.applies() with Query.or() conditions", () => { + it('should return false for Query.applies() with Query.or() conditions', () => { const tests = [ - Query.applies(["test1", "test2"], Query.or("test3", "test4")), - Query.applies(["test1", "test3", "test3"], Query.or("test4", "test5")), - Query.applies(["test1", "test2", "test3"], Query.or("test4")), - Query.applies(["test1", "test2", "test3"], Query.or(/^test2/)), + Query.applies(['test1', 'test2'], Query.or('test3', 'test4')), + Query.applies(['test1', 'test3', 'test3'], Query.or('test4', 'test5')), + Query.applies(['test1', 'test2', 'test3'], Query.or('test4')), + Query.applies(['test1', 'test2', 'test3'], Query.or(/^test2/)), Query.applies( - ["test1", "test2", "test3"], - Query.or(/^test2/, /test1$/) + ['test1', 'test2', 'test3'], + Query.or(/^test2/, /test1$/), ), ]; @@ -134,118 +134,118 @@ describe("Query", () => { }); }); - describe("Query.sortMatchesByPriority()", () => { - it("should return correct order for Query.sortMatchesByPriority()", () => { - const tests: { v: Query.Category[]; e: Query.Category[] }[] = [ + describe('Query.sortMatchesByPriority()', () => { + it('should return correct order for Query.sortMatchesByPriority()', () => { + const tests: {v: Query.Category[]; e: Query.Category[]}[] = [ { v: Query.sortMatchesByPriority([ - { name: "test1", conditions: [], path: "" }, - { name: "test2", conditions: [], path: "" }, - { name: "test3", conditions: [], path: "" }, - { name: "test4", conditions: [], path: "" }, + {name: 'test1', conditions: [], path: ''}, + {name: 'test2', conditions: [], path: ''}, + {name: 'test3', conditions: [], path: ''}, + {name: 'test4', conditions: [], path: ''}, ]), e: [ { - name: "test1", + name: 'test1', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test2", + name: 'test2', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test3", + name: 'test3', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test4", + name: 'test4', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, ], }, { v: Query.sortMatchesByPriority([ - { name: "test1", conditions: [], path: "" }, - { name: "test2", conditions: [], path: "" }, - { name: "test3", conditions: [], path: "" }, + {name: 'test1', conditions: [], path: ''}, + {name: 'test2', conditions: [], path: ''}, + {name: 'test3', conditions: [], path: ''}, { - name: "test4", + name: 'test4', conditions: [], - path: "", - priority: Query.Priority.HIGH, + path: '', + priority: 1, }, ]), e: [ { - name: "test4", + name: 'test4', conditions: [], - path: "", - priority: Query.Priority.HIGH, + path: '', + priority: 1, }, { - name: "test1", + name: 'test1', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test2", + name: 'test2', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test3", + name: 'test3', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, ], }, { v: Query.sortMatchesByPriority([ - { name: "test1", conditions: [], path: "" }, + {name: 'test1', conditions: [], path: ''}, { - name: "test2", + name: 'test2', conditions: [], - path: "", - priority: Query.Priority.LOW, + path: '', + priority: -1, }, - { name: "test3", conditions: [], path: "" }, - { name: "test4", conditions: [], path: "" }, + {name: 'test3', conditions: [], path: ''}, + {name: 'test4', conditions: [], path: ''}, ]), e: [ { - name: "test1", + name: 'test1', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test3", + name: 'test3', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test4", + name: 'test4', conditions: [], - path: "", - priority: Query.Priority.DEFAULT, + path: '', + priority: 0, }, { - name: "test2", + name: 'test2', conditions: [], - path: "", - priority: Query.Priority.LOW, + path: '', + priority: -1, }, ], }, @@ -257,30 +257,30 @@ describe("Query", () => { }); }); - describe("Query.classify()", () => { - it("should return correct array of matches for Query.classify()", () => { + describe('Query.classify()', () => { + it('should return correct array of matches for Query.classify()', () => { const tests = [ { - v: Query.classify(["test1", "test2"], []), + v: Query.classify(['test1', 'test2'], []), e: [], }, { v: Query.classify( - ["test1", "test2"], - [{ name: "test1", conditions: [Query.or("test1")], path: "" }] + ['test1', 'test2'], + [{name: 'test1', conditions: [Query.or('test1')], path: ''}], ), - e: [{ name: "test1", conditions: [Query.or("test1")], path: "" }], + e: [{name: 'test1', conditions: [Query.or('test1')], path: ''}], }, { v: Query.classify( - ["test1", "test2"], + ['test1', 'test2'], [ { - name: "test1", - conditions: [Query.and("test1", "test3")], - path: "", + name: 'test1', + conditions: [Query.and('test1', 'test3')], + path: '', }, - ] + ], ), e: [], },