-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76f90e
commit e0ed94c
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect, test } from "vitest"; | ||
import { sluggize } from "../../../src/utils/slug"; | ||
|
||
test('should sluggize from string array - without prefix and fallback option', () => { | ||
expect(sluggize(['foo', 'bar'])).toEqual('/foo/bar'); | ||
}); | ||
|
||
test('should sluggize from number array - without prefix and fallback option', () => { | ||
expect(sluggize([1, 2])).toEqual('/1/2'); | ||
}); | ||
|
||
test('should sluggize from array - without fallback option', () => { | ||
expect(sluggize(['foo', 'bar'], '_prefix')).toEqual('/_prefix/foo/bar'); | ||
}); | ||
|
||
test('should sluggize returns fallback (undefined) if slug can not convert to array', () => { | ||
expect(sluggize({'hoge': 'piyo'}, '_prefix')).toEqual(undefined); | ||
}); | ||
|
||
test('should sluggize returns fallback string if slug can not convert to array', () => { | ||
expect(sluggize({'hoge': 'piyo'}, '_prefix', 'fallback_str')).toEqual('fallback_str'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function sluggize( | ||
slug: any, // NOTE: Next.js request context is any. | ||
prefix: string | undefined = undefined, | ||
fallback: string | undefined = undefined | ||
) { | ||
try { | ||
console.log() | ||
if (slug instanceof Array) { | ||
const p = prefix === undefined ? undefined : '/' + prefix; | ||
const s = slug as Array<string>; | ||
const arr = [p].concat(s); | ||
return arr.join('/'); | ||
} else { | ||
return fallback; | ||
} | ||
} catch { | ||
return fallback; | ||
} | ||
} | ||
|