@leexi/shared 0.3.1 → 0.3.3
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 +39 -0
- package/dist/module.json +1 -1
- package/dist/runtime/composables/useLocalStorage.js +30 -1
- 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 -3
package/README.md
CHANGED
|
@@ -67,7 +67,10 @@ export default defineNuxtConfig({
|
|
|
67
67
|
- [Records](#records)
|
|
68
68
|
- [set](#set)
|
|
69
69
|
- [Strings](#strings)
|
|
70
|
+
- [camelcase](#camelcase)
|
|
70
71
|
- [capitalize](#capitalize)
|
|
72
|
+
- [kebabcase](#kebabcase)
|
|
73
|
+
- [snakecase](#snakecase)
|
|
71
74
|
- [Objects](#objects)
|
|
72
75
|
- [deepDup](#deepdup)
|
|
73
76
|
- [isSame](#issame)
|
|
@@ -105,6 +108,18 @@ foo // => { bar: { biz: 'fiz' } }
|
|
|
105
108
|
```
|
|
106
109
|
|
|
107
110
|
### Strings
|
|
111
|
+
#### camelcase
|
|
112
|
+
Convert a kebab-case string or a snake_case string to a camelCase string
|
|
113
|
+
- Type
|
|
114
|
+
```ts
|
|
115
|
+
const camelcase: (string: string) => string;
|
|
116
|
+
```
|
|
117
|
+
- Example
|
|
118
|
+
```ts
|
|
119
|
+
camelcase('foo_bar') // => 'fooBar'
|
|
120
|
+
camelcase('foo-bar') // => 'fooBar'
|
|
121
|
+
```
|
|
122
|
+
|
|
108
123
|
#### capitalize
|
|
109
124
|
Capitalizes a string
|
|
110
125
|
- Type
|
|
@@ -116,6 +131,30 @@ const capitalize: (string?: string) => string;
|
|
|
116
131
|
capitalize('foo') // => 'Foo'
|
|
117
132
|
```
|
|
118
133
|
|
|
134
|
+
#### kebabcase
|
|
135
|
+
Convert a camelCase string or a snake_case string to a kebab-case string
|
|
136
|
+
- Type
|
|
137
|
+
```ts
|
|
138
|
+
const kebabcase: (string: string) => string;
|
|
139
|
+
```
|
|
140
|
+
- Example
|
|
141
|
+
```ts
|
|
142
|
+
kebabcase('fooBar') // => 'foo-bar'
|
|
143
|
+
kebabcase('foo_bar') // => 'foo-bar'
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### snakecase
|
|
147
|
+
Convert a camelCase string or a kebab-case string to a snake_case string
|
|
148
|
+
- Type
|
|
149
|
+
```ts
|
|
150
|
+
const snakecase: (string: string) => string;
|
|
151
|
+
```
|
|
152
|
+
- Example
|
|
153
|
+
```ts
|
|
154
|
+
snakecase('fooBar') // => 'foo_bar'
|
|
155
|
+
snakecase('foo-bar') // => 'foo_bar'
|
|
156
|
+
```
|
|
157
|
+
|
|
119
158
|
### Objects
|
|
120
159
|
#### deepDup
|
|
121
160
|
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 @@
|
|
|
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("-", "")}`);
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"license": "UNLICENSED",
|
|
3
3
|
"name": "@leexi/shared",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "0.3.
|
|
5
|
+
"version": "0.3.3",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./composables": {
|
|
8
8
|
"import": "./dist/runtime/composables/index.js",
|
|
@@ -41,9 +41,7 @@
|
|
|
41
41
|
"dev": "nuxi dev playground",
|
|
42
42
|
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
43
43
|
"lint": "eslint .",
|
|
44
|
-
"postinstall": "yarn dev:prepare",
|
|
45
44
|
"prepack": "yarn lint && yarn test --reporter default && yarn tsc && yarn build",
|
|
46
|
-
"release": "npm publish",
|
|
47
45
|
"test": "vitest run",
|
|
48
46
|
"tsc": "vue-tsc --noEmit"
|
|
49
47
|
},
|