Skip to content

Commit

Permalink
Merge pull request #874 from pano9000/refactor_sanitizeAttributeName
Browse files Browse the repository at this point in the history
refactor(sanitizeAttributeName): simplify function and export
  • Loading branch information
eliandoran authored Jan 2, 2025
2 parents 2556d51 + d798388 commit 6a9c8ff
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
43 changes: 43 additions & 0 deletions spec-es6/sanitize_attribute_name.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion src/becca/entities/battribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {
this.validate();
}

this.name = sanitizeAttributeName.sanitizeAttributeName(this.name);
this.name = sanitizeAttributeName(this.name);

if (!this.value) {
// null value isn't allowed
Expand Down
6 changes: 3 additions & 3 deletions src/routes/api/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/consistency_checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ class ConsistencyChecks {
const attrNames = sql.getColumn<string>(`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) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/import/enex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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: ''
})
}
Expand Down
20 changes: 5 additions & 15 deletions src/services/sanitize_attribute_name.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
function sanitizeAttributeName(origName: string) {
let fixedName: string;

if (origName === '') {
fixedName = "unnamed";
}
else {
export default function sanitizeAttributeName(origName: string) {
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;
}


export default {
sanitizeAttributeName
};
}

0 comments on commit 6a9c8ff

Please sign in to comment.