Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ const STORAGE_KEYS = {

/**
* Normalizes input entries to ensure consistent comparison (lowercase & trimmed).
* Also strips zero-width characters and collapses multiple consecutive whitespace.
* @param {string} val - Raw string input.
* @returns {string} Normalized string.
*/
function normalizeEntry(val) {
if (typeof val !== 'string') return '';
return val.trim().toLowerCase();
return val
.replace(/[\u200B-\u200D\uFEFF]/g, '')
.toLowerCase()
.replace(/\s+/g, ' ')
.trim();
}

/**
Expand Down Expand Up @@ -186,3 +191,13 @@ function removeBlockedItem(type, value) {
});
});
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = {
STORAGE_KEYS,
normalizeEntry,
getBlockData,
addBlockedItem,
removeBlockedItem
};
}
42 changes: 42 additions & 0 deletions test/storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { describe, it } = require('node:test');
const assert = require('node:assert');
const { normalizeEntry } = require('../storage.js');

describe('normalizeEntry', () => {
it('should return empty string for non-string inputs', () => {
assert.strictEqual(normalizeEntry(null), '');
assert.strictEqual(normalizeEntry(undefined), '');
assert.strictEqual(normalizeEntry(123), '');
assert.strictEqual(normalizeEntry({}), '');
assert.strictEqual(normalizeEntry(['artist']), '');
assert.strictEqual(normalizeEntry(true), '');
assert.strictEqual(normalizeEntry(false), '');
});

it('should return empty string for empty string input', () => {
assert.strictEqual(normalizeEntry(''), '');
assert.strictEqual(normalizeEntry(' '), '');
});

it('should trim leading and trailing spaces and lowercase string', () => {
assert.strictEqual(normalizeEntry(' Taylor Swift '), 'taylor swift');
assert.strictEqual(normalizeEntry('DRAKE'), 'drake');
assert.strictEqual(normalizeEntry(' The Beatles'), 'the beatles');
});

it('should collapse multiple inner whitespaces into a single space', () => {
assert.strictEqual(normalizeEntry('Radiohead - Karma Police'), 'radiohead - karma police');
assert.strictEqual(normalizeEntry('Coldplay\t\n Yellow'), 'coldplay yellow');
});

it('should strip zero-width spaces and zero-width joiners/non-joiners/BOM', () => {
// \u200B (zero-width space), \u200C (zero-width non-joiner), \u200D (zero-width joiner), \uFEFF (zero-width no-break space / BOM)
const stringWithZeroWidth = 'Artist\u200BName\u200CWith\u200DHidden\uFEFFChars';
assert.strictEqual(normalizeEntry(stringWithZeroWidth), 'artistnamewithhiddenchars');
});

it('should correctly normalize complex inputs combining casing, spaces, and zero-width characters', () => {
const complexInput = ' \u200B Kanye \u200D WEST \uFEFF ';
assert.strictEqual(normalizeEntry(complexInput), 'kanye west');
});
});
Loading