@skmtc/core 0.0.793 → 0.0.794

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 (43) hide show
  1. package/esm/helpers/ResultsLog.d.ts +193 -0
  2. package/esm/helpers/ResultsLog.d.ts.map +1 -1
  3. package/esm/helpers/ResultsLog.js +213 -0
  4. package/esm/helpers/tracer.d.ts +124 -0
  5. package/esm/helpers/tracer.d.ts.map +1 -1
  6. package/esm/helpers/tracer.js +124 -0
  7. package/esm/typescript/PathParams.d.ts +198 -0
  8. package/esm/typescript/PathParams.d.ts.map +1 -1
  9. package/esm/typescript/PathParams.js +190 -0
  10. package/esm/typescript/ReactRouterPathParams.d.ts +190 -0
  11. package/esm/typescript/ReactRouterPathParams.d.ts.map +1 -1
  12. package/esm/typescript/ReactRouterPathParams.js +183 -0
  13. package/esm/typescript/keyValues.d.ts +125 -0
  14. package/esm/typescript/keyValues.d.ts.map +1 -1
  15. package/esm/typescript/keyValues.js +125 -1
  16. package/esm/typescript/toPathParams.d.ts +179 -0
  17. package/esm/typescript/toPathParams.d.ts.map +1 -1
  18. package/esm/typescript/toPathParams.js +179 -0
  19. package/esm/typescript/toPathTemplate.d.ts +114 -0
  20. package/esm/typescript/toPathTemplate.d.ts.map +1 -1
  21. package/esm/typescript/toPathTemplate.js +114 -0
  22. package/package.json +1 -1
  23. package/script/helpers/ResultsLog.d.ts +193 -0
  24. package/script/helpers/ResultsLog.d.ts.map +1 -1
  25. package/script/helpers/ResultsLog.js +213 -0
  26. package/script/helpers/tracer.d.ts +124 -0
  27. package/script/helpers/tracer.d.ts.map +1 -1
  28. package/script/helpers/tracer.js +124 -0
  29. package/script/typescript/PathParams.d.ts +198 -0
  30. package/script/typescript/PathParams.d.ts.map +1 -1
  31. package/script/typescript/PathParams.js +190 -0
  32. package/script/typescript/ReactRouterPathParams.d.ts +190 -0
  33. package/script/typescript/ReactRouterPathParams.d.ts.map +1 -1
  34. package/script/typescript/ReactRouterPathParams.js +183 -0
  35. package/script/typescript/keyValues.d.ts +125 -0
  36. package/script/typescript/keyValues.d.ts.map +1 -1
  37. package/script/typescript/keyValues.js +125 -1
  38. package/script/typescript/toPathParams.d.ts +179 -0
  39. package/script/typescript/toPathParams.d.ts.map +1 -1
  40. package/script/typescript/toPathParams.js +179 -0
  41. package/script/typescript/toPathTemplate.d.ts +114 -0
  42. package/script/typescript/toPathTemplate.d.ts.map +1 -1
  43. package/script/typescript/toPathTemplate.js +114 -0
@@ -2,18 +2,208 @@ import { ContentBase } from '../dsl/ContentBase.js';
2
2
  import type { GenerateContext } from '../context/GenerateContext.js';
3
3
  import type { OasOperation } from '../oas/operation/Operation.js';
4
4
  import type { GeneratorKey } from '../types/GeneratorKeys.js';
5
+ /**
6
+ * Constructor arguments for {@link ReactRouterPathParams}.
7
+ */
5
8
  type CreateArgs = {
9
+ /** The generation context for registering imports and handling dependencies */
6
10
  context: GenerateContext;
11
+ /** The generator key identifying this component */
7
12
  generatorKey: GeneratorKey;
13
+ /** The OpenAPI operation containing path parameters */
8
14
  operation: OasOperation;
15
+ /** The destination file path for import registration */
9
16
  destinationPath: string;
10
17
  };
