From afb91f82e1c41e1804dd3ecfb38d6631f882a6ad Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Thu, 2 Jan 2025 16:26:23 +0100 Subject: [PATCH 1/3] refactor(sanitizeAttributeNames): directly export function no need to wrap the exported function in an object first --- src/becca/entities/battribute.ts | 2 +- src/routes/api/sender.ts | 6 +++--- src/services/consistency_checks.ts | 2 +- src/services/import/enex.ts | 4 ++-- src/services/sanitize_attribute_name.ts | 9 ++------- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/becca/entities/battribute.ts b/src/becca/entities/battribute.ts index 97df7b056..66ef83ca8 100644 --- a/src/becca/entities/battribute.ts +++ b/src/becca/entities/battribute.ts @@ -174,7 +174,7 @@ class BAttribute extends AbstractBeccaEntity { this.validate(); } - this.name = sanitizeAttributeName.sanitizeAttributeName(this.name); + this.name = sanitizeAttributeName(this.name); if (!this.value) { // null value isn't allowed diff --git a/src/routes/api/sender.ts b/src/routes/api/sender.ts index 22eea766d..705cac25e 100644 --- a/src/routes/api/sender.ts +++ b/src/routes/api/sender.ts @@ -3,7 +3,7 @@ import imageType from "image-type"; import imageService from "../../services/image.js"; import noteService from "../../services/notes.js"; -import sanitize_attribute_name from "../../services/sanitize_attribute_name.js"; +import sanitizeAttributeName from "../../services/sanitize_attribute_name.js"; import specialNotesService from "../../services/special_notes.js"; import { Request } from 'express'; @@ -44,7 +44,7 @@ async function uploadImage(req: Request) { const labels = JSON.parse(labelsStr); for (const { name, value } of labels) { - note.setLabel(sanitize_attribute_name.sanitizeAttributeName(name), value); + note.setLabel(sanitizeAttributeName(name), value); } } @@ -73,7 +73,7 @@ function saveNote(req: Request) { if (req.body.labels) { for (const { name, value } of req.body.labels) { - note.setLabel(sanitize_attribute_name.sanitizeAttributeName(name), value); + note.setLabel(sanitizeAttributeName(name), value); } } diff --git a/src/services/consistency_checks.ts b/src/services/consistency_checks.ts index fe37c7c15..fa1cec21d 100644 --- a/src/services/consistency_checks.ts +++ b/src/services/consistency_checks.ts @@ -754,7 +754,7 @@ class ConsistencyChecks { const attrNames = sql.getColumn(`SELECT DISTINCT name FROM attributes`); for (const origName of attrNames) { - const fixedName = sanitizeAttributeName.sanitizeAttributeName(origName); + const fixedName = sanitizeAttributeName(origName); if (fixedName !== origName) { if (this.autoFix) { diff --git a/src/services/import/enex.ts b/src/services/import/enex.ts index 071b66cdb..f0805b2fc 100644 --- a/src/services/import/enex.ts +++ b/src/services/import/enex.ts @@ -151,7 +151,7 @@ function importEnex(taskContext: TaskContext, file: File, parentNote: BNote): Pr labelName = 'pageUrl'; } - labelName = sanitizeAttributeName.sanitizeAttributeName(labelName || ""); + labelName = sanitizeAttributeName(labelName || ""); if (note.attributes) { note.attributes.push({ @@ -202,7 +202,7 @@ function importEnex(taskContext: TaskContext, file: File, parentNote: BNote): Pr } else if (currentTag === 'tag' && note.attributes) { note.attributes.push({ type: 'label', - name: sanitizeAttributeName.sanitizeAttributeName(text), + name: sanitizeAttributeName(text), value: '' }) } diff --git a/src/services/sanitize_attribute_name.ts b/src/services/sanitize_attribute_name.ts index 635588f80..075ccfd03 100644 --- a/src/services/sanitize_attribute_name.ts +++ b/src/services/sanitize_attribute_name.ts @@ -1,4 +1,4 @@ -function sanitizeAttributeName(origName: string) { +export default function sanitizeAttributeName(origName: string) { let fixedName: string; if (origName === '') { @@ -10,9 +10,4 @@ function sanitizeAttributeName(origName: string) { } return fixedName; -} - - -export default { - sanitizeAttributeName -}; +} \ No newline at end of file From 1053da3e405b1a81c3a64e2fcaa60c091e15038b Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Thu, 2 Jan 2025 16:38:51 +0100 Subject: [PATCH 2/3] refactor(sanitizeAttributeNames): use a ternary operator --- src/services/sanitize_attribute_name.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/services/sanitize_attribute_name.ts b/src/services/sanitize_attribute_name.ts index 075ccfd03..b1f0e6579 100644 --- a/src/services/sanitize_attribute_name.ts +++ b/src/services/sanitize_attribute_name.ts @@ -1,13 +1,8 @@ export default function sanitizeAttributeName(origName: string) { - let fixedName: string; - - if (origName === '') { - fixedName = "unnamed"; - } - else { + const fixedName = (origName === '') + ? "unnamed" + : origName.replace(/[^\p{L}\p{N}_:]/ug, "_"); // any not allowed character should be replaced with underscore - fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_"); - } return fixedName; } \ No newline at end of file From d798388026798ab99211da9eabe86c7a7f7facb4 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Thu, 2 Jan 2025 18:16:13 +0100 Subject: [PATCH 3/3] test(sanitizeAttributeNames): add basic test --- spec-es6/sanitize_attribute_name.spec.ts | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec-es6/sanitize_attribute_name.spec.ts diff --git a/spec-es6/sanitize_attribute_name.spec.ts b/spec-es6/sanitize_attribute_name.spec.ts new file mode 100644 index 000000000..fa01a4690 --- /dev/null +++ b/spec-es6/sanitize_attribute_name.spec.ts @@ -0,0 +1,43 @@ +import sanitizeAttributeName from "../src/services/sanitize_attribute_name" +import { describe, it, execute, expect } from "./mini_test"; + +// fn value, expected value +const testCases: [fnValue: string, expectedValue: string][] = [ + ["testName", "testName"], + ["test_name", "test_name"], + ["test with space", "test_with_space"], + ["test:with:colon", "test:with:colon"], + + // numbers + ["123456", "123456"], + ["123:456", "123:456"], + ["123456 abc", "123456_abc"], + + // non-latin characters + ["ε", "ε"], + ["attribute ε", "attribute_ε"], + + + // special characters + ["test/name", "test_name"], + ["test%name", "test_name"], + ["\/", "_"], + + // empty string + ["", "unnamed"], +] + + + +describe("sanitizeAttributeName unit tests", () => { + + testCases.forEach(testCase => { + return it(`'${testCase[0]}' should return '${testCase[1]}'`, () => { + const [value, expected] = testCase; + const actual = sanitizeAttributeName(value); + expect(actual).toEqual(expected); + }) + }) +}) + +execute() \ No newline at end of file