kokkoro-plugin-hitokoto 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/service.ts +5 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kokkoro-plugin-hitokoto",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Hitokoto 一言,随机获取动漫、小说、诗词等类型的语句。",
5
5
  "keywords": [
6
6
  "bot",
package/src/service.ts CHANGED
@@ -27,6 +27,9 @@ export const TYPE_NAMES = Object.keys(TYPE_CODES).join('、');
27
27
  /** 一言接口 `c` 参数接受的句子类型代码。 */
28
28
  export type SentenceType = (typeof TYPE_CODES)[keyof typeof TYPE_CODES];
29
29
 
30
+ /** 判断名称是否为 {@link TYPE_CODES} 自身定义的键。 */
31
+ export const isTypeName = (name: string): name is keyof typeof TYPE_CODES => Object.hasOwn(TYPE_CODES, name);
32
+
30
33
  /**
31
34
  * 将中文句子类型名称转换为一言接口的类型代码。
32
35
  *
@@ -47,12 +50,10 @@ export type SentenceType = (typeof TYPE_CODES)[keyof typeof TYPE_CODES];
47
50
  */
48
51
  export function resolveTypeCodes(names: string[]): SentenceType[] {
49
52
  return names.map(name => {
50
- const code: SentenceType = TYPE_CODES[<keyof typeof TYPE_CODES>name];
51
-
52
- if (!code) {
53
+ if (!isTypeName(name)) {
53
54
  throw new Error(`类型「${name}」不是有效值,支持的句子类型有:${TYPE_NAMES}`);
54
55
  }
55
- return code;
56
+ return TYPE_CODES[name];
56
57
  });
57
58
  }
58
59