Skip to content

Commit

Permalink
Merge pull request #103 from DaniloNovakovic/master-dn/features/impro…
Browse files Browse the repository at this point in the history
…ve-typescript
  • Loading branch information
DaniloNovakovic authored May 7, 2023
2 parents bbc4176 + 703d85a commit 6f225e0
Show file tree
Hide file tree
Showing 47 changed files with 350 additions and 135 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "chrome-dynamic-bookmarks",
"version": "3.1.1",
"version": "3.1.2",
"description": "Chrome extension which dynamically updates bookmarks based on the specified regular expression.",
"pre-commit": [
"format:prettier"
],
"scripts": {
"start": "webpack-dev-server --open --env.env=dev",
"watch": "webpack --watch --env.env=dev",
Expand All @@ -10,6 +13,7 @@
"build": "cross-env NODE_ENV=production webpack -p --env.env=prod",
"build:analyze": "cross-env NODE_ENV=production webpack -p --env.env=prod --env.addons=bundleanalyzer",
"clean": "rimraf build",
"check:types": "tsc",
"reset": "rimraf node_modules",
"test": "jest",
"lint": "eslint --ext .jsx,.js,.ts,.tsx ./src",
Expand Down Expand Up @@ -87,6 +91,7 @@
"mini-css-extract-plugin": "^0.8.2",
"npm-run-all": "^4.1.5",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"pre-commit": "^1.2.2",
"prettier": "2.3.2",
"pretty-quick": "^3.1.1",
"prop-types": "^15.7.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
folderActionMenuId,
addBookmarkNodeActionMenuId,
selectedNodesActionMenuId,
};
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ export default {
REMOVE_SELECTED,
CLEAR_SELECTED,
TOGGLE_SELECTED,
};
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export const CUT = "CUT";
export default {
COPIED,
CUT,
};
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
addFolderDialogId,
editBookmarkDialogId,
editFolderDialogId,
};
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
BM_NODE_CREATED,
BM_NODE_MOVED,
BM_NODE_REMOVED,
};
} as const;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export default {
EDIT_BM_NODE,
MOVE_BM_NODE,
REMOVE_BM_NODE,
};
} as const;
4 changes: 1 addition & 3 deletions src/shared/constants/responseTypes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { MessageResponseType } from "@/shared/types";

export const SUCCESS = "success";
export const ERROR = "error";

