@nexim/alpine 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/LICENSE +661 -0
- package/README.md +81 -0
- package/dist/main.cjs +165 -0
- package/dist/main.cjs.LEGAL.txt +0 -0
- package/dist/main.cjs.map +7 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.mjs +130 -0
- package/dist/main.mjs.LEGAL.txt +0 -0
- package/dist/main.mjs.map +7 -0
- package/dist/store/store-generator.d.ts +36 -0
- package/dist/store/store-generator.d.ts.map +1 -0
- package/dist/store/store-with-backup.d.ts +88 -0
- package/dist/store/store-with-backup.d.ts.map +1 -0
- package/package.json +78 -0
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @nexim/alpine
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
`@nexim/alpine` is a versatile library designed to enhance your Alpine.js experience with a suite of utility functions and mixins. It provides robust solutions for data management, including logging capabilities and backup functionalities with local storage support. This library aims to streamline the development of high-performance projects, ensuring efficiency and scalability.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install the package using npm or yarn:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @nexim/alpine
|
|
19
|
+
|
|
20
|
+
# Or using yarn
|
|
21
|
+
yarn add @nexim/alpine
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
### alpineStoreGenerator
|
|
27
|
+
|
|
28
|
+
Generates an Alpine.js store with a default value.
|
|
29
|
+
|
|
30
|
+
#### Example Usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import {alpineStoreGenerator} from '@nexim/alpine';
|
|
34
|
+
|
|
35
|
+
const store = alpineStoreGenerator({
|
|
36
|
+
name: 'user',
|
|
37
|
+
defaultValue: {type: 'root'},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log(store.type); // Output: root
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### AlpineStoreWithBackup
|
|
44
|
+
|
|
45
|
+
Extends `AlpineStore` to add backup and restore functionality with local storage support and expiration handling.
|
|
46
|
+
|
|
47
|
+
#### Constructor
|
|
48
|
+
|
|
49
|
+
Creates an instance of `AlpineStoreWithBackup`.
|
|
50
|
+
|
|
51
|
+
- **config**: The configuration object for the store.
|
|
52
|
+
- **name**: The name of the store.
|
|
53
|
+
- **version**: The version of the store.
|
|
54
|
+
- **defaultValue**: The default value of the store.
|
|
55
|
+
- **expireDuration**: Optional. The duration after which the store expires.
|
|
56
|
+
|
|
57
|
+
#### Methods
|
|
58
|
+
|
|
59
|
+
- **save()**: Saves the current data to local storage. If the data is null, it clears the stored data. Also updates the expiration time.
|
|
60
|
+
- **clear()**: Clears the local storage and set default value to store.
|
|
61
|
+
|
|
62
|
+
#### Example Usage
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import {AlpineStoreWithBackup} from '@nexim/alpine';
|
|
66
|
+
|
|
67
|
+
const storeWithBackup = new AlpineStoreWithBackup({
|
|
68
|
+
name: 'myStoreWithBackup',
|
|
69
|
+
version: 1,
|
|
70
|
+
defaultValue: {data: 'root'},
|
|
71
|
+
expireDuration: '1d',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
storeWithBackup.store.data = 'user';
|
|
75
|
+
|
|
76
|
+
storeWithBackup.save();
|
|
77
|
+
console.log(storeWithBackup.store.data); // Output: { data: 'user' }
|
|
78
|
+
|
|
79
|
+
storeWithBackup.clear();
|
|
80
|
+
console.log(storeWithBackup.store.data); // Output: { data: 'root' }
|
|
81
|
+
```
|
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/* @nexim/alpine v0.0.1 */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/main.ts
|
|
32
|
+
var main_exports = {};
|
|
33
|
+
__export(main_exports, {
|
|
34
|
+
AlpineStoreWithBackup: () => AlpineStoreWithBackup,
|
|
35
|
+
alpineStoreGenerator: () => alpineStoreGenerator
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(main_exports);
|
|
38
|
+
var import_package_tracer = require("@alwatr/package-tracer");
|
|
39
|
+
|
|
40
|
+
// src/store/store-generator.ts
|
|
41
|
+
var import_logger = require("@alwatr/logger");
|
|
42
|
+
var import_alpinejs = __toESM(require("alpinejs"), 1);
|
|
43
|
+
var logger = (0, import_logger.createLogger)("@nexim/alpine");
|
|
44
|
+
function alpineStoreGenerator(config) {
|
|
45
|
+
logger.logMethodArgs?.("alpineStoreGenerator", config);
|
|
46
|
+
import_alpinejs.default.store(config.name, config.defaultValue);
|
|
47
|
+
const store = import_alpinejs.default.store(config.name);
|
|
48
|
+
return store;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/store/store-with-backup.ts
|
|
52
|
+
var import_local_storage = require("@alwatr/local-storage");
|
|
53
|
+
var import_logger2 = require("@alwatr/logger");
|
|
54
|
+
var import_parse_duration = require("@alwatr/parse-duration");
|
|
55
|
+
var schemaVersion = 1;
|
|
56
|
+
var AlpineStoreWithBackup = class {
|
|
57
|
+
/**
|
|
58
|
+
* Provides a Alpine.js store implementation with backup and expiration.
|
|
59
|
+
*
|
|
60
|
+
* @param {AlpineStoreWithBackupOptions} config__ - Configuration object.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* import {AlpineStoreWithBackup} from '@nexim/alpine';
|
|
64
|
+
*
|
|
65
|
+
* const storeWithBackup = new AlpineStoreWithBackup({
|
|
66
|
+
* name: 'myStoreWithBackup',
|
|
67
|
+
* version: 1,
|
|
68
|
+
* defaultValue: {data: 'root'},
|
|
69
|
+
* expireDuration: '1d',
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* storeWithBackup.store.data = 'user';
|
|
73
|
+
*
|
|
74
|
+
* storeWithBackup.save();
|
|
75
|
+
* console.log(storeWithBackup.store.data); // Output: { data: 'user' }
|
|
76
|
+
*
|
|
77
|
+
* storeWithBackup.clear();
|
|
78
|
+
* console.log(storeWithBackup.store.data); // Output: { data: 'root' }
|
|
79
|
+
*/
|
|
80
|
+
constructor(config__) {
|
|
81
|
+
this.config__ = config__;
|
|
82
|
+
/**
|
|
83
|
+
* Keys for storing data and expire time in local storage with version.
|
|
84
|
+
*/
|
|
85
|
+
this.localStorageKey__ = {
|
|
86
|
+
data: `[${"@nexim/alpine"}:data:sv${schemaVersion}]:${this.config__.name}`,
|
|
87
|
+
expireTime: `[${"@nexim/alpine"}:expire-time:sv${schemaVersion}]:${this.config__.name}`
|
|
88
|
+
};
|
|
89
|
+
this.logger_ = (0, import_logger2.createLogger)(`[${"@nexim/alpine"}]:${config__.name}`);
|
|
90
|
+
this.logger_.logMethodArgs?.("constructor", config__);
|
|
91
|
+
this.store = config__.defaultValue;
|
|
92
|
+
if (this.config__.expireDuration !== null) {
|
|
93
|
+
this.handleDataExpiration__();
|
|
94
|
+
}
|
|
95
|
+
this.load__();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Saves the current data to local storage. If the data is null, it clears the stored data.
|
|
99
|
+
*
|
|
100
|
+
* Also updates the expiration time.
|
|
101
|
+
*/
|
|
102
|
+
save() {
|
|
103
|
+
this.logger_.logMethodArgs?.("save", { data: this.store.data });
|
|
104
|
+
if (this.store.data === null) {
|
|
105
|
+
this.clear();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
import_local_storage.localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);
|
|
109
|
+
this.updateExpireTime__();
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Clears the stored data.
|
|
113
|
+
*/
|
|
114
|
+
clear() {
|
|
115
|
+
this.logger_.logMethod?.("clear");
|
|
116
|
+
import_local_storage.localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);
|
|
117
|
+
import_local_storage.localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);
|
|
118
|
+
this.store = this.config__.defaultValue;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Handles the expiration duration by checking if the stored data has expired.
|
|
122
|
+
* If expired, it clears the stored data.
|
|
123
|
+
*/
|
|
124
|
+
handleDataExpiration__() {
|
|
125
|
+
this.logger_.logMethod?.("handleDataExpiration__");
|
|
126
|
+
const expireDuration = import_local_storage.localJsonStorage.getItem(
|
|
127
|
+
this.localStorageKey__.expireTime,
|
|
128
|
+
{ time: 0 },
|
|
129
|
+
this.config__.version
|
|
130
|
+
).time;
|
|
131
|
+
if (expireDuration < Date.now()) {
|
|
132
|
+
this.clear();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Loads data from local storage and updates the store's data.
|
|
137
|
+
*
|
|
138
|
+
* When data is not found or invalid in local storage, it uses the default value.
|
|
139
|
+
*/
|
|
140
|
+
load__() {
|
|
141
|
+
this.logger_.logMethod?.("load__");
|
|
142
|
+
const newData = import_local_storage.localJsonStorage.getItem(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);
|
|
143
|
+
this.store.data = newData.data;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Updates the expiration time in local storage to the current time plus the configured expiration duration.
|
|
147
|
+
*/
|
|
148
|
+
updateExpireTime__() {
|
|
149
|
+
if (this.config__.expireDuration == null) return;
|
|
150
|
+
this.logger_.logMethod?.("updateExpireTime__");
|
|
151
|
+
const newExpireTime = Date.now() + (0, import_parse_duration.parseDuration)(this.config__.expireDuration);
|
|
152
|
+
import_local_storage.localJsonStorage.setItem(this.localStorageKey__.expireTime, { time: newExpireTime }, this.config__.version);
|
|
153
|
+
this.logger_.logOther?.("updated_expire_time", { newExpireTime });
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/main.ts
|
|
158
|
+
__dev_mode__: import_package_tracer.packageTracer.add("@nexim/alpine", "0.0.1");
|
|
159
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
160
|
+
0 && (module.exports = {
|
|
161
|
+
AlpineStoreWithBackup,
|
|
162
|
+
alpineStoreGenerator
|
|
163
|
+
});
|
|
164
|
+
/*! For license information please see main.cjs.LEGAL.txt */
|
|
165
|
+
//# sourceMappingURL=main.cjs.map
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store-with-backup.ts"],
|
|
4
|
+
"sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store-with-backup.js';\n", "import {createLogger} from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @template T - The type of the default value.\n * @property {string} name - The name of the store.\n * @property {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @template T - The type of the data.\n * @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.\n * @returns {T} - The initialized store instance.\n *\n * @example\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *\n * @description\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see https://alpinejs.dev/globals/alpine-store\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', config);\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import {localJsonStorage} from '@alwatr/local-storage';\nimport {type AlwatrLogger, createLogger} from '@alwatr/logger';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n data: DictionaryReq<any> | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {number} version - The version of the store.\n * @param {T} defaultValue - The default value of the store.\n * @param {Duration} [expireDuration] - Optional. The duration after which the store expires.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n name: string;\n version: number;\n defaultValue: T;\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param {AlpineStoreWithBackupOptions} config__ - Configuration object.\n *\n * @example\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n this.logger_ = createLogger(`[${__package_name__}]:${config__.name}`);\n this.logger_.logMethodArgs?.('constructor', config__);\n\n this.store = config__.defaultValue;\n\n if (this.config__.expireDuration !== null) {\n this.handleDataExpiration__();\n }\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', {data: this.store.data});\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n const expireDuration = localJsonStorage.getItem<{time: number}>(\n this.localStorageKey__.expireTime,\n {time: 0},\n this.config__.version,\n ).time;\n\n if (expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<T>(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);\n this.store.data = newData.data;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, {time: newExpireTime}, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', {newExpireTime});\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;;;ACA5B,oBAA2B;A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":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAIA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC"}
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/* @nexim/alpine v0.0.1 */
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
import { packageTracer } from "@alwatr/package-tracer";
|
|
5
|
+
|
|
6
|
+
// src/store/store-generator.ts
|
|
7
|
+
import { createLogger } from "@alwatr/logger";
|
|
8
|
+
import alpine from "alpinejs";
|
|
9
|
+
var logger = createLogger("@nexim/alpine");
|
|
10
|
+
function alpineStoreGenerator(config) {
|
|
11
|
+
logger.logMethodArgs?.("alpineStoreGenerator", config);
|
|
12
|
+
alpine.store(config.name, config.defaultValue);
|
|
13
|
+
const store = alpine.store(config.name);
|
|
14
|
+
return store;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/store/store-with-backup.ts
|
|
18
|
+
import { localJsonStorage } from "@alwatr/local-storage";
|
|
19
|
+
import { createLogger as createLogger2 } from "@alwatr/logger";
|
|
20
|
+
import { parseDuration } from "@alwatr/parse-duration";
|
|
21
|
+
var schemaVersion = 1;
|
|
22
|
+
var AlpineStoreWithBackup = class {
|
|
23
|
+
/**
|
|
24
|
+
* Provides a Alpine.js store implementation with backup and expiration.
|
|
25
|
+
*
|
|
26
|
+
* @param {AlpineStoreWithBackupOptions} config__ - Configuration object.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import {AlpineStoreWithBackup} from '@nexim/alpine';
|
|
30
|
+
*
|
|
31
|
+
* const storeWithBackup = new AlpineStoreWithBackup({
|
|
32
|
+
* name: 'myStoreWithBackup',
|
|
33
|
+
* version: 1,
|
|
34
|
+
* defaultValue: {data: 'root'},
|
|
35
|
+
* expireDuration: '1d',
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* storeWithBackup.store.data = 'user';
|
|
39
|
+
*
|
|
40
|
+
* storeWithBackup.save();
|
|
41
|
+
* console.log(storeWithBackup.store.data); // Output: { data: 'user' }
|
|
42
|
+
*
|
|
43
|
+
* storeWithBackup.clear();
|
|
44
|
+
* console.log(storeWithBackup.store.data); // Output: { data: 'root' }
|
|
45
|
+
*/
|
|
46
|
+
constructor(config__) {
|
|
47
|
+
this.config__ = config__;
|
|
48
|
+
/**
|
|
49
|
+
* Keys for storing data and expire time in local storage with version.
|
|
50
|
+
*/
|
|
51
|
+
this.localStorageKey__ = {
|
|
52
|
+
data: `[${"@nexim/alpine"}:data:sv${schemaVersion}]:${this.config__.name}`,
|
|
53
|
+
expireTime: `[${"@nexim/alpine"}:expire-time:sv${schemaVersion}]:${this.config__.name}`
|
|
54
|
+
};
|
|
55
|
+
this.logger_ = createLogger2(`[${"@nexim/alpine"}]:${config__.name}`);
|
|
56
|
+
this.logger_.logMethodArgs?.("constructor", config__);
|
|
57
|
+
this.store = config__.defaultValue;
|
|
58
|
+
if (this.config__.expireDuration !== null) {
|
|
59
|
+
this.handleDataExpiration__();
|
|
60
|
+
}
|
|
61
|
+
this.load__();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Saves the current data to local storage. If the data is null, it clears the stored data.
|
|
65
|
+
*
|
|
66
|
+
* Also updates the expiration time.
|
|
67
|
+
*/
|
|
68
|
+
save() {
|
|
69
|
+
this.logger_.logMethodArgs?.("save", { data: this.store.data });
|
|
70
|
+
if (this.store.data === null) {
|
|
71
|
+
this.clear();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);
|
|
75
|
+
this.updateExpireTime__();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Clears the stored data.
|
|
79
|
+
*/
|
|
80
|
+
clear() {
|
|
81
|
+
this.logger_.logMethod?.("clear");
|
|
82
|
+
localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);
|
|
83
|
+
localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);
|
|
84
|
+
this.store = this.config__.defaultValue;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Handles the expiration duration by checking if the stored data has expired.
|
|
88
|
+
* If expired, it clears the stored data.
|
|
89
|
+
*/
|
|
90
|
+
handleDataExpiration__() {
|
|
91
|
+
this.logger_.logMethod?.("handleDataExpiration__");
|
|
92
|
+
const expireDuration = localJsonStorage.getItem(
|
|
93
|
+
this.localStorageKey__.expireTime,
|
|
94
|
+
{ time: 0 },
|
|
95
|
+
this.config__.version
|
|
96
|
+
).time;
|
|
97
|
+
if (expireDuration < Date.now()) {
|
|
98
|
+
this.clear();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Loads data from local storage and updates the store's data.
|
|
103
|
+
*
|
|
104
|
+
* When data is not found or invalid in local storage, it uses the default value.
|
|
105
|
+
*/
|
|
106
|
+
load__() {
|
|
107
|
+
this.logger_.logMethod?.("load__");
|
|
108
|
+
const newData = localJsonStorage.getItem(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);
|
|
109
|
+
this.store.data = newData.data;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Updates the expiration time in local storage to the current time plus the configured expiration duration.
|
|
113
|
+
*/
|
|
114
|
+
updateExpireTime__() {
|
|
115
|
+
if (this.config__.expireDuration == null) return;
|
|
116
|
+
this.logger_.logMethod?.("updateExpireTime__");
|
|
117
|
+
const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);
|
|
118
|
+
localJsonStorage.setItem(this.localStorageKey__.expireTime, { time: newExpireTime }, this.config__.version);
|
|
119
|
+
this.logger_.logOther?.("updated_expire_time", { newExpireTime });
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/main.ts
|
|
124
|
+
__dev_mode__: packageTracer.add("@nexim/alpine", "0.0.1");
|
|
125
|
+
export {
|
|
126
|
+
AlpineStoreWithBackup,
|
|
127
|
+
alpineStoreGenerator
|
|
128
|
+
};
|
|
129
|
+
/*! For license information please see main.mjs.LEGAL.txt */
|
|
130
|
+
//# sourceMappingURL=main.mjs.map
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store-with-backup.ts"],
|
|
4
|
+
"sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store-with-backup.js';\n", "import {createLogger} from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @template T - The type of the default value.\n * @property {string} name - The name of the store.\n * @property {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @template T - The type of the data.\n * @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.\n * @returns {T} - The initialized store instance.\n *\n * @example\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *\n * @description\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see https://alpinejs.dev/globals/alpine-store\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', config);\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import {localJsonStorage} from '@alwatr/local-storage';\nimport {type AlwatrLogger, createLogger} from '@alwatr/logger';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n data: DictionaryReq<any> | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {number} version - The version of the store.\n * @param {T} defaultValue - The default value of the store.\n * @param {Duration} [expireDuration] - Optional. The duration after which the store expires.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n name: string;\n version: number;\n defaultValue: T;\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param {AlpineStoreWithBackupOptions} config__ - Configuration object.\n *\n * @example\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n this.logger_ = createLogger(`[${__package_name__}]:${config__.name}`);\n this.logger_.logMethodArgs?.('constructor', config__);\n\n this.store = config__.defaultValue;\n\n if (this.config__.expireDuration !== null) {\n this.handleDataExpiration__();\n }\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', {data: this.store.data});\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n const expireDuration = localJsonStorage.getItem<{time: number}>(\n this.localStorageKey__.expireTime,\n {time: 0},\n this.config__.version,\n ).time;\n\n if (expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<T>(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);\n this.store.data = newData.data;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, {time: newExpireTime}, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', {newExpireTime});\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAAA,SAAQ,qBAAoB;;;ACA5B,SAAQ,oBAAmB;AAC3B,OAAO,YAAY;AAE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+
*
|
|
4
|
+
* @template T - The type of the default value.
|
|
5
|
+
* @property {string} name - The name of the store.
|
|
6
|
+
* @property {T} defaultValue - The default value of the store.
|
|
7
|
+
*/
|
|
8
|
+
export type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {
|
|
9
|
+
name: string;
|
|
10
|
+
defaultValue: T;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Generates an Alpine.js store with default value.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of the data.
|
|
16
|
+
* @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.
|
|
17
|
+
* @returns {T} - The initialized store instance.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const store = alpineStoreGenerator({
|
|
21
|
+
* name: 'user',
|
|
22
|
+
* defaultValue: {type: 'root'},
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* console.log(store.type); // Output: root
|
|
26
|
+
*
|
|
27
|
+
* @description
|
|
28
|
+
* This function uses Alpine.js to create a reactive store with a default value.
|
|
29
|
+
* The store is identified by a unique name and can be accessed and manipulated
|
|
30
|
+
* throughout the application. Alpine.js stores provide a simple way to manage
|
|
31
|
+
* data in your application, making it easy to keep your UI in sync with your data.
|
|
32
|
+
*
|
|
33
|
+
* @see https://alpinejs.dev/globals/alpine-store
|
|
34
|
+
*/
|
|
35
|
+
export declare function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T;
|
|
36
|
+
//# sourceMappingURL=store-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-generator.d.ts","sourceRoot":"","sources":["../../src/store/store-generator.ts"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,aAAa,IAAI;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,CAAC,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,aAAa,EAAE,MAAM,EAAE,2BAA2B,CAAC,CAAC,CAAC,GAAG,CAAC,CAQvG"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type AlwatrLogger } from '@alwatr/logger';
|
|
2
|
+
import { type Duration } from '@alwatr/parse-duration';
|
|
3
|
+
/**
|
|
4
|
+
* Type for the store's data to extends from them.
|
|
5
|
+
*/
|
|
6
|
+
export type AlpineStoreWithBackupType = {
|
|
7
|
+
data: DictionaryReq<any> | null;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* AlpineStoreWithBackup Options.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of the store value.
|
|
13
|
+
* @param {string} name - The name of the store.
|
|
14
|
+
* @param {number} version - The version of the store.
|
|
15
|
+
* @param {T} defaultValue - The default value of the store.
|
|
16
|
+
* @param {Duration} [expireDuration] - Optional. The duration after which the store expires.
|
|
17
|
+
*/
|
|
18
|
+
export type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {
|
|
19
|
+
name: string;
|
|
20
|
+
version: number;
|
|
21
|
+
defaultValue: T;
|
|
22
|
+
expireDuration?: Duration;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Provides a Alpine.js store implementation with backup and expiration.
|
|
26
|
+
*/
|
|
27
|
+
export declare class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> {
|
|
28
|
+
private config__;
|
|
29
|
+
/**
|
|
30
|
+
* The store's data.
|
|
31
|
+
*/
|
|
32
|
+
store: T;
|
|
33
|
+
protected logger_: AlwatrLogger;
|
|
34
|
+
/**
|
|
35
|
+
* Keys for storing data and expire time in local storage with version.
|
|
36
|
+
*/
|
|
37
|
+
private localStorageKey__;
|
|
38
|
+
/**
|
|
39
|
+
* Provides a Alpine.js store implementation with backup and expiration.
|
|
40
|
+
*
|
|
41
|
+
* @param {AlpineStoreWithBackupOptions} config__ - Configuration object.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* import {AlpineStoreWithBackup} from '@nexim/alpine';
|
|
45
|
+
*
|
|
46
|
+
* const storeWithBackup = new AlpineStoreWithBackup({
|
|
47
|
+
* name: 'myStoreWithBackup',
|
|
48
|
+
* version: 1,
|
|
49
|
+
* defaultValue: {data: 'root'},
|
|
50
|
+
* expireDuration: '1d',
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* storeWithBackup.store.data = 'user';
|
|
54
|
+
*
|
|
55
|
+
* storeWithBackup.save();
|
|
56
|
+
* console.log(storeWithBackup.store.data); // Output: { data: 'user' }
|
|
57
|
+
*
|
|
58
|
+
* storeWithBackup.clear();
|
|
59
|
+
* console.log(storeWithBackup.store.data); // Output: { data: 'root' }
|
|
60
|
+
*/
|
|
61
|
+
constructor(config__: AlpineStoreWithBackupOptions<T>);
|
|
62
|
+
/**
|
|
63
|
+
* Saves the current data to local storage. If the data is null, it clears the stored data.
|
|
64
|
+
*
|
|
65
|
+
* Also updates the expiration time.
|
|
66
|
+
*/
|
|
67
|
+
save(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Clears the stored data.
|
|
70
|
+
*/
|
|
71
|
+
clear(): void;
|
|
72
|
+
/**
|
|
73
|
+
* Handles the expiration duration by checking if the stored data has expired.
|
|
74
|
+
* If expired, it clears the stored data.
|
|
75
|
+
*/
|
|
76
|
+
private handleDataExpiration__;
|
|
77
|
+
/**
|
|
78
|
+
* Loads data from local storage and updates the store's data.
|
|
79
|
+
*
|
|
80
|
+
* When data is not found or invalid in local storage, it uses the default value.
|
|
81
|
+
*/
|
|
82
|
+
private load__;
|
|
83
|
+
/**
|
|
84
|
+
* Updates the expiration time in local storage to the current time plus the configured expiration duration.
|
|
85
|
+
*/
|
|
86
|
+
private updateExpireTime__;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=store-with-backup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-with-backup.d.ts","sourceRoot":"","sources":["../../src/store/store-with-backup.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,KAAK,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAgB,KAAK,QAAQ,EAAC,MAAM,wBAAwB,CAAC;AAEpE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IAEtC,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;CACjC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,4BAA4B,CAAC,CAAC,SAAS,yBAAyB,IAAI;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,CAAC,CAAC;IAChB,cAAc,CAAC,EAAE,QAAQ,CAAC;CAC3B,CAAC;AASF;;GAEG;AACH,qBAAa,qBAAqB,CAAC,CAAC,SAAS,yBAAyB;IAuCxD,OAAO,CAAC,QAAQ;IAtC5B;;OAEG;IACH,KAAK,EAAE,CAAC,CAAC;IAET,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC;IAEhC;;OAEG;IACH,OAAO,CAAC,iBAAiB,CAGvB;IAEF;;;;;;;;;;;;;;;;;;;;;;OAsBG;gBACiB,QAAQ,EAAE,4BAA4B,CAAC,CAAC,CAAC;IAY7D;;;;OAIG;IACH,IAAI,IAAI,IAAI;IAYZ;;OAEG;IACH,KAAK,IAAI,IAAI;IASb;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAc9B;;;;OAIG;IACH,OAAO,CAAC,MAAM;IAOd;;OAEG;IACH,OAAO,CAAC,kBAAkB;CAS3B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nexim/alpine",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Utility functions to enhance Alpine.js usage with backup support.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"alpinejs",
|
|
7
|
+
"alpine",
|
|
8
|
+
"context",
|
|
9
|
+
"typescript",
|
|
10
|
+
"nexim"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/the-nexim/nanolib/tree/next/packages/alpine#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/the-nexim/nanolib/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/the-nexim/nanolib",
|
|
19
|
+
"directory": "packages/alpine"
|
|
20
|
+
},
|
|
21
|
+
"license": "AGPL-3.0-only",
|
|
22
|
+
"author": "S. Amir Mohammad Najafi <njfamirm@gmail.com> (www.njfamirm.ir)",
|
|
23
|
+
"contributors": [
|
|
24
|
+
"Arash Ghardashpoor <arash.qardashpoor@gmail.com> (https://www.agpagp.ir)"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/main.d.ts",
|
|
30
|
+
"import": "./dist/main.mjs",
|
|
31
|
+
"require": "./dist/main.cjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"main": "./dist/main.cjs",
|
|
35
|
+
"module": "./dist/main.mjs",
|
|
36
|
+
"types": "./dist/main.d.ts",
|
|
37
|
+
"files": [
|
|
38
|
+
"**/*.{js,mjs,cjs,map,d.ts,html,md,LEGAL.txt}",
|
|
39
|
+
"LICENSE",
|
|
40
|
+
"!**/*.test.js",
|
|
41
|
+
"!demo/**/*"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"b": "yarn run build",
|
|
45
|
+
"build": "yarn run build:ts && yarn run build:es",
|
|
46
|
+
"build:es": "nano-build --preset=module",
|
|
47
|
+
"build:ts": "tsc --build",
|
|
48
|
+
"c": "yarn run clean",
|
|
49
|
+
"cb": "yarn run clean && yarn run build",
|
|
50
|
+
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
51
|
+
"d": "yarn run build:es && yarn node --enable-source-maps --trace-warnings",
|
|
52
|
+
"t": "yarn run test",
|
|
53
|
+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\" ava",
|
|
54
|
+
"w": "yarn run watch",
|
|
55
|
+
"watch": "yarn run watch:ts & yarn run watch:es",
|
|
56
|
+
"watch:es": "yarn run build:es --watch",
|
|
57
|
+
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@alwatr/local-storage": "^5.0.0",
|
|
61
|
+
"@alwatr/logger": "^5.0.0",
|
|
62
|
+
"@alwatr/package-tracer": "^5.0.0",
|
|
63
|
+
"@alwatr/parse-duration": "^5.0.0",
|
|
64
|
+
"alpinejs": "^3.14.7"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@alwatr/nano-build": "^5.0.0",
|
|
68
|
+
"@alwatr/type-helper": "^5.0.0",
|
|
69
|
+
"@nexim/typescript-config": "^1.0.1",
|
|
70
|
+
"@types/alpinejs": "^3.13.11",
|
|
71
|
+
"ava": "^6.2.0",
|
|
72
|
+
"typescript": "^5.6.3"
|
|
73
|
+
},
|
|
74
|
+
"publishConfig": {
|
|
75
|
+
"access": "public"
|
|
76
|
+
},
|
|
77
|
+
"gitHead": "a02afd5953a471c872d63e4eb2e7ccbf90c21abb"
|
|
78
|
+
}
|