Total Complexity | 3 |
Complexity/F | 0 |
Lines of Code | 45 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 90.91% |
Changes | 0 |
1 | 1 | import type { TitleOptions, SetTitleOptions } from './types'; |
|
2 | 1 | import { safeString } from './utils'; |
|
3 | 1 | ||
4 | 1 | /** |
|
5 | 1 | * if use ssr document is not available |
|
6 | 1 | */ |
|
7 | 1 | const isBrowser = (): boolean => typeof document !== 'undefined'; |
|
8 | 1 | ||
9 | 1 | /** |
|
10 | 1 | * build a full title white suffix and prefix |
|
11 | 1 | */ |
|
12 | 1 | const buildPageTitle = (value: string, options: TitleOptions = {}): string => { |
|
13 | 21 | const { prefix, suffix } = options; |
|
14 | 21 | ||
15 | 21 | return `${safeString(prefix)} ${value} ${safeString(suffix)}`.trim(); |
|
16 | 21 | }; |
|
17 | 1 | ||
18 | 1 | const setPageTitle = (value: string, options: SetTitleOptions = {}): void => { |
|
19 | 18 | // test if not is a browser |
|
20 | 18 | if (!isBrowser()) { |
|
21 | console.warn('vue-page-title: no browser enviroment'); |
||
22 | return; |
||
23 | } |
||
24 | |||
25 | 18 | value = safeString(value); |
|
26 | 18 | ||
27 | 18 | // test if title is empty |
|
28 | 18 | if (value.length === 0) { |
|
29 | 1 | return; |
|
30 | 1 | } |
|
31 | 1 | ||
32 | 18 | const { setTitleMethod } = options; |
|
33 | 17 | const title = buildPageTitle(value, options); |
|
34 | 17 | ||
35 | 17 | // use custom setTitle method |
|
36 | 17 | if (setTitleMethod != null) { |
|
37 | 1 | setTitleMethod(title); |
|
38 | 1 | return; |
|
39 | 1 | } |
|
40 | 1 | ||
41 | 18 | document.title = title; |
|
42 | 16 | }; |
|
43 | 1 | ||
44 | export { buildPageTitle, setPageTitle }; |
||
45 |