18
+ /**
19
+ * Generates React Router path parameter handling code for OpenAPI operations.
20
+ *
21
+ * `ReactRouterPathParams` is a specialized content generator that creates the necessary
22
+ * React code for extracting and validating path parameters from React Router's `useParams`
23
+ * hook. It automatically generates parameter extraction, assertion logic, and prop passing
24
+ * code based on OpenAPI operation path parameters.
25
+ *
26
+ * This class is essential for generating type-safe React components that need to access
27
+ * URL path parameters, ensuring runtime validation and proper TypeScript typing. It
28
+ * integrates with React Router's `useParams` hook and adds runtime assertions using
29
+ * the `tiny-invariant` library.
30
+ *
31
+ * ## Key Features
32
+ *
33
+ * - **Automatic Parameter Extraction**: Generates `useParams()` destructuring code
34
+ * - **Runtime Assertions**: Creates invariant checks for required parameters
35
+ * - **Prop Passing**: Generates prop spreading syntax for component integration
36
+ * - **Import Management**: Automatically registers required imports
37
+ * - **Type Safety**: Ensures parameters are properly validated at runtime
38
+ *
39
+ * @example Basic usage with single path parameter
40
+ * ```typescript
41
+ * import { ReactRouterPathParams } from '@skmtc/core';
42
+ *
43
+ * const operation = new OasOperation({
44
+ * path: '/users/{id}',
45
+ * method: 'get',
46
+ * // ... other operation properties
47
+ * });
48
+ *
49
+ * const pathParams = new ReactRouterPathParams({
50
+ * context: generateContext,
51
+ * generatorKey: 'react-router-params',
52
+ * operation,
53
+ * destinationPath: './UserDetail.tsx'
54
+ * });
55
+ *
56
+ * console.log(pathParams.getParams);
57
+ * // 'const { id } = useParams()'
58
+ *
59
+ * console.log(pathParams.assertParams);
60
+ * // 'invariant(id, 'Expected id to be defined')'
61
+ *
62
+ * console.log(pathParams.passProps);
63
+ * // 'id={id}'
64
+ * ```
65
+ *
66
+ * @example Multiple path parameters
67
+ * ```typescript
68
+ * const complexOperation = new OasOperation({
69
+ * path: '/organizations/{orgId}/projects/{projectId}/issues/{issueId}',
70
+ * method: 'get',
71
+ * // ... other properties
72
+ * });
73
+ *
74
+ * const multiParams = new ReactRouterPathParams({
75
+ * context: generateContext,
76
+ * generatorKey: 'react-router-params',
77
+ * operation: complexOperation,
78
+ * destinationPath: './IssueDetail.tsx'
79
+ * });
80
+ *
81
+ * console.log(multiParams.getParams);
82
+ * // 'const { orgId, projectId, issueId } = useParams()'
83
+ *
84
+ * console.log(multiParams.assertParams);
85
+ * // 'invariant(orgId, 'Expected orgId to be defined')
86
+ * // invariant(projectId, 'Expected projectId to be defined')
87
+ * // invariant(issueId, 'Expected issueId to be defined')'
88
+ *
89
+ * console.log(multiParams.passProps);
90
+ * // 'orgId={orgId} projectId={projectId} issueId={issueId}'
91
+ * ```
92
+ *
93
+ * @example Generated React component integration
94
+ * ```typescript
95
+ * // Generated component might look like:
96
+ * import { useParams } from 'react-router-dom';
97
+ * import invariant from 'tiny-invariant';
98
+ *
99
+ * function UserDetail() {
100
+ * const { id } = useParams();
101
+ * invariant(id, 'Expected id to be defined');
102
+ *
103
+ * return <UserDetailView id={id} />;
104
+ * }
105
+ * ```
106
+ *
107
+ * @example No path parameters (empty generation)
108
+ * ```typescript
109
+ * const simpleOperation = new OasOperation({
110
+ * path: '/users',
111
+ * method: 'get',
112
+ * // ... other properties
113
+ * });
114
+ *
115
+ * const emptyParams = new ReactRouterPathParams({
116
+ * context: generateContext,
117
+ * generatorKey: 'react-router-params',
118
+ * operation: simpleOperation,
119
+ * destinationPath: './UserList.tsx'
120
+ * });
121
+ *
122
+ * console.log(emptyParams.getParams); // ''
123
+ * console.log(emptyParams.assertParams); // ''
124
+ * console.log(emptyParams.passProps); // ''
125
+ * // No imports registered for this case
126
+ * ```
127
+ *
128
+ * @example Integration in component generation
129
+ * ```typescript
130
+ * class ReactComponentGenerator {
131
+ * generateDetailComponent(operation: OasOperation) {
132
+ * const pathParams = new ReactRouterPathParams({
133
+ * context: this.context,
134
+ * generatorKey: 'react-component',
135
+ * operation,
136
+ * destinationPath: this.getDestinationPath(operation)
137
+ * });
138
+ *
139
+ * if (pathParams.names.length === 0) {
140
+ * // Generate simple component without parameters
141
+ * return this.generateSimpleComponent(operation);
142
+ * }
143
+ *
144
+ * return `
145
+ * function ${operation.operationId}() {
146
+ * ${pathParams.getParams}
147
+ * ${pathParams.assertParams}
148
+ *
149
+ * return (
150
+ * <DetailView ${pathParams.passProps} />
151
+ * );
152
+ * }`;
153
+ * }
154
+ * }
155
+ * ```
156
+ *
157
+ * @example Custom parameter validation
158
+ * ```typescript
159
+ * class CustomReactRouterParams extends ReactRouterPathParams {
160
+ * generateCustomAssertions(paramName: string): string {
161
+ * return `
162
+ * invariant(${paramName}, 'Expected ${paramName} to be defined');
163
+ * invariant(typeof ${paramName} === 'string', '${paramName} must be a string');
164
+ * invariant(${paramName}.length > 0, '${paramName} cannot be empty');`;
165
+ * }
166
+ * }
167
+ * ```
168
+ */
11
169
  export declare class ReactRouterPathParams extends ContentBase {
170
+ /** Generated code for extracting parameters from useParams() */
12
171
  getParams: string;
172
+ /** Generated code for runtime parameter assertions */
13
173
  assertParams: string;
174
+ /** Generated code for passing parameters as component props */
14
175
  passProps: string;
176
+ /** Array of parameter names extracted from the operation path */
15
177
  names: string[];
178
+ /**
179
+ * Creates a new ReactRouterPathParams instance with generated parameter handling code.
180
+ *
181
+ * The constructor analyzes the OpenAPI operation to extract path parameters and
182
+ * generates the necessary React code for parameter extraction, validation, and
183
+ * prop passing. It automatically registers required imports when parameters are present.
184
+ *
185
+ * @param args - Configuration for React Router parameter generation
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const pathParams = new ReactRouterPathParams({
190
+ * context: generateContext,
191
+ * generatorKey: 'react-params',
192
+ * operation: userDetailOperation, // has path '/users/{id}'
193
+ * destinationPath: './UserDetail.tsx'
194
+ * });
195
+ * ```
196
+ */
16
197
  constructor({ context, operation, generatorKey, destinationPath }: CreateArgs);
198
+ /**
199
+ * Returns an empty string as this class provides code through its properties.
200
+ *
201
+ * The ReactRouterPathParams class doesn't generate output through toString(),
202
+ * instead providing generated code through the `getParams`, `assertParams`,
203
+ * and `passProps` properties which can be used directly in template generation.
204
+ *
205
+ * @returns Empty string
206
+ */
17
207
  toString(): string;
18
208
  }
