@leexi/shared 0.3.2 → 0.3.4
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 +67 -0
- package/dist/module.json +1 -1
- package/dist/runtime/composables/useLocalStorage.js +30 -1
- package/dist/runtime/utils/records/index.d.ts +2 -0
- package/dist/runtime/utils/records/index.js +2 -0
- package/dist/runtime/utils/records/omit.d.ts +9 -0
- package/dist/runtime/utils/records/omit.js +1 -0
- package/dist/runtime/utils/records/pick.d.ts +9 -0
- package/dist/runtime/utils/records/pick.js +1 -0
- package/dist/runtime/utils/strings/camelcase.d.ts +8 -0
- package/dist/runtime/utils/strings/camelcase.js +1 -0
- package/dist/runtime/utils/strings/index.d.ts +3 -0
- package/dist/runtime/utils/strings/index.js +3 -0
- package/dist/runtime/utils/strings/kebabcase.d.ts +8 -0
- package/dist/runtime/utils/strings/kebabcase.js +1 -0
- package/dist/runtime/utils/strings/snakecase.d.ts +8 -0
- package/dist/runtime/utils/strings/snakecase.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,8 +66,13 @@ export default defineNuxtConfig({
|
|
|
66
66
|
- [Utils](#utils)
|
|
67
67
|
- [Records](#records)
|
|
68
68
|
- [set](#set)
|
|
69
|
+
- [omit](#omit)
|
|
70
|
+
- [pick](#pick)
|
|
69
71
|
- [Strings](#strings)
|
|
72
|
+
- [camelcase](#camelcase)
|
|
70
73
|
- [capitalize](#capitalize)
|
|
74
|
+
- [kebabcase](#kebabcase)
|
|
75
|
+
- [snakecase](#snakecase)
|
|
71
76
|
- [Objects](#objects)
|
|
72
77
|
- [deepDup](#deepdup)
|
|
73
78
|
- [isSame](#issame)
|
|
@@ -104,7 +109,45 @@ set(foo, ['bar', 'biz'], 'fiz')
|
|
|
104
109
|
foo // => { bar: { biz: 'fiz' } }
|
|
105
110
|
```
|
|
106
111
|
|
|
112
|
+
#### omit
|
|
113
|
+
Returns a copy of a given object excluding a given set of attributes
|
|
114
|
+
- Type
|
|
115
|
+
```ts
|
|
116
|
+
const omit: <T extends Record<string, unknown>, K extends (keyof T)[]>(record: T, keys: K) => Omit<T, K[number]>;
|
|
117
|
+
```
|
|
118
|
+
- Example
|
|
119
|
+
```ts
|
|
120
|
+
const foo = { a: 1, b: 2, c: 3 }
|
|
121
|
+
const bar = omit(foo, ['b', 'c'])
|
|
122
|
+
bar // => { a: 1 }
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### pick
|
|
126
|
+
Returns a copy of a given object only including a given set of attributes
|
|
127
|
+
- Type
|
|
128
|
+
```ts
|
|
129
|
+
const pick: <T extends Record<string, unknown>, K extends (keyof T)[]>(record: T, keys: K) => Pick<T, K[number]>;
|
|
130
|
+
```
|
|
131
|
+
- Example
|
|
132
|
+
```ts
|
|
133
|
+
const foo = { a: 1, b: 2, c: 3 }
|
|
134
|
+
const bar = pick(foo, ['b', 'c'])
|
|
135
|
+
bar // => { b: 2, c: 3 }
|
|
136
|
+
```
|
|
137
|
+
|
|
107
138
|
### Strings
|
|
139
|
+
#### camelcase
|
|
140
|
+
Convert a kebab-case string or a snake_case string to a camelCase string
|
|
141
|
+
- Type
|
|
142
|
+
```ts
|
|
143
|
+
const camelcase: (string: string) => string;
|
|
144
|
+
```
|
|
145
|
+
- Example
|
|
146
|
+
```ts
|
|
147
|
+
camelcase('foo_bar') // => 'fooBar'
|
|
148
|
+
camelcase('foo-bar') // => 'fooBar'
|
|
149
|
+
```
|
|
150
|
+
|
|
108
151
|
#### capitalize
|
|
109
152
|
Capitalizes a string
|
|
110
153
|
- Type
|
|
@@ -116,6 +159,30 @@ const capitalize: (string?: string) => string;
|
|
|
116
159
|
capitalize('foo') // => 'Foo'
|
|
117
160
|
```
|
|
118
161
|
|
|
162
|
+
#### kebabcase
|
|
163
|
+
Convert a camelCase string or a snake_case string to a kebab-case string
|
|
164
|
+
- Type
|
|
165
|
+
```ts
|
|
166
|
+
const kebabcase: (string: string) => string;
|
|
167
|
+
```
|
|
168
|
+
- Example
|
|
169
|
+
```ts
|
|
170
|
+
kebabcase('fooBar') // => 'foo-bar'
|
|
171
|
+
kebabcase('foo_bar') // => 'foo-bar'
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
#### snakecase
|
|
175
|
+
Convert a camelCase string or a kebab-case string to a snake_case string
|
|
176
|
+
- Type
|
|
177
|
+
```ts
|
|
178
|
+
const snakecase: (string: string) => string;
|
|
179
|
+
```
|
|
180
|
+
- Example
|
|
181
|
+
```ts
|
|
182
|
+
snakecase('fooBar') // => 'foo_bar'
|
|
183
|
+
snakecase('foo-bar') // => 'foo_bar'
|
|
184
|
+
```
|
|
185
|
+
|
|
119
186
|
### Objects
|
|
120
187
|
#### deepDup
|
|
121
188
|
Deeply duplicates an object.
|
package/dist/module.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
import { useState, watch } from "#imports";
|
|
2
|
+
import { kebabcase, snakecase } from "../utils/index.js";
|
|
2
3
|
export const useLocalStorage = (key, defaultValue) => {
|
|
3
|
-
|
|
4
|
+
if (/[-_][a-z]/.test(key)) {
|
|
5
|
+
throw new Error(`[useLocalStorage]: prefer camelCase fro key \`${key}\``);
|
|
6
|
+
}
|
|
7
|
+
const _findLocalStorageValue = () => {
|
|
8
|
+
if (localStorage?.getItem(key)) {
|
|
9
|
+
return localStorage.getItem(key);
|
|
10
|
+
}
|
|
11
|
+
for (const caseKey of [key, kebabcase(key), snakecase(key)]) {
|
|
12
|
+
if (localStorage?.getItem(caseKey)) {
|
|
13
|
+
localStorage.setItem(key, localStorage.getItem(caseKey));
|
|
14
|
+
localStorage.removeItem(caseKey);
|
|
15
|
+
return localStorage.getItem(key);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return "null";
|
|
19
|
+
};
|
|
20
|
+
const _parseLocalStorageValue = () => {
|
|
21
|
+
try {
|
|
22
|
+
const value = JSON.parse(_findLocalStorageValue());
|
|
23
|
+
return value || defaultValue;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error instanceof Error && /is not valid JSON/.test(error.message)) {
|
|
26
|
+
localStorage.setItem(key, JSON.stringify(localStorage.getItem(key)));
|
|
27
|
+
return _parseLocalStorageValue();
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const data = useState(key, _parseLocalStorageValue);
|
|
4
33
|
watch(data, () => {
|
|
5
34
|
if (data.value) {
|
|
6
35
|
localStorage?.setItem(key, JSON.stringify(data.value));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a copy of a given object excluding a given set of attributes
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const foo = { a: 1, b: 2, c: 3 }
|
|
6
|
+
* const bar = omit(foo, ['b', 'c'])
|
|
7
|
+
* bar // => { a: 1 }
|
|
8
|
+
*/
|
|
9
|
+
export declare const omit: <T extends Record<string, unknown>, K extends (keyof T)[]>(record: T, keys: K) => Omit<T, K[number]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const omit = (record, keys) => Object.fromEntries(Object.entries(record).filter(([key]) => !keys.includes(key)));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a copy of a given object only including a given set of attributes
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const foo = { a: 1, b: 2, c: 3 }
|
|
6
|
+
* const bar = pick(foo, ['b', 'c'])
|
|
7
|
+
* bar // => { b: 2, c: 3 }
|
|
8
|
+
*/
|
|
9
|
+
export declare const pick: <T extends Record<string, unknown>, K extends (keyof T)[]>(record: T, keys: K) => Pick<T, K[number]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const pick = (record, keys) => Object.fromEntries(Object.entries(record).filter(([key]) => keys.includes(key)));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const camelcase = (string) => string.replaceAll(/[-_][a-z]/g, (char) => char.toUpperCase().replace("_", "").replace("-", ""));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const kebabcase = (string) => string.replaceAll(/[A-Z]|_[a-z]/g, (char) => `-${char.toLowerCase().replace("_", "")}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const snakecase = (string) => string.replaceAll(/[A-Z]|-[a-z]/g, (char) => `_${char.toLowerCase().replace("-", "")}`);
|