Skip to content

Commit 93cca2a

Browse files
authored
perf(blog): use string replace and flatMap if possible (#600)
1 parent 8989a91 commit 93cca2a

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

plugins/blog/plugin-blog/src/node/blogPlugin.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ export const blogPlugin =
6969
type = [],
7070
slugify = (name: string): string =>
7171
name
72-
.replace(/[ _]/g, '-')
73-
.replace(/[:?*|\\/<>]/g, '')
72+
.replaceAll(/[ _]/g, '-')
73+
.replaceAll(/[:?*|\\/<>]/g, '')
7474
.toLowerCase(),
7575
} = options
7676

@@ -170,8 +170,8 @@ export const blogPlugin =
170170
...categoryResult.pageOptions,
171171
...typeResult.pageOptions,
172172
].map((page) => page.path!)
173-
categoriesMap = categoryResult.categoriesMap
174-
typesMap = typeResult.typesMap
173+
;({ categoriesMap } = categoryResult)
174+
;({ typesMap } = typeResult)
175175
},
176176

177177
onPrepared: async (): Promise<void> => {
@@ -232,7 +232,7 @@ export const blogPlugin =
232232
)
233233

234234
// add new pages
235-
if (pagesToBeAdded.length) {
235+
if (pagesToBeAdded.length > 0) {
236236
if (app.env.isDebug)
237237
logger.info(
238238
`Adding new pages: ${pagesToBeAdded.map(({ path }) => path).join(', ')}`,
@@ -251,7 +251,7 @@ export const blogPlugin =
251251
}
252252

253253
// Remove pages
254-
if (pagesToBeRemoved.length) {
254+
if (pagesToBeRemoved.length > 0) {
255255
if (app.env.isDebug)
256256
logger.info(
257257
`Removing following pages: ${pagesToBeRemoved.join(', ')}`,
@@ -266,7 +266,7 @@ export const blogPlugin =
266266
}
267267

268268
// Prepare pages entry
269-
if (pagesToBeRemoved.length || pagesToBeAdded.length) {
269+
if (pagesToBeRemoved.length > 0 || pagesToBeAdded.length > 0) {
270270
await prepareRoutes(app)
271271
}
272272

plugins/blog/plugin-blog/src/node/category/getCategory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export const getCategory = (
4545
: isString(itemPathOptions)
4646
? (name: string): string =>
4747
itemPathOptions
48-
.replace(/:key/g, slugify(key))
49-
.replace(/:name/g, slugify(name))
48+
.replaceAll(':key', slugify(key))
49+
.replaceAll(':name', slugify(name))
5050
: (): null => null
5151

5252
const categoryMap: CategoryMap = {}
@@ -55,7 +55,7 @@ export const getCategory = (
5555
for (const localePath in pagesMap) {
5656
if (path) {
5757
const pagePath = `${localePath}${removeLeadingSlash(
58-
path.replace(/:key/g, slugify(key)),
58+
path.replaceAll(':key', slugify(key)),
5959
)}`
6060

6161
pageOptions.push({
@@ -159,6 +159,6 @@ export const getCategory = (
159159
categoriesMap: fromEntries(
160160
result.map(({ key, categoryMap }) => [key, categoryMap]),
161161
),
162-
pageOptions: result.map(({ pageOptions }) => pageOptions).flat(),
162+
pageOptions: result.flatMap(({ pageOptions }) => pageOptions),
163163
}
164164
}

plugins/blog/plugin-blog/src/node/type/getType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const getType = (
3636
// get type page path
3737
const pagePath = path
3838
? `${localePath}${removeLeadingSlash(
39-
path.replace(/:key/g, slugify(key)),
39+
path.replaceAll(':key', slugify(key)),
4040
)}`
4141
: ''
4242

@@ -76,6 +76,6 @@ export const getType = (
7676

7777
return {
7878
typesMap: fromEntries(result.map(({ key, typeMap }) => [key, typeMap])),
79-
pageOptions: result.map(({ pageOptions }) => pageOptions).flat(),
79+
pageOptions: result.flatMap(({ pageOptions }) => pageOptions),
8080
}
8181
}

0 commit comments

Comments
 (0)