Skip to content

Commit

Permalink
Merge pull request #873 from pano9000/refactor_use_Set
Browse files Browse the repository at this point in the history
refactor(services): use Set instead of Arrays for faster lookups
  • Loading branch information
eliandoran authored Jan 2, 2025
2 parents d07aa09 + baea3bd commit 2556d51
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/services/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import BUILTIN_ATTRIBUTES from "./builtin_attributes.js";
import BNote from "../becca/entities/bnote.js";
import { AttributeRow } from '../becca/entities/rows.js';

const ATTRIBUTE_TYPES = ['label', 'relation'];
const ATTRIBUTE_TYPES = new Set(['label', 'relation']);

function getNotesWithLabel(name: string, value?: string): BNote[] {
const query = attributeFormatter.formatAttrForSearch({type: 'label', name, value}, value !== undefined);
Expand Down Expand Up @@ -99,7 +99,7 @@ function getAttributeNames(type: string, nameLike: string) {
}

function isAttributeType(type: string): boolean {
return ATTRIBUTE_TYPES.includes(type);
return ATTRIBUTE_TYPES.has(type);
}

function isAttributeDangerous(type: string, name: string): boolean {
Expand Down
6 changes: 3 additions & 3 deletions src/services/search/expressions/note_content_fulltext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { normalize } from "../../utils.js";
import sql from "../../sql.js";


const ALLOWED_OPERATORS = ['=', '!=', '*=*', '*=', '=*', '%='];
const ALLOWED_OPERATORS = new Set(['=', '!=', '*=*', '*=', '=*', '%=']);

const cachedRegexes: Record<string, RegExp> = {};

Expand Down Expand Up @@ -50,8 +50,8 @@ class NoteContentFulltextExp extends Expression {
}

execute(inputNoteSet: NoteSet, executionContext: {}, searchContext: SearchContext) {
if (!ALLOWED_OPERATORS.includes(this.operator)) {
searchContext.addError(`Note content can be searched only with operators: ${ALLOWED_OPERATORS.join(", ")}, operator ${this.operator} given.`);
if (!ALLOWED_OPERATORS.has(this.operator)) {
searchContext.addError(`Note content can be searched only with operators: ${Array.from(ALLOWED_OPERATORS).join(", ")}, operator ${this.operator} given.`);

return inputNoteSet;
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/search/services/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function getFulltext(_tokens: TokenData[], searchContext: SearchContext) {
}
}

const OPERATORS = [
const OPERATORS = new Set([
"=",
"!=",
"*=*",
Expand All @@ -55,14 +55,14 @@ const OPERATORS = [
"<",
"<=",
"%="
];
]);

function isOperator(token: TokenData) {
if (Array.isArray(token)) {
return false;
}

return OPERATORS.includes(token.token);
return OPERATORS.has(token.token);
}

function getExpression(tokens: TokenData[], searchContext: SearchContext, level = 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,19 @@ export function getContentDisposition(filename: string) {
return `file; filename="${sanitizedFilename}"; filename*=UTF-8''${sanitizedFilename}`;
}

const STRING_MIME_TYPES = [
const STRING_MIME_TYPES = new Set([
"application/javascript",
"application/x-javascript",
"application/json",
"application/x-sql",
"image/svg+xml"
];
]);

export function isStringNote(type: string | undefined, mime: string) {
// render and book are string note in the sense that they are expected to contain empty string
return (type && ["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas"].includes(type))
|| mime.startsWith('text/')
|| STRING_MIME_TYPES.includes(mime);
|| STRING_MIME_TYPES.has(mime);
}

export function quoteRegex(url: string) {
Expand Down

0 comments on commit 2556d51

Please sign in to comment.