var-persist/README.md

321 lines
9.8 KiB
Markdown
Raw Permalink Normal View History

2023-12-19 23:08:37 -05:00
<!-- Header -->
<div id="top" align="center">
<br />
<!-- Logo -->
2023-12-20 20:01:52 -05:00
<img src="https://git.zakscode.com/repo-avatars/89f6c36caf75762ed9f7f98b69044b7db30da5230be7c5cea54f8a1158f1669a" alt="Logo" width="200" height="200">
2023-12-19 23:08:37 -05:00
<!-- Title -->
2024-01-07 17:06:20 -05:00
### var-persist
2023-12-19 23:08:37 -05:00
<!-- Description -->
2024-01-07 17:06:20 -05:00
TypeScript: Sync variables with localStorage
2023-12-19 23:08:37 -05:00
<!-- Repo badges -->
[![Version](https://img.shields.io/badge/dynamic/json.svg?label=Version&style=for-the-badge&url=https://git.zakscode.com/api/v1/repos/ztimson/persist/tags&query=$[0].name)](https://git.zakscode.com/ztimson/persist/tags)
[![Pull Requests](https://img.shields.io/badge/dynamic/json.svg?label=Pull%20Requests&style=for-the-badge&url=https://git.zakscode.com/api/v1/repos/ztimson/persist&query=open_pr_counter)](https://git.zakscode.com/ztimson/persist/pulls)
[![Issues](https://img.shields.io/badge/dynamic/json.svg?label=Issues&style=for-the-badge&url=https://git.zakscode.com/api/v1/repos/ztimson/persist&query=open_issues_count)](https://git.zakscode.com/ztimson/persist/issues)
<!-- Links -->
---
<div>
2023-12-27 17:23:17 -05:00
<a href="https://git.zakscode.com/ztimson/persist/releases" target="_blank">Release Notes</a>
2023-12-19 23:08:37 -05:00
<a href="https://git.zakscode.com/ztimson/persist/issues/new?template=.github%2fissue_template%2fbug.md" target="_blank">Report a Bug</a>
<a href="https://git.zakscode.com/ztimson/persist/issues/new?template=.github%2fissue_template%2fenhancement.md" target="_blank">Request a Feature</a>
</div>
---
</div>
## Table of Contents
2024-01-07 17:06:20 -05:00
- [var-persist](#top)
2023-12-19 23:08:37 -05:00
- [About](#about)
2023-12-27 17:23:17 -05:00
- [Examples](#examples)
- [Built With](#built-with)
2023-12-19 23:08:37 -05:00
- [Setup](#setup)
2023-12-27 17:23:17 -05:00
- [Production](#production)
- [Development](#development)
- [Documentation](#documentation)
- [Classes](#classes)
- [Decorators](#decorators)
- [Types](#types)
2023-12-19 23:08:37 -05:00
- [License](#license)
## About
2024-01-07 17:06:20 -05:00
Var-Persist is an updated version of [webstorage-decorators](https://git.zakscode.com/ztimson/webstorage-decorators), a library which saves variables to local or session storage.
2023-12-27 17:23:17 -05:00
2023-12-27 17:39:59 -05:00
This library aims to improve upon the original's limitations by using the new [Proxy Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). Improvements include:
2024-01-05 15:57:45 -05:00
- Supports both OOP & Decorator patterns
2023-12-27 17:23:17 -05:00
- Proxy object ensures all changes are tracked including impure functions
- [Proto]types and functions can be preserved by passing the `type` option
2024-01-05 15:57:45 -05:00
**Disclaimer:** JavaScript's decorators are currently undergoing changes to the API overseen by [TC39](https://tc39.es) and currently have no support for property decorators. [Experimental decorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) must be enabled to work properly.
2023-12-27 17:23:17 -05:00
### Examples
Using objects:
```ts
2024-01-07 17:06:20 -05:00
import {Persist} from 'var-persist';
2023-12-27 17:23:17 -05:00
// Proxy Object (Always access/modify using `.value`):
let theme = new Persist<string>('theme', {default: 'os'});
console.log(theme.value) // Output: os
theme.value = 'light'; // Changes will be synced to localStorage['theme'];
```
Using decorators:
```ts
2024-01-07 17:06:20 -05:00
import {persist} from 'var-persist';
2023-12-27 17:23:17 -05:00
// Using decorators
class Theme {
@persist({key: 'theme', default: 'os'}) current!: string;
}
const theme = new Theme();
console.log(theme.current); // Output: light
theme.current = 'dark'; // You can ommit `.value` when using the decorator
```
2023-12-19 23:08:37 -05:00
2023-12-27 17:39:59 -05:00
Advanced uses:
```ts
2024-01-07 17:06:20 -05:00
import {Persist} from 'var-persist';
2023-12-27 17:39:59 -05:00
// Options are the same for both the persist decorator and object:
let example = new Persist<string[]>('example', {
storage: SessionStorage, // Use a different storage solution
default: [], // Default value if stored value === undefined
type: Array // Ensures [proto]type & methods are restored
});
// Callback when changes are made
example.watch(value => console.log(`Length - ${value.length}`));
example.value = [1, 2, 3];
// Output: Length - 3
example.value.pop(); // Impure changes are saved
// Output: Length - 2
```
2023-12-19 23:08:37 -05:00
### Built With
[![TypeScript](https://img.shields.io/badge/TypeScript-3178C6?style=for-the-badge&logo=typescript&logoColor=white)](https://typescriptlang.org/)
## Setup
<details>
<summary>
<h3 id="production" style="display: inline">
Production
</h3>
</summary>
#### Prerequisites
- [Node.js](https://nodejs.org/en/download)
#### Instructions
2024-01-07 17:06:20 -05:00
1. Install persist: `npm i var-persist`
2023-12-19 23:08:37 -05:00
2. Enable decorators inside `tsconfig.json`:
```json
{
"compilerOptions": {
"experimentalDecorators": true,
...
},
...
}
```
2023-12-27 17:23:17 -05:00
3. Import & use, see [examples above](#examples)
2023-12-19 23:08:37 -05:00
</details>
<details>
<summary>
<h3 id="development" style="display: inline">
Development
</h3>
</summary>
#### Prerequisites
- [Node.js](https://nodejs.org/en/download)
#### Instructions
1. Install the dependencies: `npm i`
2. Build library & docs: `npm build`
3. Run unit tests: `npm test`
</details>
2023-12-27 17:23:17 -05:00
## Documentation
<details>
<summary>
<h3 id="classes" style="display: inline">
Classes
</h3>
</summary>
Create a proxy object which wraps your data so any changes can be intercepted & synced with storage.
Your data is stored under the `value` property and should always be accessed/modified through it.
#### Example
```ts
2024-01-07 17:06:20 -05:00
import {Persist} from 'var-persist'
2023-12-27 17:23:17 -05:00
const theme = new Persist('theme.current', {default: 'os'});
console.log(theme.value); // Output: os
theme.value = 'light'; // Make sure you always use .value to access/modify data
location.reload(); // Simulate refresh
console.log(theme.value); // Output: light
```
#### Constructor
`Persist<T>(key: string, options?: PersistOptions)`
| Argument | Type | Description |
|-----------|--------------------------------------------------|-------------------------------------------------------------|
| `key` | `string` | Primary key value will be stored under |
| `options` | [`PersistOptions<T>`](../Home.md#persistoptions) | Configure using [PersistOptions](../Home.md#persistoptions) |
2024-01-05 15:57:45 -05:00
###### Properties
2023-12-27 17:23:17 -05:00
| Name | Type | Description |
|-----------|--------------------------------------------------|-------------------------------------------------------------|
| `key` | `string` | Primary key value will be stored under |
| `options` | [`PersistOptions<T>`](../Home.md#persistoptions) | Configure using [PersistOptions](../Home.md#persistoptions) |
| `value` | `T` | Current value |
#### Methods
##### clear
Delete value from storage
`clear(): void`
##### load
Load value from storage
`load(): void`
##### save
Save current value to storage
`save(): void`
##### watch
2024-01-05 15:57:45 -05:00
Callback function which is run when there are changes
2023-12-27 17:23:17 -05:00
`watch(fn: (value: T) => void): void`
2024-01-05 15:57:45 -05:00
###### Parameters
2023-12-27 17:23:17 -05:00
2024-01-05 15:57:45 -05:00
| Name | Type | Description |
|------|-------------------------|---------------------------------------------------------------------------------------|
| `fn` | (`value`: `T`) => `any` | Callback will run on each change; it's passed the next value & it's return is ignored |
###### Returns
`() => void` - Function which will unsubscribe the watch/callback when called
2023-12-27 17:23:17 -05:00
##### toString
Return value as JSON string
`toString(): string`
###### Returns
`string` - Stringified JSON object
##### valueOf
2024-01-05 15:57:45 -05:00
Current value
2023-12-27 17:23:17 -05:00
`valueOf(): T`
###### Returns
`T` - Current value
</details>
<details>
<summary>
<h3 id="decorators" style="display: inline">
Decorators
</h3>
</summary>
2024-01-05 15:57:45 -05:00
Create a proxy object which wraps your data so any changes can be intercepted & synced with storage.
Your data is stored under the value property and should always be accessed/modified through it.
#### Example
```ts
2024-01-07 17:06:20 -05:00
import {Persist} from 'var-persist';
2024-01-05 15:57:45 -05:00
class Theme {
// This property will automatically sync with localStorage
@Persist({default: 'os'}) current!: string;
constructor() {
console.log(this.current); // Output: os
this.current == localStorage.getItem('Thene.current'); // True
this.current = 'light';
console.log(this.current); // Output: light
}
}
```
#### Constructor
2023-12-27 17:23:17 -05:00
`persist(options?: {key?: string} & PersistOptions<T>)`
2024-01-05 15:57:45 -05:00
| Argument | Type | Description |
|-----------|--------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|
| `options` | `{key: string}` & [`PersistOptions<T>`](../Home.md#persistoptions) | Configure using [PersistOptions](../Home.md#persistoptions). Storage key can also be ovverriden by passing `key` as an option |
2023-12-27 17:23:17 -05:00
</details>
<details>
<summary>
<h3 id="types" style="display: inline">
Types
</h3>
</summary>
`PersistOptions<T>`
Configurable options to change persistence behavior
| Property | Type | Description |
|------------|-----------|----------------------------------------------------------------------------------------|
| `default?` | `T` | Default/Initial value if undefined |
| `storage?` | `Storage` | Storage implementation, defaults to LocalStorage. Can also be used with SessionStorage |
| `type?` | `any` | Force value to have \[proto\]type |
</details>
2023-12-19 23:08:37 -05:00
## License
Copyright © 2023 Zakary Timson | Available under MIT Licensing
See the [license](./LICENSE) for more information.