Skip to content

Commit

Permalink
Rename package
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinBirkhoff committed Feb 17, 2024
1 parent 3ae0336 commit d3fe314
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 104 deletions.
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div align="center">

![Build Status](https://github.com/GavinBirkhoff/local-store-pro/actions/workflows/node-ci.yml/badge.svg)
[![codecov](https://codecov.io/github/GavinBirkhoff/local-store-pro/branch/main/graph/badge.svg)](https://codecov.io/github/GavinBirkhoff/local-store-pro)
![license](https://img.shields.io/github/license/gavinbirkhoff/local-store-pro)
![release](https://img.shields.io/github/release/gavinbirkhoff/local-store-pro.svg)
![Build Status](https://github.com/GavinBirkhoff/storetify/actions/workflows/node-ci.yml/badge.svg)
[![codecov](https://codecov.io/github/GavinBirkhoff/storetify/branch/main/graph/badge.svg)](https://codecov.io/github/GavinBirkhoff/storetify)
![license](https://img.shields.io/github/license/gavinbirkhoff/storetify)
![release](https://img.shields.io/github/release/gavinbirkhoff/storetify.svg)

</div>

English | [简体中文](https://github.com/GavinBirkhoff/local-store-pro/blob/main/README.zh-CN.md)
English | [简体中文](https://github.com/GavinBirkhoff/storetify/blob/main/README.zh-CN.md)

The encapsulation of local storage localStorage, provides expiration time setting and subscription functions, provides simple API use, no dependencies, and the compression is only 3.71KB (gzipped: 1.37KB).

Expand All @@ -23,13 +23,13 @@ The encapsulation of local storage localStorage, provides expiration time settin

```bash
# npm install
npm install local-store-pro
npm install storetify

# yarn install
yarn add local-store-pro
yarn add storetify

#pnpm install
pnpm add local-store-pro
pnpm add storetify
```

## 🏗️ Build
Expand All @@ -46,22 +46,20 @@ npm test

# 🔨 Usage

or manually download and include in your HTML **local-store-pro.min.js**,you can also pass [UNPKG](https://unpkg.com/local-store-pro/lib/) to download:
```js
import store from 'storetify';
store("test","storetify");
```

or manually download and include in your HTML **storetify.min.js**,you can also pass [UNPKG](https://unpkg.com/storetify/lib/) to download:

```html
<script src="https://unpkg.com/local-store-pro/lib/local-store-pro.min.js"></script>
<script src="https://unpkg.com/storetify/lib/storetify.min.js"></script>
<script type="text/javascript">
LocalStorePro("test","local-store-pro");
Storetify("test","storetify");
</script>
```

or

```js
import store from 'local-store-pro';
store("test","local-store-pro");
```

## ⚙️ API

### set
Expand Down Expand Up @@ -121,7 +119,7 @@ store.subscribe("test",(e)=>{})
For the event variable e, it is an abbreviated object from the StorageEvent object, which provides some practical properties, which can be used to observe the changes of key-value pairs well, as shown in the following table:

```ts
type NextStorageEventValue = Partial<any> | any[] | null | string | number
type NextStorageEventValue = Record<string, any> | any[] | null | string | number
```
| Property | Type | Description|
Expand Down
36 changes: 17 additions & 19 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div align="center">

![Build Status](https://github.com/GavinBirkhoff/local-store-pro/actions/workflows/node-ci.yml/badge.svg)
[![codecov](https://codecov.io/github/GavinBirkhoff/local-store-pro/branch/main/graph/badge.svg)](https://codecov.io/github/GavinBirkhoff/local-store-pro)
![license](https://img.shields.io/github/license/gavinbirkhoff/local-store-pro)
![release](https://img.shields.io/github/release/gavinbirkhoff/local-store-pro.svg)
![Build Status](https://github.com/GavinBirkhoff/storetify/actions/workflows/node-ci.yml/badge.svg)
[![codecov](https://codecov.io/github/GavinBirkhoff/storetify/branch/main/graph/badge.svg)](https://codecov.io/github/GavinBirkhoff/storetify)
![license](https://img.shields.io/github/license/gavinbirkhoff/storetify)
![release](https://img.shields.io/github/release/gavinbirkhoff/storetify.svg)

</div>

[English](https://github.com/GavinBirkhoff/local-store-pro/blob/main/README.md) | 简体中文
[English](https://github.com/GavinBirkhoff/storetify/blob/main/README.md) | 简体中文

🦄本地存储localStorage的封装,提供过期时间设置和订阅功能,提供简单API使用,没有依赖,压缩只有 3.81KB(gzipped: 1.39KB)。

Expand All @@ -23,13 +23,13 @@

```bash
# npm 安装
npm install local-store-pro
npm install storetify

# yarn 安装
yarn add local-store-pro
yarn add storetify

#pnpm 安装
pnpm add local-store-pro
pnpm add storetify
```

## 🏗️ 构建
Expand All @@ -46,22 +46,20 @@ npm test

# 🔨 使用

或者在您的HTML中手动下载并引入 **local-store-pro.min.js**,你也可以通过 [UNPKG](https://unpkg.com/local-store-pro/lib/) 进行下载:
```js
import store from 'storetify';
store("test","storetify");
```

或者在您的HTML中手动下载并引入 **storetify.min.js**,你也可以通过 [UNPKG](https://unpkg.com/storetify/lib/) 进行下载:

```html
<script src="https://unpkg.com/local-store-pro/lib/local-store-pro.min.js"></script>
<script src="https://unpkg.com/storetify/lib/storetify.min.js"></script>
<script type="text/javascript">
LocalStorePro("test","local-store-pro");
Storetify("test","storetify");
</script>
```

or

```js
import store from 'local-store-pro';
store("test","local-store-pro");
```

# ⚙️ API

### set
Expand Down Expand Up @@ -121,7 +119,7 @@ store.subscribe("test",(e)=>{})
对于事件变量e,是一个来自StorageEvent对象的简略对象,提供了一些实用的属性,可以很好的观察键值对的变化,如下表:

```ts
type NextStorageEventValue = Partial<any> | any[] | null | string | number
type NextStorageEventValue = Record<string, any> | any[] | null | string | number
```
| Property | Type | Description|
Expand Down
4 changes: 2 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<body>
<button id="btn">++</button>
<button id="immutable">immutable</button>
<script src="../lib/local-store-pro.min.js"></script>
<script src="../lib/storetify.min.js"></script>
<script>
const store = LocalStorePro
const store = Storetify
console.dir(store, 'store');
let i = 1
const btn = document.getElementById('btn')
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "storeotify",
"name": "storetify",
"version": "1.0.0",
"description": "Better localStorage, subscribable localStorage, expirable localStorage",
"main": "./lib/storeotify.cjs.js",
"module": "./lib/storeotify.esm.js",
"browser": "./lib/storeotify.min.js",
"unpkg": "./lib/storeotify.min.js",
"types": "./lib/storeotify.d.ts",
"main": "./lib/storetify.cjs.js",
"module": "./lib/storetify.esm.js",
"browser": "./lib/storetify.min.js",
"unpkg": "./lib/storetify.min.js",
"types": "./lib/storetify.d.ts",
"scripts": {
"init": "npm install && npm run build",
"lint": "eslint src --ext .js,.ts",
Expand Down Expand Up @@ -49,9 +49,9 @@
]
},
"bugs": {
"url": "https://github.com/GavinBirkhoff/storeotify/issues"
"url": "https://github.com/GavinBirkhoff/storetify/issues"
},
"homepage": "https://github.com/GavinBirkhoff/storeotify#readme",
"homepage": "https://github.com/GavinBirkhoff/storetify#readme",
"devDependencies": {
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^8.3.3",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default [
{
format: "umd",
file: `lib/${pkg.name}.min.js`,
name: "LocalStorePro",
name: "Storetify",
noConflict: true,
sourcemap: true,
},
Expand Down
2 changes: 1 addition & 1 deletion src/PowerStorage.ts → src/NextStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dispatchStorageEvent, each, isValidKey, jsonParse } from "./utils"
* Next localStorage
*/
class NextStorage {
namespace = "ts-store"
namespace = "storetify"

protected store = localStorage

Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import store from "./store"
export {
LocalStorePro,
Storetify,
StoreArgument,
StorageEventKey,
StoreListener,
StoreProEvent,
StoretifyEvent,
NextStorageValue,
NextStorageEventValue,
LocalStoragePro,
LocalStoreStageMap,
StoretifyStageMap,
} from "./type/"
export default store
22 changes: 11 additions & 11 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import NextStorage from "./PowerStorage"
import { LocalStorePro, LocalStoreStage, LocalStoreStageMap, StoreArgument, StoreStage } from "./type"
import NextStorage from "./NextStorage"
import { Storetify, StoretifyStage, StoretifyStageMap, StoreArgument, StoreStage } from "./type"

const storage = NextStorage.getInstance()

const store: LocalStoreStage = function <T, K extends StoreArgument<T>>(...rest: K): LocalStoreStageMap[K["length"]] {
const store: StoretifyStage = function <T, K extends StoreArgument<T>>(...rest: K): StoretifyStageMap[K["length"]] {
const len = rest.length
const [key, value, expires] = rest
if (len === 1 && typeof key === "string") {
return storage.get(key) as LocalStoreStageMap[K["length"]]
return storage.get(key) as StoretifyStageMap[K["length"]]
}
if (len >= 2 && typeof key === "string") {
if (value === undefined) {
return storage.remove(key) as LocalStoreStageMap[K["length"]]
return storage.remove(key) as StoretifyStageMap[K["length"]]
}
if (typeof value === "function") {
return storage.set(key, value(), expires) as LocalStoreStageMap[K["length"]]
return storage.set(key, value(), expires) as StoretifyStageMap[K["length"]]
}
return storage.set(key, value, expires) as LocalStoreStageMap[K["length"]]
return storage.set(key, value, expires) as StoretifyStageMap[K["length"]]
}
return null as LocalStoreStageMap[K["length"]]
return null as StoretifyStageMap[K["length"]]
}

function init(): LocalStorePro {
function init(): Storetify {
const propertyNames = Object.getOwnPropertyNames(Object.getPrototypeOf(storage))
propertyNames.forEach(key => {
if (key !== "constructor" && key !== "getStore") {
const value = storage[key as keyof NextStorage]
const storeKey = key as keyof LocalStorePro
const storeKey = key as keyof Storetify
if (typeof value === "function") {
;(store as any)[storeKey] = value.bind(storage)
} else {
Expand Down Expand Up @@ -59,7 +59,7 @@ function init(): LocalStorePro {
window.addEventListener("storage", handleStorageChange)
}

return store as LocalStorePro
return store as Storetify
}

export default init()
28 changes: 13 additions & 15 deletions src/type/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import NextStorage from "../PowerStorage"
import NextStorage from "../NextStorage"

export type StorageEventKey = string | null
export type StorageEventValue = string | null
export type NextStorageValue = Partial<any> | any[] | null | string | number
export type NextStorageEventValue = Partial<any> | any[] | null | string | number
export type NextStorageValue = Record<string, any> | any[] | null | string | number
export type NextStorageEventValue = Record<string, any> | any[] | null | string | number

export type StoreArgument<T> = [string?, T?, number?]
export type StoreListener = <T extends StoreProEvent>(e: T) => void
export type StoreListener = <T extends StoretifyEvent>(e: T) => void

export type LocalStoreStageMap = [null, NextStorageValue, NextStorage, NextStorage]
export type StoretifyStageMap = [null, NextStorageValue, NextStorage, NextStorage]

export type NextStorageEventValueOrNextStorage<K extends { length: number }> = LocalStoreStageMap[K["length"]]
export type NextStorageEventValueOrNextStorage<K extends { length: number }> = StoretifyStageMap[K["length"]]

export interface StoreProEvent extends Omit<StorageEvent, "newValue" | "oldValue"> {
export interface StoretifyEvent extends Omit<StorageEvent, "newValue" | "oldValue"> {
newValue: NextStorageEventValue
oldValue: NextStorageEventValue
native: StorageEvent
Expand All @@ -25,18 +25,16 @@ export interface StoreStage {
remove: (key: string, soft?: boolean) => void
has: (key: string) => boolean
clear: () => void
subscribe: (key: string, listener: (e: StoreProEvent) => void) => void
unsubscribe: (keys: string | string[], listener?: (e: StoreProEvent) => void) => void
subscribe: (key: string, listener: (e: StoretifyEvent) => void) => void
unsubscribe: (keys: string | string[], listener?: (e: StoretifyEvent) => void) => void
getObserver(key: string): StoreListener[]
getUsed: () => string
}

export interface LocalStoreStage extends Partial<StoreStage> {
<T, K extends StoreArgument<T>>(...rest: K): LocalStoreStageMap[K["length"]]
export interface StoretifyStage extends Partial<StoreStage> {
<T, K extends StoreArgument<T>>(...rest: K): StoretifyStageMap[K["length"]]
}

export interface LocalStorePro extends StoreStage {
<T, K extends StoreArgument<T>>(...rest: K): LocalStoreStageMap[K["length"]]
export interface Storetify extends StoreStage {
<T, K extends StoreArgument<T>>(...rest: K): StoretifyStageMap[K["length"]]
}

export type LocalStoragePro = NextStorage
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextStorageEventValue, StoreListener, StoreProEvent } from "./type"
import { NextStorageEventValue, StoreListener, StoretifyEvent } from "./type"

export function dispatchStorageEvent({
key,
Expand Down Expand Up @@ -38,7 +38,7 @@ export function each(funcs: StoreListener[], ev: StorageEvent, defaultKey: strin
url: ev.url,
isTrusted: ev.isTrusted,
native: ev,
} as StoreProEvent
} as StoretifyEvent

funcs.forEach((func: StoreListener) => {
if (typeof func === "function") {
Expand Down
6 changes: 3 additions & 3 deletions test/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import store, { StoreProEvent } from "../src/index"
import store, { StoretifyEvent } from "../src/index"

describe("local-store-pro api test", () => {
describe("storetify api test", () => {
test("test setNamespace getNamespace", () => {
const namespace = "my-store"
;(store as any).setNamespace(namespace)
Expand Down Expand Up @@ -71,7 +71,7 @@ describe("local-store-pro api test", () => {
})
test("test subscribe and expires", done => {
store("token-6823", "xxxx", 5)
const callFun = (ev: StoreProEvent) => {
const callFun = (ev: StoretifyEvent) => {
if (ev.newValue) {
done()
}
Expand Down
4 changes: 2 additions & 2 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import store from "../src/index"
import fn from "../example/fn"

jest.mock("../example/fn")
const keyPrefix = "test-local-store-pro"
const keyPrefix = "test-storetify"
const getKey = (key: string) => keyPrefix + key
describe("local-store-pro", () => {
describe("storetify", () => {
test("store() return null", () => {
expect(store()).toBe(null)
})
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import store from "./store";
export { LocalStorePro, StoreArgument, StorageEventKey, StoreListener, StoreProEvent, NextStorageValue, NextStorageEventValue, LocalStoragePro, LocalStoreStageMap, } from "./type/";
export { Storetify, StoreArgument, StorageEventKey, StoreListener, StoretifyEvent, NextStorageValue, NextStorageEventValue, StoretifyStageMap, } from "./type/";
export default store;
4 changes: 2 additions & 2 deletions types/store.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { LocalStorePro } from "./type";
declare const _default: LocalStorePro;
import { Storetify } from "./type";
declare const _default: Storetify;
export default _default;
Loading

0 comments on commit d3fe314

Please sign in to comment.