19
209
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"ReactRouterPathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/ReactRouterPathParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAE7D,KAAK,UAAU,GAAG;IAChB,OAAO,EAAE,eAAe,CAAA;IACxB,YAAY,EAAE,YAAY,CAAA;IAC1B,SAAS,EAAE,YAAY,CAAA;IACvB,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AAED,qBAAa,qBAAsB,SAAQ,WAAW;IACpD,SAAS,EAAE,MAAM,CAAK;IAEtB,YAAY,EAAE,MAAM,CAAK;IAEzB,SAAS,EAAE,MAAM,CAAK;IAEtB,KAAK,EAAE,MAAM,EAAE,CAAA;gBAEH,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,EAAE,UAAU;IAwBpE,QAAQ,IAAI,MAAM;CAG5B"}
1
+ {"version":3,"file":"ReactRouterPathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/ReactRouterPathParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAE7D;;GAEG;AACH,KAAK,UAAU,GAAG;IAChB,+EAA+E;IAC/E,OAAO,EAAE,eAAe,CAAA;IACxB,mDAAmD;IACnD,YAAY,EAAE,YAAY,CAAA;IAC1B,uDAAuD;IACvD,SAAS,EAAE,YAAY,CAAA;IACvB,wDAAwD;IACxD,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsJG;AACH,qBAAa,qBAAsB,SAAQ,WAAW;IACpD,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAK;IAEtB,sDAAsD;IACtD,YAAY,EAAE,MAAM,CAAK;IAEzB,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAK;IAEtB,iEAAiE;IACjE,KAAK,EAAE,MAAM,EAAE,CAAA;IAEf;;;;;;;;;;;;;;;;;;OAkBG;gBACS,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,EAAE,UAAU;IAwB7E;;;;;;;;OAQG;IACM,QAAQ,IAAI,MAAM;CAG5B"}
@@ -1,25 +1,199 @@
1
1
  import { ContentBase } from '../dsl/ContentBase.js';
