@vertesia/json 0.80.0-dev-20251118 → 0.80.0

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/README.md +180 -0
  2. package/package.json +47 -38
package/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # @vertesia/json
2
+
3
+ JSON utilities for TypeScript, including type definitions and object traversal with the visitor pattern.
4
+
5
+ ## Features
6
+
7
+ - **JSON Type Definitions**: Strict TypeScript types for JSON values
8
+ - **Object Walker**: Traverse nested objects and arrays using the visitor pattern
9
+ - **Async Support**: Async object walker for asynchronous operations during traversal
10
+ - **Map Function**: Transform object values while preserving structure
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @vertesia/json
16
+ # or
17
+ pnpm add @vertesia/json
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### JSON Types
23
+
24
+ Strict TypeScript types for JSON values:
25
+
26
+ ```typescript
27
+ import type {
28
+ JSONPrimitive, // string | number | boolean | null
29
+ JSONArray, // JSONValue[]
30
+ JSONObject, // { [key: string]: JSONValue }
31
+ JSONComposite, // JSONArray | JSONObject
32
+ JSONValue // JSONPrimitive | JSONComposite
33
+ } from '@vertesia/json';
34
+
35
+ function processJson(data: JSONValue) {
36
+ // Type-safe JSON handling
37
+ }
38
+ ```
39
+
40
+ ### Object Walker
41
+
42
+ Traverse objects using the visitor pattern:
43
+
44
+ ```typescript
45
+ import { ObjectWalker } from '@vertesia/json';
46
+
47
+ const data = {
48
+ name: 'John',
49
+ tags: ['admin', 'user'],
50
+ metadata: {
51
+ created: '2024-01-01',
52
+ count: 42
53
+ }
54
+ };
55
+
56
+ const walker = new ObjectWalker();
57
+
58
+ walker.walk(data, {
59
+ onStartObject: (key, value) => {
60
+ console.log(`Start object: ${key}`);
61
+ },
62
+ onEndObject: (key, value) => {
63
+ console.log(`End object: ${key}`);
64
+ },
65
+ onStartIteration: (key, value) => {
66
+ console.log(`Start array: ${key}`);
67
+ },
68
+ onEndIteration: (key, value) => {
69
+ console.log(`End array: ${key}`);
70
+ },
71
+ onValue: (key, value) => {
72
+ console.log(`Value: ${key} = ${value}`);
73
+ }
74
+ });
75
+ ```
76
+
77
+ ### Map Values
78
+
79
+ Transform values while preserving structure:
80
+
81
+ ```typescript
82
+ import { ObjectWalker } from '@vertesia/json';
83
+
84
+ const data = {
85
+ name: 'John',
86
+ age: 30,
87
+ scores: [85, 90, 78]
88
+ };
89
+
90
+ const walker = new ObjectWalker();
91
+ const result = walker.map(data, (key, value) => {
92
+ if (typeof value === 'number') {
93
+ return value * 2;
94
+ }
95
+ return value;
96
+ });
97
+
98
+ // result: { name: 'John', age: 60, scores: [170, 180, 156] }
99
+ ```
100
+
101
+ ### Async Object Walker
102
+
103
+ For asynchronous operations during traversal:
104
+
105
+ ```typescript
106
+ import { AsyncObjectWalker } from '@vertesia/json';
107
+
108
+ const walker = new AsyncObjectWalker();
109
+
110
+ await walker.walk(data, {
111
+ onValue: async (key, value) => {
112
+ if (typeof value === 'string') {
113
+ await processString(value);
114
+ }
115
+ }
116
+ });
117
+
118
+ // Async map
119
+ const result = await walker.map(data, async (key, value) => {
120
+ if (typeof value === 'string') {
121
+ return await translateText(value);
122
+ }
123
+ return value;
124
+ });
125
+ ```
126
+
127
+ ### Iterator Support
128
+
129
+ Enable support for iterables beyond arrays:
130
+
131
+ ```typescript
132
+ const walker = new ObjectWalker(true); // Enable iterator support
133
+
134
+ const data = {
135
+ items: new Set([1, 2, 3])
136
+ };
137
+
138
+ walker.walk(data, {
139
+ onValue: (key, value) => {
140
+ console.log(key, value);
141
+ }
142
+ });
143
+ ```
144
+
145
+ ## API Reference
146
+
147
+ ### Types
148
+
149
+ | Type | Description |
150
+ |------|-------------|
151
+ | `JSONPrimitive` | `string \| number \| boolean \| null` |
152
+ | `JSONArray` | `JSONValue[]` |
153
+ | `JSONObject` | `{ [key: string]: JSONValue }` |
154
+ | `JSONComposite` | `JSONArray \| JSONObject` |
155
+ | `JSONValue` | `JSONPrimitive \| JSONComposite` |
156
+
157
+ ### ObjectWalker
158
+
159
+ | Method | Description |
160
+ |--------|-------------|
161
+ | `walk(obj, visitor)` | Traverse object with visitor callbacks |
162
+ | `map(obj, mapFn)` | Transform values while preserving structure |
163
+
164
+ ### ObjectVisitor Callbacks
165
+
166
+ | Callback | Description |
167
+ |----------|-------------|
168
+ | `onStartObject(key, value)` | Called when entering an object |
169
+ | `onEndObject(key, value)` | Called when leaving an object |
170
+ | `onStartIteration(key, value)` | Called when entering an array/iterable |
171
+ | `onEndIteration(key, value)` | Called when leaving an array/iterable |
172
+ | `onValue(key, value)` | Called for primitive values |
173
+
174
+ ### AsyncObjectWalker
175
+
176
+ Same API as `ObjectWalker` but all visitor callbacks and map functions return Promises.
177
+
178
+ ## License
179
+
180
+ Apache-2.0
package/package.json CHANGED
@@ -1,40 +1,49 @@
1
1
  {
2
- "name": "@vertesia/json",
3
- "version": "0.80.0-dev-20251118",
4
- "description": "JSON utlities",
5
- "type": "module",
2
+ "name": "@vertesia/json",
3
+ "version": "0.80.0",
4
+ "description": "JSON utlities",
5
+ "type": "module",
6
+ "types": "./lib/types/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "src"
10
+ ],
11
+ "license": "Apache-2.0",
12
+ "exports": {
6
13
  "types": "./lib/types/index.d.ts",
7
- "files": [
8
- "lib",
9
- "src"
10
- ],
11
- "license": "Apache-2.0",
12
- "scripts": {
13
- "test": "vitest run",
14
- "build": "pnpm exec tsmod build && pnpm exec rollup -c",
15
- "clean": "rimraf ./node_modules ./lib ./tsconfig.tsbuildinfo"
16
- },
17
- "exports": {
18
- "types": "./lib/types/index.d.ts",
19
- "import": "./lib/esm/index.js",
20
- "require": "./lib/cjs/index.js"
21
- },
22
- "devDependencies": {
23
- "@rollup/plugin-commonjs": "^28.0.3",
24
- "@rollup/plugin-node-resolve": "^16.0.1",
25
- "@rollup/plugin-typescript": "^12.1.2",
26
- "rollup": "^4.40.2",
27
- "rollup-plugin-terser": "^7.0.2",
28
- "ts-dual-module": "^0.6.3",
29
- "typescript": "^5.0.2",
30
- "vitest": "^3.0.9"
31
- },
32
- "ts_dual_module": {
33
- "outDir": "lib"
34
- },
35
- "repository": {
36
- "type": "git",
37
- "url": "https://github.com/vertesia/composableai.git",
38
- "directory": "packages/json"
39
- }
40
- }
14
+ "import": "./lib/esm/index.js",
15
+ "require": "./lib/cjs/index.js"
16
+ },
17
+ "devDependencies": {
18
+ "@rollup/plugin-commonjs": "^28.0.3",
19
+ "@rollup/plugin-node-resolve": "^16.0.1",
20
+ "@rollup/plugin-typescript": "^12.1.2",
21
+ "rollup": "^4.40.2",
22
+ "rollup-plugin-terser": "^7.0.2",
23
+ "ts-dual-module": "^0.6.3",
24
+ "typescript": "^5.0.2",
25
+ "vitest": "^3.0.9"
26
+ },
27
+ "ts_dual_module": {
28
+ "outDir": "lib"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/vertesia/composableai.git",
33
+ "directory": "packages/json"
34
+ },
35
+ "keywords": [
36
+ "vertesia",
37
+ "json",
38
+ "utilities",
39
+ "walker",
40
+ "visitor",
41
+ "traverse",
42
+ "typescript"
43
+ ],
44
+ "scripts": {
45
+ "test": "vitest run",
46
+ "build": "pnpm exec tsmod build && pnpm exec rollup -c",
47
+ "clean": "rimraf ./node_modules ./lib ./tsconfig.tsbuildinfo"
48
+ }
49
+ }