react-lib-tools 0.0.18 → 0.0.19

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-lib-tools",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "type": "module",
5
5
  "author": "Brian Vaughn <brian.david.vaughn@gmail.com> (https://github.com/bvaughn/)",
6
6
  "contributors": [
@@ -8,6 +8,7 @@ import { syntaxHighlight } from "../syntax-highlight.ts";
8
8
  import { getPropTypeText } from "./getPropTypeText.ts";
9
9
  import { parseDescription } from "./parseDescription.ts";
10
10
  import { propsToTable } from "./propsToTable.ts";
11
+ import { getDefaultValueType } from "./getDefaultValueType.ts";
11
12
 
12
13
  const TOKEN_TO_REPLACE = "TOKEN_TO_REPLACE";
13
14
 
@@ -61,11 +62,13 @@ export async function compileComponent({
61
62
  let formattedValue = prop.defaultValue.value;
62
63
 
63
64
  // Wrap strings in quotes
64
- if (typeof prop.defaultValue.value === "string") {
65
- // But don't double quote, e.g.
66
- // tagName?: keyof IntrinsicElements = ""div" as TagName"
67
- if (!prop.defaultValue.value.includes('" as ')) {
68
- formattedValue = `"${prop.defaultValue.value}"`;
65
+ switch (getDefaultValueType(prop.defaultValue.value)) {
66
+ case "string": {
67
+ // But don't double quote, e.g.
68
+ // tagName?: keyof IntrinsicElements = ""div" as TagName"
69
+ if (!prop.defaultValue.value.includes('" as ')) {
70
+ formattedValue = `"${prop.defaultValue.value}"`;
71
+ }
69
72
  }
70
73
  }
71
74
 
@@ -0,0 +1,51 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { getDefaultValueType } from "./getDefaultValueType";
3
+
4
+ describe("getDefaultValueType", () => {
5
+ test.each(["[]", "[1,2]", '["a","b","c"]'])(
6
+ "should identify array value: %s",
7
+ (value) => {
8
+ expect(getDefaultValueType(value)).toBe("array");
9
+ }
10
+ );
11
+
12
+ test.each(["true", "false"])("should identify boolean value: %s", (value) => {
13
+ expect(getDefaultValueType(value)).toBe("boolean");
14
+ });
15
+
16
+ test("should detect null values", () => {
17
+ expect(getDefaultValueType("null")).toBe("null");
18
+ });
19
+
20
+ test.each(["0", "1", "1.0", "-1", "-1.0"])(
21
+ "should identify numeric value: %s",
22
+ (value) => {
23
+ expect(getDefaultValueType(value)).toBe("number");
24
+ }
25
+ );
26
+
27
+ test.each(["{}", '{foo:"a",bar:true, baz:123}'])(
28
+ "should identify object value: %s",
29
+ (value) => {
30
+ expect(getDefaultValueType(value)).toBe("object");
31
+ }
32
+ );
33
+
34
+ test.each(["", " ", "foo bar baz"])(
35
+ "should identify string value: %o",
36
+ (value) => {
37
+ expect(getDefaultValueType(value)).toBe("string");
38
+ }
39
+ );
40
+
41
+ test("should detect undefined values", () => {
42
+ expect(getDefaultValueType("undefined")).toBe("undefined");
43
+ });
44
+
45
+ test.each(["1a", "-.!/"])(
46
+ "should identify invalid value: %o as string",
47
+ (value) => {
48
+ expect(getDefaultValueType(value)).toBe("string");
49
+ }
50
+ );
51
+ });
@@ -0,0 +1,50 @@
1
+ export type Type =
2
+ | "array"
3
+ | "boolean"
4
+ | "null"
5
+ | "number"
6
+ | "object"
7
+ | "string"
8
+ | "undefined";
9
+
10
+ export function getDefaultValueType(value: string): Type {
11
+ switch (value) {
12
+ case "false":
13
+ case "true": {
14
+ return "boolean";
15
+ }
16
+ case "null": {
17
+ return "null";
18
+ }
19
+ case "undefined": {
20
+ return "undefined";
21
+ }
22
+ default: {
23
+ if (value.trim() === "") {
24
+ return "string";
25
+ } else if (value.startsWith("{") && value.endsWith("}")) {
26
+ return "object";
27
+ }
28
+ }
29
+ }
30
+
31
+ try {
32
+ const parsed = JSON.parse(value);
33
+ switch (typeof parsed) {
34
+ case "number": {
35
+ return "number";
36
+ }
37
+ default: {
38
+ if (Array.isArray(parsed)) {
39
+ return "array";
40
+ }
41
+
42
+ return "object";
43
+ }
44
+ }
45
+ } catch {
46
+ // No-op
47
+ }
48
+
49
+ return "string";
50
+ }
package/styles.css CHANGED
@@ -102,6 +102,9 @@ code {
102
102
  color: rgba(255, 255, 255, 0.8);
103
103
  }
104
104
 
105
+ .tok-bool {
106
+ color: var(--color-teal-300);
107
+ }
105
108
  .tok-comment {
106
109
  color: var(--color-slate-500);
107
110
  }
@@ -115,6 +118,7 @@ code {
115
118
  .tok-meta {
116
119
  }
117
120
  .tok-number {
121
+ color: var(--color-teal-300);
118
122
  }
119
123
  .tok-operator {
120
124
  color: var(--color-slate-400);