Skip to content

Commit

Permalink
feat: utils
Browse files Browse the repository at this point in the history
  • Loading branch information
betterRunner committed Sep 6, 2021
1 parent 04047ab commit 7c63c9d
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum StorageKeys {
notes = "notes",
tags = "tags",
}

export const DOMATTR_RECT_GROUP = "note-id";

export const PREFIX_RECT = 'rect'
export const PREFIX_RECT_GROUP = "note"; // use "note" as group prefix as rects belong to note
23 changes: 23 additions & 0 deletions src/utils/letter-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pinyin, { STYLE_NORMAL } from "pinyin";

export function transArrInfoLetterMap(
arr: ({ [key: string]: any } | string)[],
key: string
) {
const res: { [key: string]: any[] } = {};
arr.forEach((ele) => {
const item = key ? (ele as any)[key] : ele;
const parts = pinyin(item, {
style: STYLE_NORMAL,
});
if (parts.length) {
let firstLetter = parts[0]?.[0]?.[0].toUpperCase() || "";
if (firstLetter < 'A' || firstLetter > 'Z') {
firstLetter = '_'
}
res[firstLetter] = res[firstLetter] || [];
res[firstLetter].push(ele);
}
});
return res;
}
27 changes: 27 additions & 0 deletions src/utils/mitt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mitt from "mitt";

const emitter = {
...mitt(),
// add `once` function manually
// https://github.com/developit/mitt/issues/54
once: (type: string, handler: (...args: any) => void) => {
const wrappedHandler = (evt: any) => {
handler(evt);
emitter.off(type, wrappedHandler);
};
emitter.on(type, wrappedHandler);
},
};

export default emitter;

export async function sendEmitAndWait(name: string, data: any) {
// wait until the `${name}-cb` event triggers
return new Promise((resolve) => {
emitter.once(`${name}-cb`, (data) => {
resolve(data);
});
// send the emit
emitter.emit(name, data);
});
}
30 changes: 30 additions & 0 deletions src/utils/search-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pinyin, { STYLE_NORMAL } from "pinyin";
import isChinese from "is-chinese";

export function filterBySearchText(arr: any[], key = "", text = "") {
const _getChPart = (text = "") => {
return text.replace(/[A-Za-z]/g, "");
};

const chPart = _getChPart(text);
const isTextContainCh = isChinese(chPart);
if (isTextContainCh) {
// searchText contains Chinese text
return arr.filter((e) => {
const item = key ? e[key] : e;
return item.includes(chPart);
});
} else {
return arr.filter((e) => {
const item = key ? e[key] : e;
// parse all texts to pinyin parts
const parts = pinyin(item, {
style: STYLE_NORMAL,
});
return parts
.map((part) => (part.length ? part[0] : ""))
.join("")
.includes(text);
});
}
}
113 changes: 113 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const _set = (key: string, value: any) =>
chrome.storage.local.set({ [key]: value });

const _get = (key: string) =>
new Promise((resolve) => {
chrome.storage.local.get([key], function(val: any) {
const res = val?.[key];
resolve(res);
});
});

export function set(key: string, value: any) {
return _set(key, value);
}

export function get(key: string) {
return _get(key);
}

export async function addItemToArr(arrKey: string, val: any) {
const arr = ((await _get(arrKey)) as any[]) ?? [];
arr.push(val);
set(arrKey, arr);
return arr;
}

export async function delItemFromArr(
arrKey: string,
val: string,
valKey?: string
) {
const arr = ((await _get(arrKey)) as any[]) ?? [];
const index = arr.findIndex((ele) =>
valKey ? ele?.[valKey] === val : ele === val
);
if (index !== -1) {
arr.splice(index, 1);
set(arrKey, arr);
}
return arr;
}

const _operArrItem = async (
arrKey: string,
itemKey: string,
itemVal: string,
oper: (item: any) => any
) => {
const arr = ((await _get(arrKey)) as any[]) ?? [];
const index = arr.findIndex((ele) => itemKey ? ele?.[itemKey] === itemVal : ele === itemVal);
if (index !== -1) {
// run the `oper` to get new item
const newItem = oper(arr[index]);
arr.splice(index, 1, newItem);
set(arrKey, arr);
}
return arr;
};

export async function updateArrItemProperty(
arrKey: string,
itemKey: string,
itemVal: string,
key: string,
val: any
) {
return _operArrItem(arrKey, itemKey, itemVal, (item) => ({
...item,
[key]: val,
}));
}

export async function addItemToArrProperty(
arrKey: string,
itemKey: string,
itemVal: string,
arrPropKey: string,
val: any,
checkDuplicate: boolean = true,
) {
return _operArrItem(arrKey, itemKey, itemVal, (item) => {
const arrProp = item?.[arrPropKey] || [];
const insert = checkDuplicate ? !arrProp.includes(val) : true;
insert && arrProp.push(val);
return {
...item,
[arrPropKey]: arrProp,
};
});
}

export async function delItemFromArrProperty(
arrKey: string,
itemKey: string,
itemVal: string,
arrPropKey: string,
val: any,
valKey?: string
) {
return _operArrItem(arrKey, itemKey, itemVal, (item) => {
const arrProp = item?.[arrPropKey] || [];
const index = arrProp.findIndex((ele: any) =>
valKey ? ele?.[valKey] === val : ele === val
);
if (index !== -1) {
arrProp.splice(index, 1);
}
return {
...item,
[arrPropKey]: arrProp,
};
});
}
12 changes: 9 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export const isObject = (obj: any) => {
return typeof obj === 'object' && obj !== null
}
return typeof obj === "object" && obj !== null;
};

export const getObjectType = (obj: any) => Object.prototype.toString.call(obj)
export const getObjectType = (obj: any) => Object.prototype.toString.call(obj);

export const removeUrlPostfix = (url: string) => {
let res = url.split("?")?.[0] ?? "";
res = url.split("#")?.[0] ?? "";
return res;
};

0 comments on commit 7c63c9d

Please sign in to comment.