Skip to content

Commit 06cfb81

Browse files
committed
feat: add upperAfterPrefix config option
1 parent a7876c4 commit 06cfb81

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ npm i nuxt-lodash -D
2121
2. Add it to the `modules` section of your `nuxt.config`:
2222

2323
```ts
24-
import { defineNuxtConfig } from 'nuxt'
25-
2624
export default defineNuxtConfig({
27-
modules: ['nuxt-lodash']
28-
})
25+
modules: ["nuxt-lodash"],
26+
});
2927
```
3028

3129
## 🚀 Example
@@ -34,7 +32,7 @@ Use any [Lodash](https://lodash.com) methods in your Nuxt application, they will
3432

3533
```html
3634
<script setup>
37-
const text = useToUpper('it works!');
35+
const text = useToUpper("it works!");
3836
</script>
3937

4038
<template>
@@ -44,30 +42,32 @@ const text = useToUpper('it works!');
4442

4543
## 🔨 Config
4644

47-
| Name | Default | Description |
48-
| ------------ | -------- | -------------------------------------------------------------------------------- |
49-
| `prefix` | `'use'` | String to prepend before each Lodash function (false to disable) |
50-
| `prefixSkip` | `['is']` | Functions that starts with keywords in this array will be skipped by prefix |
51-
| `exclude` | `[]` | Array of Lodash functions to exclude from auto-imports |
52-
| `alias` | `[]` | Array of array pairs to rename specific Lodash functions (prefix is still added) |
45+
| Name | Default | Description |
46+
| ------------------ | -------- | ------------------------------------------------------------------------------------ |
47+
| `prefix` | `'use'` | String to prepend before each Lodash function (false to disable) |
48+
| `prefixSkip` | `['is']` | Functions that starts with keywords in this array will be skipped by prefix |
49+
| `upperAfterPrefix` | `true` | If true it will automatically uppercase first letter after prefix (false to disable) |
50+
| `exclude` | `[]` | Array of Lodash functions to exclude from auto-imports |
51+
| `alias` | `[]` | Array of array pairs to rename specific Lodash functions (prefix is still added) |
5352

5453
## 💻 Example - Config
5554

5655
```ts
57-
import { defineNuxtConfig } from 'nuxt';
58-
5956
export default defineNuxtConfig({
6057
modules: ['nuxt-lodash'],
6158
lodash: {
62-
prefix: 'use',
63-
prefixSkip: ['is'],
59+
prefix: '_',
60+
prefixSkip: ['string'],
61+
upperAfterPrefix: false,
6462
exclude: ['map'],
6563
alias: [
66-
['camelCase', 'stringToCamelCase'], // => useStringToCamelCase
67-
['kebabCase', 'stringToKebabCase'], // => useStringToKebabCase
68-
]
69-
}
70-
});
64+
['camelCase', 'stringToCamelCase'], // => stringToCamelCase
65+
['kebabCase', 'stringToKebab'], // => stringToKebab
66+
['isDate', 'isLodashDate'], // => _isLodashDate
67+
],
68+
},
69+
})
70+
7171
```
7272

7373
## 📄 License

playground/app.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
<script setup>
2+
const text = _toUpper('it works!')
3+
</script>
14
<template>
25
<div>{{ text }}</div>
36
</template>
4-
<script setup>
5-
const text = useToUpper('it works!')
6-
</script>

playground/nuxt.config.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { defineNuxtConfig } from 'nuxt'
2-
import nuxtLodash from '..'
3-
41
export default defineNuxtConfig({
5-
modules: [
6-
nuxtLodash
7-
],
2+
modules: ['nuxt-lodash'],
83
lodash: {
9-
prefix: 'use',
10-
prefixSkip: ['is'],
4+
prefix: '_',
5+
prefixSkip: ['string'],
6+
upperAfterPrefix: true,
117
exclude: ['map'],
128
alias: [
13-
['camelCase', 'stringToCamelCase'], // => useStringToCamelCase
14-
['kebabCase', 'stringToKebabCase'] // => useStringToKebabCase
9+
['camelCase', 'stringToCamelCase'], // => stringToCamelCase
10+
['kebabCase', 'stringToKebab'], // => stringToKebab
11+
['isDate', 'isLodashDate'] // => _isLodashDate
1512
]
1613
}
1714
})

src/module.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import exculdeDefaults from './exclude'
44

55
export interface ModuleOptions {
66
/**
7-
* Prefix to be added before every lodash function.
7+
* Prefix to be added before every lodash function
88
* False to disable prefix
99
*
1010
* @defaultValue `use`
1111
*/
1212
prefix: false | string;
1313
/**
1414
* Functions that starts with keywords in this array will be skipped by prefix
15-
15+
*
1616
* @defaultValue ['is']
1717
*/
1818
prefixSkip: string[];
@@ -28,34 +28,39 @@ export interface ModuleOptions {
2828
* @defaultValue []
2929
*/
3030
alias: Iterable<[string, string]>;
31+
/**
32+
* Upper case first letter after prefix
33+
* False to disable uppercasing
34+
*
35+
* @defaultValue true
36+
*/
37+
upperAfterPrefix: boolean
3138
}
3239

3340
export default defineNuxtModule<ModuleOptions>({
3441
meta: {
3542
name: 'nuxt-lodash',
3643
configKey: 'lodash',
3744
compatibility: {
38-
nuxt: '^3.0.0-rc.9'
45+
nuxt: '^3.0.0-rc.12'
3946
}
4047
},
4148
defaults: {
4249
prefix: 'use',
4350
prefixSkip: ['is'],
4451
exclude: [],
45-
alias: []
52+
alias: [],
53+
upperAfterPrefix: true
4654
},
4755
setup (options, nuxt) {
48-
const prefix = options.prefix || ''
4956
const aliasMap = new Map<string, string>(options.alias)
5057
const exludes = [...options.exclude, ...exculdeDefaults]
5158

52-
for (const [name] of Object.entries(lodash)) {
59+
for (const name of Object.keys(lodash)) {
5360
if (!exludes.includes(name)) {
5461
const alias = aliasMap.has(name) ? aliasMap.get(name)! : name
55-
const as = (() => {
56-
const isPrefix = !options.prefixSkip.some(key => alias.startsWith(key)) && prefix
57-
return isPrefix ? prefix + lodash.upperFirst(alias) : alias
58-
})()
62+
const prefix = (!options.prefixSkip.some(key => alias.startsWith(key)) && options.prefix) || ''
63+
const as = prefix ? prefix + (options.upperAfterPrefix ? lodash.upperFirst(alias) : alias) : alias
5964
addImports({ name, as, from: 'lodash-es' })
6065
}
6166
}

0 commit comments

Comments
 (0)