Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkxin committed Aug 25, 2024
1 parent 2c6ef8a commit d94433d
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 26 deletions.
28 changes: 3 additions & 25 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Markdown from './lib/markdown.js';
import { Tab, TabGroup, TabListGrouper } from './lib/tabs.js';
import { Bookmarks } from './bookmarks.js';
import CustomFormatsStorage from './storage/custom-formats-storage.js';
import CustomFormat from './lib/custom-format.js';

const COLOR_GREEN = '#738a05';
const COLOR_RED = '#d11b24';
Expand Down Expand Up @@ -229,31 +230,8 @@ function renderBuiltInFormat(format, tabLists, listType) {
*/
async function renderCustomFormat({ slot, lists }) {
const customFormat = await CustomFormatsStorage.get(slot);

const links = lists
.map((list) => list.tabs.map((tab) => ({
title: tab.title,
url: tab.url,
})))
.flat()
.map((item, idx) => ({
...item,
number: idx + 1,
}));

const groups = lists
.map((list, idx) => ({
name: list.name,
is_ungrouped: list.isNonGroup(),
number: idx + 1,
links: list.tabs.map((tab, jdx) => ({
title: tab.title,
url: tab.url,
number: jdx + 1,
})),
}));

return customFormat.render({ links, groups });
const input = CustomFormat.makeRenderInput(lists);
return customFormat.render(input);
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/lib/custom-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ import Mustache from '../vendor/mustache.mjs';
// disable HTML escape
Mustache.escape = function (text) { return text; };

Check warning on line 4 in src/lib/custom-format.js

View workflow job for this annotation

GitHub Actions / build

Unexpected unnamed function

/**
* @typedef {Object} RenderInputLink
* @prop {String} title
* @prop {String} url
* @prop {Number} number
*/

/**
* @typedef {Object} RenderInputGroup
* @prop {String} name
* @prop {Boolean} is_ungrouped
* @prop {Number} number
* @prop {RenderInputLink[]} links
*/

/**
* @typedef {Object} RenderInput
* @prop {RenderInputLink[]} links
* @prop {RenderInputGroup[]} groups
*/

export default class CustomFormat {
/**
* @param slot {string}
Expand All @@ -25,4 +46,38 @@ export default class CustomFormat {
render(input) {
return Mustache.render(this.template, input);
}

/**
*
* @param lists {TabList[]}
* @returns {RenderInput}
*/
static makeRenderInput(lists) {
/** @type {RenderInputLink[]} */
const links = lists
.map((list) => list.tabs.map((tab) => ({
title: tab.title,
url: tab.url,
})))
.flat()
.map((item, idx) => ({
...item,
number: idx + 1,
}));

/** @type {RenderInputGroup[]} */
const groups = lists
.map((list, idx) => ({
name: list.name,
is_ungrouped: list.isNonGroup(),
number: idx + 1,
links: list.tabs.map((tab, jdx) => ({
title: tab.title,
url: tab.url,
number: jdx + 1,
})),
}));

return { links, groups };
}
}
2 changes: 1 addition & 1 deletion src/lib/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class TabGroup {
}
}

TabGroup.NonGroupId = (Object.hasOwn(chrome, 'tabGroups')) ? chrome.tabGroups.TAB_GROUP_ID_NONE : -1;
TabGroup.NonGroupId = -1;

export class TabList {
/**
Expand Down
93 changes: 93 additions & 0 deletions test/custom-format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, test } from 'node:test';
import * as assert from 'node:assert';
import { Tab, TabList } from '../src/lib/tabs.js';
import CustomFormat from '../src/lib/custom-format.js';

describe('custom-format.js', () => {
describe('makeRenderInput', () => {
test('ungrouped', () => {
/** @type TabList[] */
const lists = [
new TabList('', -1, [
new Tab('Example 1', 'https://example.com/1', -1),
new Tab('Example 2', 'https://example.com/2', -1),
new Tab('Example 3', 'https://example.com/3', -1),
]),
];

const actual = CustomFormat.makeRenderInput(lists);
assert.deepEqual(actual, {
links: [
{ title: 'Example 1', url: 'https://example.com/1', number: 1 },
{ title: 'Example 2', url: 'https://example.com/2', number: 2 },
{ title: 'Example 3', url: 'https://example.com/3', number: 3 },
],
groups: [
{
name: '',
is_ungrouped: true,
number: 1,
links: [
{ title: 'Example 1', url: 'https://example.com/1', number: 1 },
{ title: 'Example 2', url: 'https://example.com/2', number: 2 },
{ title: 'Example 3', url: 'https://example.com/3', number: 3 },
],
},
],
});
});

test('one group', () => {
/** @type TabList[] */
const lists = [
new TabList('', -1, [
new Tab('Example 1', 'https://example.com/1', -1),
]),
new TabList('My Group', 42, [
new Tab('Example 2', 'https://example.com/2', 42),
new Tab('Example 3', 'https://example.com/3', 42),
]),
new TabList('', -1, [
new Tab('Example 4', 'https://example.com/4', -1),
]),
];

const actual = CustomFormat.makeRenderInput(lists);
assert.deepEqual(actual, {
links: [
{ title: 'Example 1', url: 'https://example.com/1', number: 1 },
{ title: 'Example 2', url: 'https://example.com/2', number: 2 },
{ title: 'Example 3', url: 'https://example.com/3', number: 3 },
{ title: 'Example 4', url: 'https://example.com/4', number: 4 },
],
groups: [
{
name: '',
is_ungrouped: true,
number: 1,
links: [
{ title: 'Example 1', url: 'https://example.com/1', number: 1 },
],
},
{
name: 'My Group',
is_ungrouped: false,
number: 2,
links: [
{ title: 'Example 2', url: 'https://example.com/2', number: 1 },
{ title: 'Example 3', url: 'https://example.com/3', number: 2 },
],
},
{
name: '',
is_ungrouped: true,
number: 3,
links: [
{ title: 'Example 4', url: 'https://example.com/4', number: 1 },
],
},
],
});
});
});
});

0 comments on commit d94433d

Please sign in to comment.