utools-utils
Version:
Common utilities for uTools
320 lines (314 loc) • 8.34 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
AbstractStorage: () => AbstractStorage,
BrowserStorageAdapter: () => BrowserStorageAdapter,
entitySearcher: () => entitySearcher,
hideAndOutPlugin: () => hideAndOutPlugin,
isBrowser: () => isBrowser,
isUTools: () => isUTools,
local: () => local,
pinyinMatch: () => pinyinMatch,
searchList: () => searchList,
searchListItems: () => searchListItems,
sync: () => sync,
templateBuilder: () => templateBuilder,
toMap: () => toMap
});
module.exports = __toCommonJS(src_exports);
// src/common.ts
function isUTools() {
return Reflect.has(window, "utools");
}
function isBrowser() {
return !isUTools();
}
function hideAndOutPlugin() {
utools.hideMainWindow();
utools.outPlugin();
}
function toMap(arr, keyFn, valFn) {
if (valFn) {
const map2 = /* @__PURE__ */ new Map();
arr.forEach((item, index) => map2.set(keyFn(item, index), valFn(item, index)));
return map2;
}
const map = /* @__PURE__ */ new Map();
arr.forEach((item, index) => map.set(keyFn(item, index), item));
return map;
}
// src/search.ts
function entitySearcher(keys) {
return (item, word) => {
word = word.toLowerCase();
for (const key of keys) {
const value = item[key];
if (typeof value === "string" && value.toLowerCase().includes(word)) {
return true;
}
}
return false;
};
}
function search(list, word, searcher) {
if (!word)
return list;
return list.filter((item) => searcher(item, word));
}
function searchList(list, words, searcher) {
if (!Array.isArray(words))
return search(list, words, searcher);
let filteredList = list;
for (const word of words) {
filteredList = search(filteredList, word, searcher);
}
return filteredList;
}
function matchString(s, p, start) {
if (start + p.length > s.length)
return false;
for (let i = 0; i < p.length; i++) {
if (s.charAt(start++) !== p.charAt(i))
return false;
}
return true;
}
function pinyinMatch(pinyin, pattern, options) {
let pys;
if (typeof pinyin === "string") {
const separator = options?.separator ?? " ";
pys = pinyin.split(separator);
} else {
pys = pinyin;
}
if (options?.case === "upper") {
pattern = pattern.toUpperCase();
} else if (options?.case === "sensitive") {
pattern = pattern;
} else {
pattern = pattern.toLowerCase();
}
let flatPinyin = "";
let firstPinyin = "";
let pos = 0;
const n = pys.length;
const positions = new Array(n);
for (let i = 0; i < n; i++) {
const py = pys[i];
flatPinyin += py;
firstPinyin += py.charAt(0);
positions[i] = pos;
pos += py.length;
}
for (let i = 0; i < n; i++) {
const py = pys[i];
if (py.startsWith(pattern)) {
return { from: i, to: i };
}
if (pattern.startsWith(py) && matchString(flatPinyin, pattern, positions[i])) {
const matchPinyinLen = positions[i] + pattern.length;
let to = i;
for (let j = i; j < n; j++) {
if (positions[j] >= matchPinyinLen)
break;
to = j;
}
return { from: i, to };
}
}
const firstIndex = firstPinyin.indexOf(pattern);
if (firstIndex >= 0) {
return { from: firstIndex, to: firstIndex + pattern.length - 1 };
}
return null;
}
// src/storage.ts
var AbstractStorage = class {
get(key, defaultVal) {
if (defaultVal !== void 0) {
return this.getItem(key) ?? defaultVal;
}
return this.getItem(key);
}
};
var UToolsSyncStorage = class extends AbstractStorage {
getItem(key) {
return utools.dbStorage.getItem(key);
}
like(prefix) {
const docs = utools.db.allDocs(prefix);
const key = "value";
return docs.map((doc) => doc[key]);
}
set(key, value) {
utools.dbStorage.setItem(key, value);
}
remove(key) {
utools.dbStorage.removeItem(key);
}
};
var UToolsLocalStorage = class extends AbstractStorage {
storage = new UToolsSyncStorage();
localKey(key) {
return `${utools.getNativeId()}/${key}`;
}
getItem(key) {
return this.storage.get(this.localKey(key));
}
like(prefix) {
return this.storage.like(this.localKey(prefix));
}
set(key, value) {
this.storage.set(this.localKey(key), value);
}
remove(key) {
this.storage.remove(this.localKey(key));
}
};
var BrowserStorageAdapter = class extends AbstractStorage {
constructor(storage) {
super();
this.storage = storage;
}
getItem(key) {
const value = this.storage.getItem(key);
return value !== null ? JSON.parse(value) : null;
}
like(prefix) {
const list = [];
for (const key in this.storage) {
if (key.startsWith(prefix)) {
list.push(JSON.parse(this.storage[key]));
}
}
return list;
}
set(key, value) {
this.storage.setItem(key, JSON.stringify(value));
}
remove(key) {
this.storage.removeItem(key);
}
};
var sync = new UToolsSyncStorage();
var local = new UToolsLocalStorage();
// src/template.ts
function searchListItems(listItems, word) {
return searchList(listItems, word, entitySearcher(["title", "description"]));
}
var TemplateBuilder = class {
exports = {};
/**
* 根据无 UI 模板构建
* @param templates 无 UI 模板
*/
none(...templates) {
for (const template of templates) {
this.exports[template.code] = {
mode: "none",
args: {
enter: (action) => template.enter(action)
}
};
}
return this;
}
/**
* 根据不可变列表模板构建
* @param templates 不可变列表模板,列表项是固定的
*/
immutableList(...templates) {
for (const template of templates) {
const { list, placeholder } = template;
this.exports[template.code] = {
mode: "list",
args: {
enter: (action, render) => render(list),
search: (action, searchWord, render) => {
if (template.search) {
template.search(action, searchWord, render);
} else {
render(searchListItems(list, searchWord));
}
},
select: (action, item) => item.handler(action),
placeholder
}
};
}
return this;
}
/**
* 根据可变列表模板构建
* @param templates 可变列表模板,列表项是动态的
*/
mutableList(...templates) {
for (const template of templates) {
const { placeholder } = template;
this.exports[template.code] = {
mode: "list",
args: {
enter: (action, render) => {
template.enter?.(action, (list) => {
template.$list = list;
render(list);
});
},
search: (action, searchWord, render) => {
if (template.search) {
template.search(action, searchWord, render);
} else {
render(searchListItems(template.$list ?? [], searchWord));
}
},
select: (action, item) => template.select(action, item),
placeholder
}
};
}
return this;
}
/**
* 获取构建结果
*/
build() {
return this.exports;
}
};
function templateBuilder() {
return new TemplateBuilder();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
AbstractStorage,
BrowserStorageAdapter,
entitySearcher,
hideAndOutPlugin,
isBrowser,
isUTools,
local,
pinyinMatch,
searchList,
searchListItems,
sync,
templateBuilder,
toMap
});
;