export default {
SUCCESS,
ERROR,
} as Record<string, MessageResponseType>;
} as const;
22 changes: 0 additions & 22 deletions src/shared/lib/Queue.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Queue", () => {
beforeEach(() => {
queue = new Queue();
queue.enqueue(item);
queue.dequeue(item);
queue.dequeue();
});
describe("dequeue", () => {
it("returns undefined", () => {
Expand Down
23 changes: 23 additions & 0 deletions src/shared/lib/Queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Denque from "denque";

export default class Queue<T = unknown> {
_denque: Denque<T>;
constructor(array: T[] = []) {
this._denque = new Denque<T>(array);
}
enqueue(item: T) {
this._denque.unshift(item);
}
dequeue(): T {
return this._denque.pop();
}
clear(): void {
this._denque.clear();
}
isEmpty(): boolean {
return this._denque.isEmpty();
}
toArray(): T[] {
return this._denque.toArray();
}
}
2 changes: 0 additions & 2 deletions src/shared/lib/array/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/shared/lib/array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as indexOfOrDefault } from "./indexOfOrDefault";
export { default as sliceRange } from "./sliceRange";
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*
* _If `item` is not found then it will returns `defaultValue`_
*/
export default function indexOfOrDefault(
array = [],
item = null,
export default function indexOfOrDefault<TValue>(
array: TValue[] = [],
item: TValue = null,
defaultValue = -1
) {
): number {
let index = array.indexOf(item);
if (index < 0) {
index = defaultValue;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
*
* _If `fromIndex > toIndex` then it will slice by `[toIndex, fromIndex]`_
*/
export default function sliceRange(array = [], fromIndex = 0, toIndex = 0) {
export default function sliceRange<TValue>(
array: TValue[] = [],
fromIndex = 0,
toIndex = 0
) {
return fromIndex < toIndex
? array.slice(fromIndex, toIndex + 1)
: array.slice(toIndex, fromIndex + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import { checkAndHandleError } from "@/shared/lib/browser/log";
import { BookmarkTreeNode } from "@/shared/types/bookmark.types";

import getCurrentBrowser from "../getCurrentBrowser";

const browser = getCurrentBrowser();
const bookmarks = browser.bookmarks;

export default {
create,
createAsync,
get,
getSubTree,
getTreeRoot,
getChildren,
search,
move,
update,
remove,
removeTree,
};

/**
* Creates a bookmark or folder under the specified parentId.
* If url is `NULL` or missing, it will be a folder
* @param {object} bookmark - `{title:string, url:string, parentId:string?, index:integer?}`
* @param {function} done - callback function called with `done(errMsg, newBookmark)`
* If url is missing it will be a folder
*/
export function create({ title, url, parentId = null, index = null }, done) {
export function create(
{
title,
url,
parentId = undefined,
index = undefined,
}: chrome.bookmarks.BookmarkCreateArg,
done: (errMsg: string, newBookmark?: BookmarkTreeNode) => void
) {
bookmarks.create({ title, url, parentId, index }, (newBookmark) => {
if (!checkAndHandleError(done)) {
done(null, newBookmark);
Expand All @@ -36,18 +29,21 @@ export function create({ title, url, parentId = null, index = null }, done) {
/**
* Creates a bookmark or folder under the specified parentId.
* If url is `NULL` or missing, it will be a folder
* @returns {Promise} Promise object represents the newly created bookmark / folder
* @returns Promise object represents the newly created bookmark / folder
*/
export function createAsync(bookmarkNode) {
return new Promise(function (resolve, reject) {
export function createAsync(bookmarkNode: chrome.bookmarks.BookmarkCreateArg) {
return new Promise<BookmarkTreeNode>(function (resolve, reject) {
create(bookmarkNode, function (err, data) {
if (err) reject(err);
else resolve(data);
});
});
}

export function get(id, done) {
export function get(
id: string,
done: (errMsg: string, node?: BookmarkTreeNode) => void
) {
bookmarks.get(id, (results) => {
if (!checkAndHandleError(done)) {
const node = results[0];
Expand All @@ -58,9 +54,11 @@ export function get(id, done) {

/**
* Retrieves part of the Bookmarks hierarchy, starting at the specified node.
* @param {function} done - callback function called with `done(errMsg, subTreeRoot)`
*/
export function getSubTree(id, done) {
export function getSubTree(
id: string,
done: (err: string, rootNode?: BookmarkTreeNode) => void
) {
bookmarks.getSubTree(id, (results) => {
if (!checkAndHandleError(done)) {
const rootNode = results[0];
Expand All @@ -71,9 +69,10 @@ export function getSubTree(id, done) {

/**
* Retrives the root node of the bookmarks
* @param {function} done - callback function called with `done(errMsg, treeRoot)`
*/
export function getTreeRoot(done) {
export function getTreeRoot(
done: (err: string, rootNode?: BookmarkTreeNode) => void
) {
bookmarks.getTree((results) => {
if (!checkAndHandleError(done)) {
const rootNode = results[0];
Expand All @@ -84,10 +83,11 @@ export function getTreeRoot(done) {

/**
* Retrieves the children of the specified BookmarkTreeNode `id`.
* @param {string} id
* @param {function} done - callback called with `done([bookmarks])`
*/
export function getChildren(id, done) {
export function getChildren(
id: string,
done: (children?: BookmarkTreeNode[]) => void
) {
bookmarks.getChildren(id, (results) => {
if (checkAndHandleError()) {
done([]);
Expand All @@ -99,11 +99,14 @@ export function getChildren(id, done) {

/**
* Searches for BookmarkTreeNodes matching the given query.
* @param {string} query - string of words and quoted phrases that are matched against bookmark URLs and titles
* @param {callback} done - callback function called with `done([bookmarks])`
* @param query - string of words and quoted phrases that are matched against bookmark URLs and titles
* @param done - callback function called with `done([bookmarks])`
*/
export function search(query, done) {
bookmarks.search(query || {}, (results) => {
export function search(
query: string | chrome.bookmarks.BookmarkSearchQuery,
done: (results?: BookmarkTreeNode[]) => void
) {
bookmarks.search(query || ({} as unknown), (results) => {
if (checkAndHandleError()) {
done([]);
} else {
Expand All @@ -112,34 +115,56 @@ export function search(query, done) {
});
}

export function move(id, { parentId, index }, done) {
export function move(
id: string,
{ parentId, index }: chrome.bookmarks.BookmarkDestinationArg,
done: (errMsg: string, node?: BookmarkTreeNode) => void
) {
bookmarks.move(id, { parentId, index }, (result) => {
if (!checkAndHandleError(done)) {
done(null, result);
}
});
}

export function update(id, { title, url }, done) {
bookmarks.update(id, { title, url }, (updatedNode) => {
export function update(
id: string,
changes: chrome.bookmarks.BookmarkChangesArg,
done: (errMsg: string, node?: BookmarkTreeNode) => void
) {
bookmarks.update(id, changes, (updatedNode) => {
if (!checkAndHandleError(done)) {
done(null, updatedNode);
}
});
}

export function remove(id, done) {
export function remove(id: string, done: (errMsg: string) => void) {
bookmarks.remove(id, () => {
if (!checkAndHandleError(done)) {
done(null);
}
});
}

export function removeTree(id, done) {
export function removeTree(id: string, done: (errMsg: string) => void) {
bookmarks.removeTree(id, () => {
if (!checkAndHandleError(done)) {
done(null);
}
});
}

export default {
create,
createAsync,
get,
getSubTree,
getTreeRoot,
getChildren,
search,
move,
update,
remove,
removeTree,
};
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function copyBookmarkAsync(node) {
async function copyFolderAsync(fromFolder) {
const createdFolder = await bookmarks.createAsync(fromFolder);
const createdChildren = [];
for (let fromChild of fromFolder.children) {
for (const fromChild of fromFolder.children) {
const promise = copyNodeAsync({ ...fromChild, parentId: createdFolder.id });
createdChildren.push(promise);
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
function isFile(node) {
import { BookmarkTreeNode, NormalizedBookmarkTreeNode } from "@/shared/types";

function isFile(node: BookmarkTreeNode) {
return !!node.url;
}

/**
* Normalizes bookmark tree to form `{<id>: {...node, children?:[<id>]}}`
* @param {node} treeRoot - root of the bookmark tree to normalize
*/
export default function normalizeBookmarkTree(treeRoot) {
export default function normalizeBookmarkTree(
treeRoot: BookmarkTreeNode
): Record<string, NormalizedBookmarkTreeNode> {
const normalized = {};

(function traverseTree(node) {
if (isFile(node)) {
normalized[node.id] = node;
return;
}
let childIds = [];
for (let child of node.children) {
const childIds = [];
for (const child of node.children) {
traverseTree(child);
childIds.push(child.id);
}
Expand Down
Loading

0 comments on commit 6f225e0

Please sign in to comment.