-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtag.ts
69 lines (64 loc) · 1.76 KB
/
tag.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { parseNamespace } from './namespace.js';
import type { NamespaceName } from './interfaces/ehtag.js';
import { RawTag } from './raw-tag.js';
export function parseTag(tag: string):
| {
valid: true;
ns: NamespaceName | undefined;
raw: RawTag;
}
| {
valid: false;
ns: NamespaceName | undefined;
raw: string;
} {
tag = tag ?? '';
let ns: string | undefined;
const i = tag.indexOf(':');
if (i >= 0) {
ns = tag.slice(0, i);
tag = tag.slice(i + 1);
}
const namespace = parseNamespace(ns);
const raw = RawTag(tag);
if (!raw)
return {
valid: false,
ns: ns != null ? namespace : undefined,
raw: tag.trim().toLowerCase(),
};
return {
valid: true,
ns: ns != null ? namespace : undefined,
raw,
};
}
/** 生成含命名空间 `f`, `m` 和 `x` 的标签 */
export function tagAbbr(tag: RawTag, ns?: NamespaceName): string {
if (ns === 'male') return `m:${tag}`;
if (ns === 'female') return `f:${tag}`;
if (ns === 'mixed') return `x:${tag}`;
return tag;
}
/** 生成含命名空间的标签 */
export function tagFull(tag: RawTag, ns: NamespaceName): string {
return `${ns}:${tag}`;
}
export const namespaceMapToSearch: Readonly<Record<NamespaceName, string>> = Object.freeze({
artist: 'a:',
parody: 'p:',
reclass: 'r:',
character: 'c:',
group: 'g:',
language: 'l:',
male: 'm:',
female: 'f:',
mixed: 'x:',
other: 'o',
cosplayer: 'cos:',
rows: 'rows:',
});
/** 生成含单字母命名空间的标签 */
export function tagAbbrFull(tag: RawTag, ns: NamespaceName): string {
return `${namespaceMapToSearch[ns]}${tag}`;
}