Skip to content

Commit 158c51f

Browse files
committed
fix: handle argument is null or undefined
1 parent cbcc4fa commit 158c51f

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

docs/src/methods/string.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ Merges the given string argument with the first argument (the beginning of the U
497497

498498
### Parameters
499499

500-
- `args::string[]`
500+
- `args::any[]`
501501

502502
### Returns
503503

lib/index.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ export default class Qsu {
836836
return [...new Set(str)].join('');
837837
}
838838

839-
static urlJoin(...args: string[]): string {
839+
static urlJoin(...args: any[]): string {
840840
if (!args) {
841841
return '';
842842
}
@@ -845,19 +845,21 @@ export default class Qsu {
845845
let urlResult = '';
846846

847847
for (let i = 0; i < argLength; i += 1) {
848-
if (
849-
i === 0 ||
850-
args[i].startsWith('/') ||
851-
args[i].startsWith('?') ||
852-
args[i].startsWith('&')
853-
) {
854-
urlResult += args[i];
855-
} else {
856-
urlResult += `/${args[i]}`;
848+
if (args[i] !== null && args[i] !== undefined) {
849+
if (
850+
i === 0 ||
851+
args[i].startsWith('/') ||
852+
args[i].startsWith('?') ||
853+
args[i].startsWith('&')
854+
) {
855+
urlResult += args[i];
856+
} else {
857+
urlResult += `/${args[i]}`;
858+
}
857859
}
858860
}
859861

860-
return urlResult;
862+
return urlResult.replace(/\/$/g, '');
861863
}
862864

863865
/*

test/string.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ st`),
243243
});
244244

245245
it('urlJoin', (done) => {
246+
assert.strictEqual(urlJoin('https://example.com'), 'https://example.com');
247+
assert.strictEqual(urlJoin('https://example.com', null, 'world/'), 'https://example.com/world');
246248
assert.strictEqual(
247249
urlJoin('https://example.com', 'hello', 'world'),
248250
'https://example.com/hello/world'

0 commit comments

Comments
 (0)