Skip to content

Commit d57be12

Browse files
authored
chore: cleanup, short-circuit .split calls (#14245)
Just some small optimizations, extracted from #14244 to make the diff easier to read: - short circuit .split() calls => faster - remove unused code and clean up code
1 parent 8b0ac01 commit d57be12

File tree

8 files changed

+19
-22
lines changed

8 files changed

+19
-22
lines changed

packages/next/src/utilities/handleAuthRedirect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ export const handleAuthRedirect = ({ config, route, searchParams, user }: Args):
4343
{ addQueryPrefix: true },
4444
)}`
4545

46-
return `${redirectTo.split('?')[0]}${searchParamsWithRedirect}`
46+
return `${redirectTo.split('?', 1)[0]}${searchParamsWithRedirect}`
4747
}

packages/payload/src/bin/generateImportMap/utilities/parsePayloadComponent.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ export function parsePayloadComponent(PayloadComponent: PayloadComponent): {
1111
const pathAndMaybeExport =
1212
typeof PayloadComponent === 'string' ? PayloadComponent : PayloadComponent.path
1313

14-
let path: string | undefined = ''
15-
let exportName: string | undefined = 'default'
14+
let path: string
15+
let exportName: string
1616

17-
if (pathAndMaybeExport?.includes('#')) {
18-
;[path, exportName] = pathAndMaybeExport.split('#')
17+
if (pathAndMaybeExport.includes('#')) {
18+
;[path, exportName] = pathAndMaybeExport.split('#', 2) as [string, string]
1919
} else {
2020
path = pathAndMaybeExport
21+
exportName = 'default'
2122
}
2223

2324
if (typeof PayloadComponent === 'object' && PayloadComponent.exportName) {
2425
exportName = PayloadComponent.exportName
2526
}
2627

27-
return { exportName: exportName!, path: path! }
28+
return { exportName, path }
2829
}

packages/payload/src/utilities/addDataAndFileToRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const addDataAndFileToRequest: AddDataAndFileToRequest = async (req) => {
1212
const { body, headers, method, payload } = req
1313

1414
if (method && ['PATCH', 'POST', 'PUT'].includes(method.toUpperCase()) && body) {
15-
const [contentType] = (headers.get('Content-Type') || '').split(';')
15+
const [contentType] = (headers.get('Content-Type') || '').split(';', 1)
1616
const bodyByteSize = parseInt(req.headers.get('Content-Length') || '0', 10)
1717

1818
if (contentType === 'application/json') {

packages/payload/src/utilities/createArrayFromCommaDelineated.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ export function createArrayFromCommaDelineated(input: string): string[] {
22
if (Array.isArray(input)) {
33
return input
44
}
5-
if (input.indexOf(',') > -1) {
6-
return input.split(',')
7-
}
8-
return [input]
5+
6+
return input.split(',')
97
}

packages/payload/src/utilities/dependencies/getDependencies.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ const filename = fileURLToPath(import.meta.url)
2525
const dirname = path.dirname(filename)
2626

2727
const payloadPkgDirname = path.resolve(dirname, '../../../') // pkg dir (outside src)
28-
// if node_modules is in payloadPkgDirname, go to parent dir which contains node_modules
29-
if (payloadPkgDirname.includes('node_modules')) {
30-
payloadPkgDirname.split('node_modules').slice(0, -1)
31-
}
28+
3229
const resolvedCwd = path.resolve(process.cwd())
3330

3431
export type NecessaryDependencies = {

packages/richtext-lexical/src/features/upload/client/plugin/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export const UploadPlugin: PluginComponent<UploadFeaturePropsClient> = ({ client
199199
byteNumbers[i] = byteCharacters.charCodeAt(i)
200200
}
201201
const byteArray = new Uint8Array(byteNumbers)
202-
const file = new File([byteArray], 'pasted-image.' + mimeType?.split('/')[1], {
202+
const file = new File([byteArray], 'pasted-image.' + mimeType?.split('/', 2)[1], {
203203
type: mimeType,
204204
})
205205
transformedImage = { alt: undefined, file, formID }
@@ -208,7 +208,7 @@ export const UploadPlugin: PluginComponent<UploadFeaturePropsClient> = ({ client
208208
const res = await fetch(src)
209209
const blob = await res.blob()
210210
const inferredFileName =
211-
src.split('/').pop() || 'pasted-image' + blob.type.split('/')[1]
211+
src.split('/').pop() || 'pasted-image' + blob.type.split('/', 2)[1]
212212
const file = new File([blob], inferredFileName, {
213213
type: blob.type,
214214
})

packages/richtext-lexical/src/field/Diff/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,23 @@ export const LexicalDiffComponent: RichTextFieldDiffServerComponent = async (arg
2727
i18n,
2828
locale,
2929
nestingLevel,
30+
req,
3031
versionValue: valueTo,
3132
} = args
3233

3334
const converters: HTMLConvertersFunctionAsync = ({ defaultConverters }) => ({
3435
...defaultConverters,
3536
...LinkDiffHTMLConverterAsync({}),
3637
...ListItemDiffHTMLConverterAsync,
37-
...UploadDiffHTMLConverterAsync({ i18n: args.i18n, req: args.req }),
38-
...RelationshipDiffHTMLConverterAsync({ i18n: args.i18n, req: args.req }),
39-
...UnknownDiffHTMLConverterAsync({ i18n: args.i18n, req: args.req }),
38+
...UploadDiffHTMLConverterAsync({ i18n, req }),
39+
...RelationshipDiffHTMLConverterAsync({ i18n, req }),
40+
...UnknownDiffHTMLConverterAsync({ i18n, req }),
4041
})
4142

4243
const payloadPopulateFn = await getPayloadPopulateFn({
4344
currentDepth: 0,
4445
depth: 1,
45-
req: args.req,
46+
req,
4647
})
4748
const fromHTML = await convertLexicalToHTMLAsync({
4849
converters,

packages/richtext-lexical/src/field/RenderLexical/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const RenderLexical: React.FC<
4242
const serverFunctionContext = useServerFunctions()
4343
const { _internal_renderField } = serverFunctionContext
4444

45-
const [entityType, entitySlug] = schemaPath.split('.')
45+
const [entityType, entitySlug] = schemaPath.split('.', 2)
4646

4747
const fieldPath = path ?? (field && 'name' in field ? field?.name : '') ?? ''
4848

0 commit comments

Comments
 (0)