c063 1.6.3 → 1.6.5

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.
@@ -1,5 +1,16 @@
1
1
  import React from "react";
2
2
  import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
3
+ /**
4
+ * 檢查給定的值是否為有效的 `CodeTokenType`。
5
+ * @param value 要檢查的值
6
+ * @returns 如果值是有效的 `CodeTokenType`,則返回 `true`,否則返回 `false`
7
+ * @example
8
+ * ```ts
9
+ * isCodeTokenType("keyword1"); // true
10
+ * isCodeTokenType("invalidType"); // false
11
+ * ```
12
+ */
13
+ export declare const isCodeTokenType: (value: any) => value is CodeTokenType;
3
14
  /**
4
15
  * `c063` 是一組語法高亮 token 建構器集合。
5
16
  * 每個 key 對應一種語法分類(如 `keyword1`, `string`, `comment` 等),
@@ -1,4 +1,33 @@
1
1
  import React from "react";
2
+ const CODE_TOKEN_TYPES = new Set([
3
+ "keyword1",
4
+ "keyword2",
5
+ "function",
6
+ "string",
7
+ "number",
8
+ "comment",
9
+ "type",
10
+ "variable",
11
+ "constant",
12
+ "brackets1",
13
+ "brackets2",
14
+ "brackets3",
15
+ "operator",
16
+ "default",
17
+ ]);
18
+ /**
19
+ * 檢查給定的值是否為有效的 `CodeTokenType`。
20
+ * @param value 要檢查的值
21
+ * @returns 如果值是有效的 `CodeTokenType`,則返回 `true`,否則返回 `false`
22
+ * @example
23
+ * ```ts
24
+ * isCodeTokenType("keyword1"); // true
25
+ * isCodeTokenType("invalidType"); // false
26
+ * ```
27
+ */
28
+ export const isCodeTokenType = (value) => {
29
+ return CODE_TOKEN_TYPES.has(value);
30
+ };
2
31
  /**
3
32
  * `c063` 是一組語法高亮 token 建構器集合。
4
33
  * 每個 key 對應一種語法分類(如 `keyword1`, `string`, `comment` 等),
@@ -22,6 +51,9 @@ const c063 = new Proxy({}, {
22
51
  * @returns 一個 CodeToken 物件
23
52
  */
24
53
  const builder = (children, props) => {
54
+ if (!isCodeTokenType(prop)) {
55
+ throw new Error(`Invalid CodeTokenType: ${String(prop)}`);
56
+ }
25
57
  return {
26
58
  children,
27
59
  type: prop,
@@ -87,6 +119,9 @@ export const extractTokenContent = (token) => {
87
119
  * @returns 是否相等
88
120
  */
89
121
  export const isTokenEqual = (a, b) => {
122
+ if (!isCodeTokenType(a.type) || !isCodeTokenType(b.type)) {
123
+ return false;
124
+ }
90
125
  return a.type === b.type && extractTokenContent(a) === extractTokenContent(b);
91
126
  };
92
127
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "c063",
4
- "version": "1.6.3",
4
+ "version": "1.6.5",
5
5
  "description": "A React component for displaying code snippets with syntax highlighting.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -6,6 +6,36 @@ import {
6
6
  ParsableLanguage,
7
7
  } from "../types";
8
8
 
9
+ const CODE_TOKEN_TYPES = new Set<CodeTokenType>([
10
+ "keyword1",
11
+ "keyword2",
12
+ "function",
13
+ "string",
14
+ "number",
15
+ "comment",
16
+ "type",
17
+ "variable",
18
+ "constant",
19
+ "brackets1",
20
+ "brackets2",
21
+ "brackets3",
22
+ "operator",
23
+ "default",
24
+ ]);
25
+ /**
26
+ * 檢查給定的值是否為有效的 `CodeTokenType`。
27
+ * @param value 要檢查的值
28
+ * @returns 如果值是有效的 `CodeTokenType`,則返回 `true`,否則返回 `false`
29
+ * @example
30
+ * ```ts
31
+ * isCodeTokenType("keyword1"); // true
32
+ * isCodeTokenType("invalidType"); // false
33
+ * ```
34
+ */
35
+ export const isCodeTokenType = (value: any): value is CodeTokenType => {
36
+ return CODE_TOKEN_TYPES.has(value);
37
+ };
38
+
9
39
  /**
10
40
  * `c063` 是一組語法高亮 token 建構器集合。
11
41
  * 每個 key 對應一種語法分類(如 `keyword1`, `string`, `comment` 等),
@@ -34,6 +64,9 @@ const c063 = new Proxy(
34
64
  children: React.ReactNode,
35
65
  props?: CodeTokenProps<T>
36
66
  ) => {
67
+ if (!isCodeTokenType(prop)) {
68
+ throw new Error(`Invalid CodeTokenType: ${String(prop)}`);
69
+ }
37
70
  return {
38
71
  children,
39
72
  type: prop,
@@ -108,6 +141,9 @@ export const isTokenEqual = <T extends React.ElementType>(
108
141
  a: CodeTokenProps<T>,
109
142
  b: CodeTokenProps<T>
110
143
  ): boolean => {
144
+ if (!isCodeTokenType(a.type) || !isCodeTokenType(b.type)) {
145
+ return false;
146
+ }
111
147
  return a.type === b.type && extractTokenContent(a) === extractTokenContent(b);
112
148
  };
113
149