mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 02:52:27 +08:00
refactor(services): use Set instead of Arrays for faster lookups
This commit is contained in:
parent
6fe4027a75
commit
baea3bdcdd
@ -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);
|
||||
@ -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 {
|
||||
|
@ -13,7 +13,7 @@ import utils from "../../utils.js";
|
||||
import sql from "../../sql.js";
|
||||
|
||||
|
||||
const ALLOWED_OPERATORS = ['=', '!=', '*=*', '*=', '=*', '%='];
|
||||
const ALLOWED_OPERATORS = new Set(['=', '!=', '*=*', '*=', '=*', '%=']);
|
||||
|
||||
const cachedRegexes: Record<string, RegExp> = {};
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ function getFulltext(_tokens: TokenData[], searchContext: SearchContext) {
|
||||
}
|
||||
}
|
||||
|
||||
const OPERATORS = [
|
||||
const OPERATORS = new Set([
|
||||
"=",
|
||||
"!=",
|
||||
"*=*",
|
||||
@ -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) {
|
||||
|
@ -152,19 +152,19 @@ 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"
|
||||
];
|
||||
]);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function quoteRegex(url: string) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user