2
+ /**
3
+ * Generates React Router path parameter handling code for OpenAPI operations.
4
+ *
5
+ * `ReactRouterPathParams` is a specialized content generator that creates the necessary
6
+ * React code for extracting and validating path parameters from React Router's `useParams`
7
+ * hook. It automatically generates parameter extraction, assertion logic, and prop passing
8
+ * code based on OpenAPI operation path parameters.
9
+ *
10
+ * This class is essential for generating type-safe React components that need to access
11
+ * URL path parameters, ensuring runtime validation and proper TypeScript typing. It
12
+ * integrates with React Router's `useParams` hook and adds runtime assertions using
13
+ * the `tiny-invariant` library.
14
+ *
15
+ * ## Key Features
16
+ *
17
+ * - **Automatic Parameter Extraction**: Generates `useParams()` destructuring code
18
+ * - **Runtime Assertions**: Creates invariant checks for required parameters
19
+ * - **Prop Passing**: Generates prop spreading syntax for component integration
20
+ * - **Import Management**: Automatically registers required imports
21
+ * - **Type Safety**: Ensures parameters are properly validated at runtime
22
+ *
23
+ * @example Basic usage with single path parameter
24
+ * ```typescript
25
+ * import { ReactRouterPathParams } from '@skmtc/core';
26
+ *
27
+ * const operation = new OasOperation({
28
+ * path: '/users/{id}',
29
+ * method: 'get',
30
+ * // ... other operation properties
31
+ * });
32
+ *
33
+ * const pathParams = new ReactRouterPathParams({
34
+ * context: generateContext,
35
+ * generatorKey: 'react-router-params',
36
+ * operation,
37
+ * destinationPath: './UserDetail.tsx'
38
+ * });
39
+ *
40
+ * console.log(pathParams.getParams);
41
+ * // 'const { id } = useParams()'
42
+ *
43
+ * console.log(pathParams.assertParams);
44
+ * // 'invariant(id, 'Expected id to be defined')'
45
+ *
46
+ * console.log(pathParams.passProps);
47
+ * // 'id={id}'
48
+ * ```
49
+ *
50
+ * @example Multiple path parameters
51
+ * ```typescript
52
+ * const complexOperation = new OasOperation({
53
+ * path: '/organizations/{orgId}/projects/{projectId}/issues/{issueId}',
54
+ * method: 'get',
55
+ * // ... other properties
56
+ * });
57
+ *
58
+ * const multiParams = new ReactRouterPathParams({
59
+ * context: generateContext,
60
+ * generatorKey: 'react-router-params',
61
+ * operation: complexOperation,
62
+ * destinationPath: './IssueDetail.tsx'
63
+ * });
64
+ *
65
+ * console.log(multiParams.getParams);
66
+ * // 'const { orgId, projectId, issueId } = useParams()'
67
+ *
68
+ * console.log(multiParams.assertParams);
69
+ * // 'invariant(orgId, 'Expected orgId to be defined')
70
+ * // invariant(projectId, 'Expected projectId to be defined')
71
+ * // invariant(issueId, 'Expected issueId to be defined')'
72
+ *
73
+ * console.log(multiParams.passProps);
74
+ * // 'orgId={orgId} projectId={projectId} issueId={issueId}'
75
+ * ```
76
+ *
77
+ * @example Generated React component integration
78
+ * ```typescript
79
+ * // Generated component might look like:
80
+ * import { useParams } from 'react-router-dom';
81
+ * import invariant from 'tiny-invariant';
82
+ *
83
+ * function UserDetail() {
84
+ * const { id } = useParams();
85
+ * invariant(id, 'Expected id to be defined');
86
+ *
87
+ * return <UserDetailView id={id} />;
88
+ * }
89
+ * ```
90
+ *
91
+ * @example No path parameters (empty generation)
92
+ * ```typescript
93
+ * const simpleOperation = new OasOperation({
94
+ * path: '/users',
95
+ * method: 'get',
96
+ * // ... other properties
97
+ * });
98
+ *
99
+ * const emptyParams = new ReactRouterPathParams({
100
+ * context: generateContext,
101
+ * generatorKey: 'react-router-params',
102
+ * operation: simpleOperation,
103
+ * destinationPath: './UserList.tsx'
104
+ * });
105
+ *
106
+ * console.log(emptyParams.getParams); // ''
107
+ * console.log(emptyParams.assertParams); // ''
108
+ * console.log(emptyParams.passProps); // ''
109
+ * // No imports registered for this case
110
+ * ```
111
+ *
112
+ * @example Integration in component generation
113
+ * ```typescript
114
+ * class ReactComponentGenerator {
115
+ * generateDetailComponent(operation: OasOperation) {
116
+ * const pathParams = new ReactRouterPathParams({
117
+ * context: this.context,
118
+ * generatorKey: 'react-component',
119
+ * operation,
120
+ * destinationPath: this.getDestinationPath(operation)
121
+ * });
122
+ *
123
+ * if (pathParams.names.length === 0) {
124
+ * // Generate simple component without parameters
125
+ * return this.generateSimpleComponent(operation);
126
+ * }
127
+ *
128
+ * return `
129
+ * function ${operation.operationId}() {
130
+ * ${pathParams.getParams}
131
+ * ${pathParams.assertParams}
132
+ *
133
+ * return (
134
+ * <DetailView ${pathParams.passProps} />
135
+ * );
136
+ * }`;
137
+ * }
138
+ * }
139
+ * ```
140
+ *
141
+ * @example Custom parameter validation
142
+ * ```typescript
143
+ * class CustomReactRouterParams extends ReactRouterPathParams {
144
+ * generateCustomAssertions(paramName: string): string {
145
+ * return `
146
+ * invariant(${paramName}, 'Expected ${paramName} to be defined');
147
+ * invariant(typeof ${paramName} === 'string', '${paramName} must be a string');
148
+ * invariant(${paramName}.length > 0, '${paramName} cannot be empty');`;
149
+ * }
150
+ * }
151
+ * ```
152
+ */
2
153
  export class ReactRouterPathParams extends ContentBase {
154
+ /**
155
+ * Creates a new ReactRouterPathParams instance with generated parameter handling code.
156
+ *
157
+ * The constructor analyzes the OpenAPI operation to extract path parameters and
158
+ * generates the necessary React code for parameter extraction, validation, and
159
+ * prop passing. It automatically registers required imports when parameters are present.
160
+ *
161
+ * @param args - Configuration for React Router parameter generation
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * const pathParams = new ReactRouterPathParams({
166
+ * context: generateContext,
167
+ * generatorKey: 'react-params',
168
+ * operation: userDetailOperation, // has path '/users/{id}'
169
+ * destinationPath: './UserDetail.tsx'
170
+ * });
171
+ * ```
172
+ */
3
173
  constructor({ context, operation, generatorKey, destinationPath }) {
4
174
  super({ context, generatorKey });
175
+ /** Generated code for extracting parameters from useParams() */
5
176
  Object.defineProperty(this, "getParams", {
6
177
  enumerable: true,
7
178
  configurable: true,
8
179
  writable: true,
9
180
  value: ''
10
181
  });
182
+ /** Generated code for runtime parameter assertions */
11
183
  Object.defineProperty(this, "assertParams", {
12
184
  enumerable: true,
13
185
  configurable: true,
14
186
  writable: true,
15
187
  value: ''
16
188
  });
189
+ /** Generated code for passing parameters as component props */
17
190
  Object.defineProperty(this, "passProps", {
18
191
  enumerable: true,
19
192
  configurable: true,
20
193
  writable: true,
21
194
  value: ''
22
195
  });
196
+ /** Array of parameter names extracted from the operation path */
23
197
  Object.defineProperty(this, "names", {
24
198
  enumerable: true,
25
199
  configurable: true,
@@ -43,6 +217,15 @@ export class ReactRouterPathParams extends ContentBase {
43
217
  });
44
218
  }
45
219
  }
220
+ /**
221
+ * Returns an empty string as this class provides code through its properties.
222
+ *
223
+ * The ReactRouterPathParams class doesn't generate output through toString(),
224
+ * instead providing generated code through the `getParams`, `assertParams`,
225
+ * and `passProps` properties which can be used directly in template generation.
226
+ *
227
+ * @returns Empty string
228
+ */
46
229
  toString() {
47
230
  return ``;
48
231
  }
@@ -1,3 +1,128 @@
1
1
  import type { Stringable } from '../dsl/Stringable.js';
2
+ /**
3
+ * Converts a properties object into a formatted TypeScript object literal string.
4
+ *
5
+ * This utility function takes a record of key-value pairs and generates a properly
6
+ * formatted TypeScript object literal. It automatically filters out empty values
7
+ * and handles proper spacing and comma placement. If all properties are empty,
8
+ * it returns the EMPTY constant instead of an empty object.
9
+ *
10
+ * The function is particularly useful for generating TypeScript interface properties,
11
+ * object type definitions, and literal object expressions in code generation scenarios.
12
+ *
13
+ * @param properties - Record of property names to their stringable values
14
+ * @returns Formatted object literal string or EMPTY if no valid properties
15
+ *
16
+ * @example Basic object generation
17
+ * ```typescript
18
+ * import { keyValues } from '@skmtc/core';
19
+ *
20
+ * const props = {
21
+ * name: { toString: () => 'string' },
22
+ * age: { toString: () => 'number' },
23
+ * email: { toString: () => 'string' }
24
+ * };
25
+ *
26
+ * const result = keyValues(props);
27
+ * console.log(result);
28
+ * // {name: string,
29
+ * // age: number,
30
+ * // email: string}
31
+ * ```
32
+ *
33
+ * @example With empty values filtered
34
+ * ```typescript
35
+ * const propsWithEmpty = {
36
+ * name: { toString: () => 'string' },
37
+ * description: { toString: () => '' }, // Empty, will be filtered
38
+ * age: { toString: () => 'number' },
39
+ * notes: { toString: () => 'EMPTY' } // EMPTY constant, will be filtered
40
+ * };
41
+ *
42
+ * const result = keyValues(propsWithEmpty);
43
+ * console.log(result);
44
+ * // {name: string,
45
+ * // age: number}
46
+ * ```
47
+ *
48
+ * @example All empty properties
49
+ * ```typescript
50
+ * const emptyProps = {
51
+ * description: { toString: () => '' },
52
+ * notes: { toString: () => 'EMPTY' }
53
+ * };
54
+ *
55
+ * const result = keyValues(emptyProps);
56
+ * console.log(result === EMPTY); // true
57
+ * ```
58
+ *
59
+ * @example TypeScript interface generation
60
+ * ```typescript
61
+ * class InterfaceGenerator {
62
+ * generateInterface(name: string, properties: Record<string, Stringable>) {
63
+ * const propsString = keyValues(properties);
64
+ *
65
+ * if (propsString === EMPTY) {
66
+ * return `interface ${name} {}`;
67
+ * }
68
+ *
69
+ * return `interface ${name} ${propsString}`;
70
+ * }
71
+ * }
72
+ *
73
+ * const generator = new InterfaceGenerator();
74
+ * const userInterface = generator.generateInterface('User', {
75
+ * id: { toString: () => 'string' },
76
+ * name: { toString: () => 'string' },
77
+ * age: { toString: () => 'number' }
78
+ * });
79
+ *
80
+ * console.log(userInterface);
81
+ * // interface User {id: string,
82
+ * // name: string,
83
+ * // age: number}
84
+ * ```
85
+ *
86
+ * @example Object literal generation
87
+ * ```typescript
88
+ * function generateObjectLiteral(values: Record<string, string>) {
89
+ * const stringableValues = Object.fromEntries(
90
+ * Object.entries(values).map(([k, v]) => [k, { toString: () => `'${v}'` }])
91
+ * );
92
+ *
93
+ * const result = keyValues(stringableValues);
94
+ * return result === EMPTY ? '{}' : result;
95
+ * }
96
+ *
97
+ * const config = generateObjectLiteral({
98
+ * apiUrl: 'https://api.example.com',
99
+ * timeout: '5000'
100
+ * });
101
+ *
102
+ * console.log(config);
103
+ * // {apiUrl: 'https://api.example.com',
104
+ * // timeout: '5000'}
105
+ * ```
106
+ *
107
+ * @example Conditional property inclusion
108
+ * ```typescript
109
+ * class ConditionalGenerator {
110
+ * generateObjectType(fields: Array<{name: string, type: string, optional?: boolean}>) {
111
+ * const properties: Record<string, Stringable> = {};
112
+ *
113
+ * fields.forEach(field => {
114
+ * if (field.type) { // Only include fields with types
115
+ * const optionalMarker = field.optional ? '?' : '';
116
+ * properties[field.name] = {
117
+ * toString: () => `${optionalMarker}: ${field.type}`
118
+ * };
119
+ * }
120
+ * });
121
+ *
122
+ * return keyValues(properties);
123
+ * }
124
+ * }
125
+ * ```
126
+ */
2
127
  export declare const keyValues: (properties: Record<string, Stringable>) => string;
3
128
  //# sourceMappingURL=keyValues.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"keyValues.d.ts","sourceRoot":"","sources":["../../src/typescript/keyValues.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAGtD,eAAO,MAAM,SAAS,GAAI,YAAY,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,KAAG,MAUlE,CAAA"}
1
+ {"version":3,"file":"keyValues.d.ts","sourceRoot":"","sources":["../../src/typescript/keyValues.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4HG;AACH,eAAO,MAAM,SAAS,GAAI,YAAY,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,KAAG,MAUlE,CAAA"}
@@ -1,5 +1,129 @@
1
1
  import { EMPTY } from '../dsl/constants.js';
2
- // Filter out properties with falsy or EMPTY values and return as joined string
2
+ /**
3
+ * Converts a properties object into a formatted TypeScript object literal string.
4
+ *
5
+ * This utility function takes a record of key-value pairs and generates a properly
6
+ * formatted TypeScript object literal. It automatically filters out empty values
7
+ * and handles proper spacing and comma placement. If all properties are empty,
8
+ * it returns the EMPTY constant instead of an empty object.
9
+ *
10
+ * The function is particularly useful for generating TypeScript interface properties,
11
+ * object type definitions, and literal object expressions in code generation scenarios.
12
+ *
13
+ * @param properties - Record of property names to their stringable values
14
+ * @returns Formatted object literal string or EMPTY if no valid properties
15
+ *
16
+ * @example Basic object generation
17
+ * ```typescript
18
+ * import { keyValues } from '@skmtc/core';
19
+ *
20
+ * const props = {
21
+ * name: { toString: () => 'string' },
22
+ * age: { toString: () => 'number' },
23
+ * email: { toString: () => 'string' }
24
+ * };
25
+ *
26
+ * const result = keyValues(props);
27
+ * console.log(result);
28
+ * // {name: string,
29
+ * // age: number,
30
+ * // email: string}
31
+ * ```
32
+ *
33
+ * @example With empty values filtered
34
+ * ```typescript
35
+ * const propsWithEmpty = {
36
+ * name: { toString: () => 'string' },
37
+ * description: { toString: () => '' }, // Empty, will be filtered
38
+ * age: { toString: () => 'number' },
39
+ * notes: { toString: () => 'EMPTY' } // EMPTY constant, will be filtered
40
+ * };
41
+ *
42
+ * const result = keyValues(propsWithEmpty);
43
+ * console.log(result);
44
+ * // {name: string,
45
+ * // age: number}
46
+ * ```
47
+ *
48
+ * @example All empty properties
49
+ * ```typescript
50
+ * const emptyProps = {
51
+ * description: { toString: () => '' },
52
+ * notes: { toString: () => 'EMPTY' }
53
+ * };
54
+ *
55
+ * const result = keyValues(emptyProps);
56
+ * console.log(result === EMPTY); // true
57
+ * ```
58
+ *
59
+ * @example TypeScript interface generation
60
+ * ```typescript
61
+ * class InterfaceGenerator {
62
+ * generateInterface(name: string, properties: Record<string, Stringable>) {
63
+ * const propsString = keyValues(properties);
64
+ *
65
+ * if (propsString === EMPTY) {
66
+ * return `interface ${name} {}`;
67
+ * }
68
+ *
69
+ * return `interface ${name} ${propsString}`;
70
+ * }
71
+ * }
72
+ *
73
+ * const generator = new InterfaceGenerator();
74
+ * const userInterface = generator.generateInterface('User', {
75
+ * id: { toString: () => 'string' },
76
+ * name: { toString: () => 'string' },
77
+ * age: { toString: () => 'number' }
78
+ * });
79
+ *
80
+ * console.log(userInterface);
81
+ * // interface User {id: string,
82
+ * // name: string,
83
+ * // age: number}
84
+ * ```
85
+ *
86
+ * @example Object literal generation
87
+ * ```typescript
88
+ * function generateObjectLiteral(values: Record<string, string>) {
89
+ * const stringableValues = Object.fromEntries(
90
+ * Object.entries(values).map(([k, v]) => [k, { toString: () => `'${v}'` }])
91
+ * );
92
+ *
93
+ * const result = keyValues(stringableValues);
94
+ * return result === EMPTY ? '{}' : result;
95
+ * }
96
+ *
97
+ * const config = generateObjectLiteral({
98
+ * apiUrl: 'https://api.example.com',
99
+ * timeout: '5000'
100
+ * });
101
+ *
102
+ * console.log(config);
103
+ * // {apiUrl: 'https://api.example.com',
104
+ * // timeout: '5000'}
105
+ * ```
106
+ *
107
+ * @example Conditional property inclusion
108
+ * ```typescript
109
+ * class ConditionalGenerator {
110
+ * generateObjectType(fields: Array<{name: string, type: string, optional?: boolean}>) {
111
+ * const properties: Record<string, Stringable> = {};
112
+ *
113
+ * fields.forEach(field => {
114
+ * if (field.type) { // Only include fields with types
115
+ * const optionalMarker = field.optional ? '?' : '';
116
+ * properties[field.name] = {
117
+ * toString: () => `${optionalMarker}: ${field.type}`
118
+ * };
119
+ * }
120
+ * });
121
+ *
122
+ * return keyValues(properties);
123
+ * }
124
+ * }
125
+ * ```
126
+ */
3
127
  export const keyValues = (properties) => {
4
128
  const filteredEntries = Object.entries(properties)
5
129
  .map(([key, value]) => {