c063 1.6.4 → 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,15 @@
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
+ */
3
13
  export declare const isCodeTokenType: (value: any) => value is CodeTokenType;
4
14
  /**
5
15
  * `c063` 是一組語法高亮 token 建構器集合。
@@ -1,22 +1,32 @@
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
+ */
2
28
  export const isCodeTokenType = (value) => {
3
- const codeTokenTypes = [
4
- "keyword1",
5
- "keyword2",
6
- "function",
7
- "string",
8
- "number",
9
- "comment",
10
- "type",
11
- "variable",
12
- "constant",
13
- "brackets1",
14
- "brackets2",
15
- "brackets3",
16
- "operator",
17
- "default",
18
- ];
19
- return codeTokenTypes.includes(value);
29
+ return CODE_TOKEN_TYPES.has(value);
20
30
  };
21
31
  /**
22
32
  * `c063` 是一組語法高亮 token 建構器集合。
@@ -32,7 +42,7 @@ export const isCodeTokenType = (value) => {
32
42
  * @returns 以 `CodeTokenType` 為 key 的建構器函式集合。
33
43
  */
34
44
  const c063 = new Proxy({}, {
35
- get: (target, prop, receiver) => {
45
+ get: (_, prop) => {
36
46
  /**
37
47
  * 建立指定語法類型的 CodeToken。
38
48
  *
@@ -42,7 +52,7 @@ const c063 = new Proxy({}, {
42
52
  */
43
53
  const builder = (children, props) => {
44
54
  if (!isCodeTokenType(prop)) {
45
- return Reflect.get(target, prop, receiver);
55
+ throw new Error(`Invalid CodeTokenType: ${String(prop)}`);
46
56
  }
47
57
  return {
48
58
  children,
@@ -109,6 +119,9 @@ export const extractTokenContent = (token) => {
109
119
  * @returns 是否相等
110
120
  */
111
121
  export const isTokenEqual = (a, b) => {
122
+ if (!isCodeTokenType(a.type) || !isCodeTokenType(b.type)) {
123
+ return false;
124
+ }
112
125
  return a.type === b.type && extractTokenContent(a) === extractTokenContent(b);
113
126
  };
114
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.4",
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,26 +6,35 @@ 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
+ */
9
35
  export const isCodeTokenType = (value: any): value is CodeTokenType => {
10
- const codeTokenTypes: CodeTokenType[] = [
11
- "keyword1",
12
- "keyword2",
13
- "function",
14
- "string",
15
- "number",
16
- "comment",
17
- "type",
18
- "variable",
19
- "constant",
20
- "brackets1",
21
- "brackets2",
22
- "brackets3",
23
- "operator",
24
- "default",
25
- ];
26
- return codeTokenTypes.includes(value);
36
+ return CODE_TOKEN_TYPES.has(value);
27
37
  };
28
-
29
38
 
30
39
  /**
31
40
  * `c063` 是一組語法高亮 token 建構器集合。
@@ -43,7 +52,7 @@ export const isCodeTokenType = (value: any): value is CodeTokenType => {
43
52
  const c063 = new Proxy(
44
53
  {},
45
54
  {
46
- get: (target, prop: CodeTokenType, receiver) => {
55
+ get: (_, prop: CodeTokenType) => {
47
56
  /**
48
57
  * 建立指定語法類型的 CodeToken。
49
58
  *
@@ -56,7 +65,7 @@ const c063 = new Proxy(
56
65
  props?: CodeTokenProps<T>
57
66
  ) => {
58
67
  if (!isCodeTokenType(prop)) {
59
- return Reflect.get(target, prop, receiver);
68
+ throw new Error(`Invalid CodeTokenType: ${String(prop)}`);
60
69
  }
61
70
  return {
62
71
  children,
@@ -132,6 +141,9 @@ export const isTokenEqual = <T extends React.ElementType>(
132
141
  a: CodeTokenProps<T>,
133
142
  b: CodeTokenProps<T>
134
143
  ): boolean => {
144
+ if (!isCodeTokenType(a.type) || !isCodeTokenType(b.type)) {
145
+ return false;
146
+ }
135
147
  return a.type === b.type && extractTokenContent(a) === extractTokenContent(b);
136
148
  };
137
149