@gradin/vue-utils 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/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # vue-utils
2
+
3
+ Useful utilities for Vue projects.
4
+
5
+ [![npm (scoped)](https://img.shields.io/npm/v/@gradin/vue-utils)](https://www.npmjs.com/package/@gradin/vue-utils)
6
+ ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@gradin/vue-utils)
7
+ ![npm](https://img.shields.io/npm/dt/@gradin/vue-utils)
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ # Using npm
13
+ npm install @gradin/vue-utils
14
+
15
+ # Using Yarn
16
+ yarn add @gradin/vue-utils
17
+ ```
18
+
19
+ ## Featurs
20
+
21
+ ### Improved Vue Reactive
22
+
23
+ The `gReactive` function is a wrapper around Vue's `reactive` function that adds some useful features.
24
+ - **Reset**: The `reset` method resets the reactive object to its initial state.
25
+ - **Set**: The `set` method sets the reactive object to a new value, merging the new value with the existing value.
26
+
27
+ Example `reactive` vs `gReactive`
28
+
29
+ <details>
30
+ <summary>Show Codes</summary>
31
+
32
+ ```typescript
33
+ // before
34
+ import { reactive } from 'vue';
35
+ const form = reactive({
36
+ name: '',
37
+ sku: '',
38
+ description: '',
39
+ price: 0,
40
+ category_id: <number|null> null,
41
+ })
42
+
43
+ const resetForm = () => {
44
+ form.name = '';
45
+ form.sku = '';
46
+ form.description = '';
47
+ form.price = 0;
48
+ form.category_id = null;
49
+ }
50
+
51
+ const setFormForUpdate = (product: Product) => {
52
+ form.name = product.name;
53
+ form.sku = product.sku;
54
+ form.description = product.description;
55
+ form.price = product.price;
56
+ form.category_id = product.category_id;
57
+ }
58
+
59
+ const submitForm = () => {
60
+ axios.post('/api/products', form)
61
+ .then(response => {
62
+ // handle success
63
+ })
64
+ }
65
+
66
+ // after
67
+ import { gReactive } from '@gradin/vue-utils';
68
+ const form = gReactive({ // almost the same as vue reactive
69
+ name: '',
70
+ sku: '',
71
+ description: '',
72
+ price: 0,
73
+ category_id: <number|null> null,
74
+ })
75
+ form.reset(); // reset the form
76
+ form.set(product); // set the form for update
77
+
78
+ const submitForm = () => { // exactly the same as before
79
+ axios.post('/api/products', form)
80
+ .then(response => {
81
+ // handle success
82
+ })
83
+ }
84
+ ```
85
+ </details>
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import { reactive, watch } from "vue";
2
+ import { useRoute } from 'vue-router';
3
+ export const resetableReactive = (data) => {
4
+ const initialValue = Object.assign({}, data);
5
+ const form = reactive(data);
6
+ Object.defineProperty(form, 'reset', {
7
+ value: () => {
8
+ Object.assign(form, initialValue);
9
+ },
10
+ enumerable: false, // <== this hides it from JSON.stringify and Object.keys
11
+ });
12
+ Object.defineProperty(form, 'set', {
13
+ value: (newValue) => {
14
+ for (const key in initialValue) {
15
+ if (key in newValue) {
16
+ form[key] = newValue[key];
17
+ }
18
+ }
19
+ },
20
+ enumerable: false, // <== this hides it from JSON.stringify and Object.keys
21
+ });
22
+ return form;
23
+ };
24
+ /**
25
+ * Watch the route full path, and trigger callback when query / params change
26
+ * but don't trigger when route name change (move away to other page).
27
+ * Optional watchSource to watch just specific part of the route (not fullPath).
28
+ */
29
+ export const whenRouteChange = (callback, watchSource) => {
30
+ const route = useRoute();
31
+ const routeName = route.name; // cache route name
32
+ watch(watchSource !== null && watchSource !== void 0 ? watchSource : (() => route.fullPath), () => {
33
+ if (route.name === routeName) {
34
+ callback();
35
+ }
36
+ }, { immediate: true });
37
+ };
@@ -0,0 +1,10 @@
1
+ export declare const resetableReactive: <T extends object>(data: T) => T & {
2
+ reset: () => void;
3
+ set: (newValue: Partial<T>) => void;
4
+ };
5
+ /**
6
+ * Watch the route full path, and trigger callback when query / params change
7
+ * but don't trigger when route name change (move away to other page).
8
+ * Optional watchSource to watch just specific part of the route (not fullPath).
9
+ */
10
+ export declare const whenRouteChange: (callback: () => void, watchSource?: () => unknown) => void;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@gradin/vue-utils",
3
+ "version": "0.0.1",
4
+ "description": "Useful utilities for Vue projects.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "lint": "eslint . --fix"
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/gradints/vue-utils.git"
18
+ },
19
+ "keywords": [
20
+ "vue",
21
+ "javascript",
22
+ "typescript"
23
+ ],
24
+ "author": "Christhofer",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/gradints/vue-utils/issues"
28
+ },
29
+ "homepage": "https://github.com/gradints/vue-utils#readme",
30
+ "dependencies": {
31
+ "vue": "^3.5.13"
32
+ },
33
+ "devDependencies": {
34
+ "eslint": "^9.26.0",
35
+ "eslint-plugin-simple-import-sort": "^12.1.1",
36
+ "typescript": "^5.8.3",
37
+ "typescript-eslint": "^8.32.0"
38
+ },
39
+ "peerDependencies": {
40
+ "vue-router": "^4.5.1"
41
+ }
42
+ }