1.1.15 • Published 7 months ago
@foundbyte/util v1.1.15
util
TODO: util library by Foundbyte
install
yarn add @foundbyte/util
// or
npm install @foundbyte/util
Usage
import { StorageService } from '@foundbyte/util';
enum EStorage {
Role = 'role',
User = 'user',
}
const storage = new StorageService<EStorage>({
rootKey: 'foundbyte',
});
storage.set(EStorage.Role, 'admin');
storage.get(EStorage.Role); // 'admin'
storage.set(EStorage.User, {
name: 'landSnow',
email: '2728997@qq.com',
});
storage.get(EStorage.User);
storage.remove(EStorage.User);
storage.get(EStorage.User); // undefined
StorageService
class StorageService<T = string> {
constructor(options: IStorageOptions) {}
}
IStorageOptions
interface IStorageOptions {
/** 唯一key */
rootKey: string;
/** 存储的类型 */
type?: 'local' | 'session';
}
API
表中的类型 T 为初始化实例传入 StorageService
的泛型类型,建议统一使用枚举(enum
)类型
方法 | 返回值 | 说明 |
---|---|---|
get(key: T) | any | 获取存储的数据 |
set(key: T, value: any) | - | 存储数据 |
remove(key: T) | - | 删除单条数据 |
clear() | - | 清空存储的数据 |
clipboard
import { clipboard } from '@foundbyte/util';
copy
clipboard.copy(`@foundbyte/util`);
paste
clipboard.paste().then(text => {
console.log(text);
});