Skip to content

Commit

Permalink
chore: add jsdoc comments on util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay Dineshkumar Shah authored and Tanmay Dineshkumar Shah committed Dec 5, 2023
1 parent 6cc7381 commit a01cd52
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/components/option/Option.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
// reset search query every-time shortcut list changes
$: resetSearchQuery($shortcuts)
/**
* loads the shortcuts into the store from storage
* @returns {Promise<void>}
*/
async function loadShortcuts() {
await shortcuts.init()
}
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/browser/constructShortcutRules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { FIREFOX, SAFARI } from "~lib/constants/browserNames"
import type { Shortcut } from "~lib/types"
import { isNonChromiumBrowser } from "./getBrowserName"

/**
* construct the declarative net request rules for given shortcuts array
* @param {Shortcut[]} shortcuts shortcuts array
* @returns {browser.declarativeNetRequest.Rule[] | chrome.declarativeNetRequest.Rule[]} array of shortcut rules
*/
export function constructShortcutRules(shortcuts: Shortcut[]) {
if (isNonChromiumBrowser()) {
return shortcuts.map((shortcut, index) => {
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/browser/getBrowserName.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { FIREFOX, SAFARI } from "~lib/constants/browserNames";

/**
* gives the browser name
* @returns {string} browser name as per plasmo env
*/
export function getBrowserName() {
return process.env.PLASMO_BROWSER
}

/**
* checks if the browser is a non chromium based one or not
* @returns {boolean} true if if not based on chromium. else false
*/
export function isNonChromiumBrowser() {
// For now, only safari and firefox are considered in non chromium browsers
// More can be added in future if required
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/browser/getDeclarativeNetRequestRules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { isNonChromiumBrowser } from "./getBrowserName";

/**
* gives the saved declarative net request rules in browser
* @returns {browser.declarativeNetRequest.Rule[] | chrome.declarativeNetRequest.Rule[]} declarative net request rules array
*/
export async function getDeclarativeNetRequestRules() {
const browserName = process.env.PLASMO_BROWSER
if (isNonChromiumBrowser()) {
Expand Down
9 changes: 7 additions & 2 deletions lib/utils/browser/getFaviconURL.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { isNonChromiumBrowser } from "./getBrowserName"

export function getFaviconURL(u: string) {
/**
* gives the saved favicon url in browser of a given website
* @param {string} url the shortlink's url
* @returns {browser.declarativeNetRequest.Rule[] | chrome.declarativeNetRequest.Rule[]} declarative net request rules array
*/
export function getFaviconURL(shortcutURL: string) {
let url: URL | null = null
if (isNonChromiumBrowser()) {
// NOTE: This is a fallback icon. Firefox does not support accessing cached favicon
Expand All @@ -11,7 +16,7 @@ export function getFaviconURL(u: string) {
}

if (url) {
url.searchParams.set("pageUrl", u)
url.searchParams.set("pageUrl", shortcutURL)
url.searchParams.set("size", "32")
return url.toString()
}
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/browser/getTabUrl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { isNonChromiumBrowser } from "./getBrowserName"

/**
* gives the active/current tab url
* @returns {Promise<string>} url string of the active/current tab
*/
export async function getActiveTabURL() {
return await getTabURL()
}

/**
* utility function to get current tab url using browser APIs
* @returns {Promise<string>} url string of the active/current tab
*/
function getTabURL() {
return new Promise<string>((resolve) => {
try {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/browser/storageUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Storage } from "@plasmohq/storage";
import type { Shortcut } from "~lib/types";

/**
* gives the browser local storage data for a given key
* @param {string} key key for the stored data
* @returns {Promise<T>} stored data with given type
*/
export async function getDataFromStorage<T>(key) {
const storage = new Storage({
area: "local"
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/browser/tabOperations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { isNonChromiumBrowser } from "./getBrowserName"

/**
* opens new tab
* @param {chrome.tabs.CreateProperties | browser.tabs._CreateCreateProperties} props properties for creating new tab
* @returns {void}
*/
export function openNewTab(props: chrome.tabs.CreateProperties | browser.tabs._CreateCreateProperties) {
if (isNonChromiumBrowser()) {
browser.tabs.create(props)
Expand All @@ -9,6 +14,11 @@ export function openNewTab(props: chrome.tabs.CreateProperties | browser.tabs._C
}
}

/**
* updates the current tab
* @param {chrome.tabs.UpdateProperties | browser.tabs._UpdateUpdateProperties} props properties for updating the tab
* @returns {void}
*/
export function updateTab(props: chrome.tabs.UpdateProperties | browser.tabs._UpdateUpdateProperties) {
if (isNonChromiumBrowser()) {
browser.tabs.update(props)
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/browser/updateDeclarativeNetRequestRules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { isNonChromiumBrowser } from "./getBrowserName";

/**
* updates the declarative net request rules of the browser
* @param { browser.declarativeNetRequest.Rule[] | chrome.declarativeNetRequest.Rule[]} addRules rules that need to be added
* @param {number[]} removeRuleIds IDs of rules that need to be removed
* @returns {void}
*/
export async function updateDeclarativeNetRequestRules(
addRules: browser.declarativeNetRequest.Rule[]
| chrome.declarativeNetRequest.Rule[],
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/constructShortcutsFromCSVData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export async function constructShortcutsFromCSVData(csvData: string[][], savedSh
const shortcut: Shortcut = {
shortlink: shortcutRow[0],
url: shortcutRow[1],
createdAt: new Date(),
updatedAt: new Date(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
return shortcut;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/copyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* copies the given text to clipboard
* @param {string} text text to be copied
* @returns {void}
*/
export function copyToClipboard(text: string): void {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/isNotNull.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* checks if value is null or not
* @param {T | null} value value to be checked
* @returns {boolean} true if value is not null. else false
*/
export function isNotNull<T>(value: T | null): value is T {
return value !== null;
}
5 changes: 5 additions & 0 deletions lib/utils/setShortcutsToStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { Shortcut } from "~lib/types"
import { setDataToStorage } from "./browser/storageUtils"
import { SHORTCUTS_STORAGE_KEY } from "~lib/constants/keys"

/**
* set/store given shortcuts to storage
* @param {Shortcut[]} shortcuts shortcuts to be stored
* @returns {Promise<void>}
*/
export async function setShortcutsToStorage(shortcuts: Shortcut[]) {
await setDataToStorage(SHORTCUTS_STORAGE_KEY, shortcuts)
}
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"include": [
".plasmo/index.d.ts",
"./**/*.ts",
"./**/*.svelte"
"./**/*.svelte",
],
"compilerOptions": {
"paths": {
Expand All @@ -15,6 +15,6 @@
]
},
"baseUrl": ".",
"types": ["svelte", "chrome", "firefox-webext-browser"]
"types": ["svelte", "chrome", "firefox-webext-browser", "node"]
}
}

0 comments on commit a01cd52

Please sign in to comment.