@skmtc/core 0.0.792 → 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 (113) hide show
  1. package/esm/dsl/GeneratedValueList.d.ts +158 -0
  2. package/esm/dsl/GeneratedValueList.d.ts.map +1 -1
  3. package/esm/dsl/GeneratedValueList.js +158 -0
  4. package/esm/dsl/Stringable.d.ts +76 -0
  5. package/esm/dsl/Stringable.d.ts.map +1 -1
  6. package/esm/dsl/Stringable.js +75 -0
  7. package/esm/helpers/ResultsLog.d.ts +193 -0
  8. package/esm/helpers/ResultsLog.d.ts.map +1 -1
  9. package/esm/helpers/ResultsLog.js +213 -0
  10. package/esm/helpers/collateExamples.d.ts +150 -0
  11. package/esm/helpers/collateExamples.d.ts.map +1 -1
  12. package/esm/helpers/collateExamples.js +145 -0
  13. package/esm/helpers/formatNumber.d.ts +95 -0
  14. package/esm/helpers/formatNumber.d.ts.map +1 -1
  15. package/esm/helpers/formatNumber.js +95 -0
  16. package/esm/helpers/generationStats.d.ts +287 -1
  17. package/esm/helpers/generationStats.d.ts.map +1 -1
  18. package/esm/helpers/generationStats.js +266 -0
  19. package/esm/helpers/isEmpty.d.ts +62 -0
  20. package/esm/helpers/isEmpty.d.ts.map +1 -1
  21. package/esm/helpers/isEmpty.js +62 -0
  22. package/esm/helpers/isImported.d.ts +80 -0
  23. package/esm/helpers/isImported.d.ts.map +1 -1
  24. package/esm/helpers/isImported.js +80 -0
  25. package/esm/helpers/parseModuleName.d.ts +175 -0
  26. package/esm/helpers/parseModuleName.d.ts.map +1 -1
  27. package/esm/helpers/parseModuleName.js +153 -0
  28. package/esm/helpers/toResolvedArtifactPath.d.ts +145 -0
  29. package/esm/helpers/toResolvedArtifactPath.d.ts.map +1 -1
  30. package/esm/helpers/toResolvedArtifactPath.js +140 -0
  31. package/esm/helpers/tracer.d.ts +124 -0
  32. package/esm/helpers/tracer.d.ts.map +1 -1
  33. package/esm/helpers/tracer.js +124 -0
  34. package/esm/oas/schema/Schema.d.ts +117 -0
  35. package/esm/oas/schema/Schema.d.ts.map +1 -1
  36. package/esm/typescript/PathParams.d.ts +198 -0
  37. package/esm/typescript/PathParams.d.ts.map +1 -1
  38. package/esm/typescript/PathParams.js +190 -0
  39. package/esm/typescript/ReactRouterPathParams.d.ts +190 -0
  40. package/esm/typescript/ReactRouterPathParams.d.ts.map +1 -1
  41. package/esm/typescript/ReactRouterPathParams.js +183 -0
  42. package/esm/typescript/identifiers.d.ts +159 -0
  43. package/esm/typescript/identifiers.d.ts.map +1 -1
  44. package/esm/typescript/identifiers.js +159 -0
  45. package/esm/typescript/keyValues.d.ts +125 -0
  46. package/esm/typescript/keyValues.d.ts.map +1 -1
  47. package/esm/typescript/keyValues.js +125 -1
  48. package/esm/typescript/toPathParams.d.ts +179 -0
  49. package/esm/typescript/toPathParams.d.ts.map +1 -1
  50. package/esm/typescript/toPathParams.js +179 -0
  51. package/esm/typescript/toPathTemplate.d.ts +114 -0
  52. package/esm/typescript/toPathTemplate.d.ts.map +1 -1
  53. package/esm/typescript/toPathTemplate.js +114 -0
  54. package/esm/typescript/withDescription.d.ts +119 -0
  55. package/esm/typescript/withDescription.d.ts.map +1 -1
  56. package/esm/typescript/withDescription.js +119 -0
  57. package/package.json +1 -1
  58. package/script/dsl/GeneratedValueList.d.ts +158 -0
  59. package/script/dsl/GeneratedValueList.d.ts.map +1 -1
  60. package/script/dsl/GeneratedValueList.js +158 -0
  61. package/script/dsl/Stringable.d.ts +76 -0
  62. package/script/dsl/Stringable.d.ts.map +1 -1
  63. package/script/dsl/Stringable.js +75 -0
  64. package/script/helpers/ResultsLog.d.ts +193 -0
  65. package/script/helpers/ResultsLog.d.ts.map +1 -1
  66. package/script/helpers/ResultsLog.js +213 -0
  67. package/script/helpers/collateExamples.d.ts +150 -0
  68. package/script/helpers/collateExamples.d.ts.map +1 -1
  69. package/script/helpers/collateExamples.js +145 -0
  70. package/script/helpers/formatNumber.d.ts +95 -0
  71. package/script/helpers/formatNumber.d.ts.map +1 -1
  72. package/script/helpers/formatNumber.js +95 -0
  73. package/script/helpers/generationStats.d.ts +287 -1
  74. package/script/helpers/generationStats.d.ts.map +1 -1
  75. package/script/helpers/generationStats.js +266 -0
  76. package/script/helpers/isEmpty.d.ts +62 -0
  77. package/script/helpers/isEmpty.d.ts.map +1 -1
  78. package/script/helpers/isEmpty.js +62 -0
  79. package/script/helpers/isImported.d.ts +80 -0
  80. package/script/helpers/isImported.d.ts.map +1 -1
  81. package/script/helpers/isImported.js +80 -0
  82. package/script/helpers/parseModuleName.d.ts +175 -0
  83. package/script/helpers/parseModuleName.d.ts.map +1 -1
  84. package/script/helpers/parseModuleName.js +153 -0
  85. package/script/helpers/toResolvedArtifactPath.d.ts +145 -0
  86. package/script/helpers/toResolvedArtifactPath.d.ts.map +1 -1
  87. package/script/helpers/toResolvedArtifactPath.js +140 -0
  88. package/script/helpers/tracer.d.ts +124 -0
  89. package/script/helpers/tracer.d.ts.map +1 -1
  90. package/script/helpers/tracer.js +124 -0
  91. package/script/oas/schema/Schema.d.ts +117 -0
  92. package/script/oas/schema/Schema.d.ts.map +1 -1
  93. package/script/typescript/PathParams.d.ts +198 -0
  94. package/script/typescript/PathParams.d.ts.map +1 -1
  95. package/script/typescript/PathParams.js +190 -0
  96. package/script/typescript/ReactRouterPathParams.d.ts +190 -0
  97. package/script/typescript/ReactRouterPathParams.d.ts.map +1 -1
  98. package/script/typescript/ReactRouterPathParams.js +183 -0
  99. package/script/typescript/identifiers.d.ts +159 -0
  100. package/script/typescript/identifiers.d.ts.map +1 -1
  101. package/script/typescript/identifiers.js +159 -0
  102. package/script/typescript/keyValues.d.ts +125 -0
  103. package/script/typescript/keyValues.d.ts.map +1 -1
  104. package/script/typescript/keyValues.js +125 -1
  105. package/script/typescript/toPathParams.d.ts +179 -0
  106. package/script/typescript/toPathParams.d.ts.map +1 -1
  107. package/script/typescript/toPathParams.js +179 -0
  108. package/script/typescript/toPathTemplate.d.ts +114 -0
  109. package/script/typescript/toPathTemplate.d.ts.map +1 -1
  110. package/script/typescript/toPathTemplate.js +114 -0
  111. package/script/typescript/withDescription.d.ts +119 -0
  112. package/script/typescript/withDescription.d.ts.map +1 -1
  113. package/script/typescript/withDescription.js +119 -0
@@ -1,3 +1,162 @@
1
+ /**
2
+ * Safely formats a key for use in object literals, adding quotes if needed.
3
+ *
4
+ * This function determines whether a property key needs to be quoted in JavaScript/TypeScript
5
+ * object literals based on identifier naming rules. Valid identifiers are left unquoted,
6
+ * while invalid identifiers (containing spaces, special characters, starting with numbers, etc.)
7
+ * are wrapped in single quotes.
8
+ *
9
+ * @param key - The property key to format
10
+ * @returns The key as-is if it's a valid identifier, otherwise wrapped in quotes
11
+ *
12
+ * @example Valid identifiers (no quotes needed)
13
+ * ```typescript
14
+ * import { handleKey } from '@skmtc/core';
15
+ *
16
+ * console.log(handleKey('name')); // 'name'
17
+ * console.log(handleKey('userName')); // 'userName'
18
+ * console.log(handleKey('_private')); // '_private'
19
+ * console.log(handleKey('$special')); // '$special'
20
+ * ```
21
+ *
22
+ * @example Invalid identifiers (quotes added)
23
+ * ```typescript
24
+ * console.log(handleKey('first-name')); // "'first-name'"
25
+ * console.log(handleKey('2nd-place')); // "'2nd-place'"
26
+ * console.log(handleKey('user name')); // "'user name'"
27
+ * console.log(handleKey('api-key')); // "'api-key'"
28
+ * console.log(handleKey('data.nested')); // "'data.nested'"
29
+ * ```
30
+ *
31
+ * @example Usage in object generation
32
+ * ```typescript
33
+ * class ObjectGenerator {
34
+ * generateObjectLiteral(properties: Record<string, string>) {
35
+ * const props = Object.entries(properties)
36
+ * .map(([key, value]) => `${handleKey(key)}: ${value}`)
37
+ * .join(', ');
38
+ *
39
+ * return `{ ${props} }`;
40
+ * }
41
+ * }
42
+ *
43
+ * const generator = new ObjectGenerator();
44
+ * const result = generator.generateObjectLiteral({
45
+ * 'name': 'string',
46
+ * 'first-name': 'string',
47
+ * 'age': 'number'
48
+ * });
49
+ *
50
+ * console.log(result);
51
+ * // { name: string, 'first-name': string, age: number }
52
+ * ```
53
+ *
54
+ * @example TypeScript interface generation
55
+ * ```typescript
56
+ * function generateInterface(name: string, properties: Record<string, string>) {
57
+ * const props = Object.entries(properties)
58
+ * .map(([key, type]) => ` ${handleKey(key)}: ${type};`)
59
+ * .join('\n');
60
+ *
61
+ * return `interface ${name} {\n${props}\n}`;
62
+ * }
63
+ *
64
+ * const userInterface = generateInterface('User', {
65
+ * 'id': 'string',
66
+ * 'first-name': 'string',
67
+ * 'last_name': 'string',
68
+ * '2fa-enabled': 'boolean'
69
+ * });
70
+ *
71
+ * console.log(userInterface);
72
+ * // interface User {
73
+ * // id: string;
74
+ * // 'first-name': string;
75
+ * // last_name: string;
76
+ * // '2fa-enabled': boolean;
77
+ * // }
78
+ * ```
79
+ */
1
80
  export declare const handleKey: (key: string) => string;
81
+ /**
82
+ * Safely formats property access, using dot notation or bracket notation as appropriate.
83
+ *
84
+ * This function generates the appropriate property access syntax for JavaScript/TypeScript
85
+ * code. Valid identifiers use dot notation (`parent.property`), while invalid identifiers
86
+ * use bracket notation (`parent['property']`).
87
+ *
88
+ * @param name - The property name to access
89
+ * @param parent - The parent object expression
90
+ * @returns Properly formatted property access expression
91
+ *
92
+ * @example Valid identifiers (dot notation)
93
+ * ```typescript
94
+ * import { handlePropertyName } from '@skmtc/core';
95
+ *
96
+ * console.log(handlePropertyName('name', 'user')); // 'user.name'
97
+ * console.log(handlePropertyName('firstName', 'user')); // 'user.firstName'
98
+ * console.log(handlePropertyName('_id', 'document')); // 'document._id'
99
+ * console.log(handlePropertyName('$meta', 'data')); // 'data.$meta'
100
+ * ```
101
+ *
102
+ * @example Invalid identifiers (bracket notation)
103
+ * ```typescript
104
+ * console.log(handlePropertyName('first-name', 'user')); // "user['first-name']"
105
+ * console.log(handlePropertyName('2nd-place', 'result')); // "result['2nd-place']"
106
+ * console.log(handlePropertyName('user name', 'form')); // "form['user name']"
107
+ * console.log(handlePropertyName('api-key', 'config')); // "config['api-key']"
108
+ * ```
109
+ *
110
+ * @example Usage in accessor generation
111
+ * ```typescript
112
+ * class PropertyAccessor {
113
+ * generateGetter(objectName: string, propertyPath: string[]) {
114
+ * return propertyPath.reduce((acc, prop) =>
115
+ * handlePropertyName(prop, acc), objectName
116
+ * );
117
+ * }
118
+ * }
119
+ *
120
+ * const accessor = new PropertyAccessor();
121
+ * const path1 = accessor.generateGetter('data', ['user', 'profile', 'firstName']);
122
+ * const path2 = accessor.generateGetter('config', ['api-settings', 'retry-count']);
123
+ *
124
+ * console.log(path1); // 'data.user.profile.firstName'
125
+ * console.log(path2); // "config['api-settings']['retry-count']"
126
+ * ```
127
+ *
128
+ * @example Complex object navigation
129
+ * ```typescript
130
+ * function generateObjectAccess(base: string, properties: string[]) {
131
+ * let result = base;
132
+ *
133
+ * for (const prop of properties) {
134
+ * result = handlePropertyName(prop, result);
135
+ * }
136
+ *
137
+ * return result;
138
+ * }
139
+ *
140
+ * const access1 = generateObjectAccess('response', ['data', 'user-info', 'name']);
141
+ * const access2 = generateObjectAccess('settings', ['ui', 'theme', 'darkMode']);
142
+ *
143
+ * console.log(access1); // "response.data['user-info'].name"
144
+ * console.log(access2); // "settings.ui.theme.darkMode"
145
+ * ```
146
+ *
147
+ * @example Function generation
148
+ * ```typescript
149
+ * function generatePropertyGetter(objName: string, propName: string) {
150
+ * const access = handlePropertyName(propName, objName);
151
+ * return `function get${propName.replace(/[^a-zA-Z0-9]/g, '')}() { return ${access}; }`;
152
+ * }
153
+ *
154
+ * const getter1 = generatePropertyGetter('user', 'firstName');
155
+ * const getter2 = generatePropertyGetter('config', 'api-key');
156
+ *
157
+ * console.log(getter1); // "function getfirstName() { return user.firstName; }"
158
+ * console.log(getter2); // "function getapikey() { return config['api-key']; }"
159
+ * ```
160
+ */
2
161
  export declare const handlePropertyName: (name: string, parent: string) => string;
3
162
  //# sourceMappingURL=identifiers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"identifiers.d.ts","sourceRoot":"","sources":["../../src/typescript/identifiers.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,KAAG,MAEvC,CAAA;AAED,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,EAAE,QAAQ,MAAM,KAAG,MAEjE,CAAA"}
1
+ {"version":3,"file":"identifiers.d.ts","sourceRoot":"","sources":["../../src/typescript/identifiers.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8EG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,KAAG,MAEvC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+EG;AACH,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,EAAE,QAAQ,MAAM,KAAG,MAEjE,CAAA"}
@@ -1,7 +1,166 @@
1
1
  import { isIdentifierName } from '@babel/helper-validator-identifier';
2
+ /**
3
+ * Safely formats a key for use in object literals, adding quotes if needed.
4
+ *
5
+ * This function determines whether a property key needs to be quoted in JavaScript/TypeScript
6
+ * object literals based on identifier naming rules. Valid identifiers are left unquoted,
7
+ * while invalid identifiers (containing spaces, special characters, starting with numbers, etc.)
8
+ * are wrapped in single quotes.
9
+ *
10
+ * @param key - The property key to format
11
+ * @returns The key as-is if it's a valid identifier, otherwise wrapped in quotes
12
+ *
13
+ * @example Valid identifiers (no quotes needed)
14
+ * ```typescript
15
+ * import { handleKey } from '@skmtc/core';
16
+ *
17
+ * console.log(handleKey('name')); // 'name'
18
+ * console.log(handleKey('userName')); // 'userName'
19
+ * console.log(handleKey('_private')); // '_private'
20
+ * console.log(handleKey('$special')); // '$special'
21
+ * ```
22
+ *
23
+ * @example Invalid identifiers (quotes added)
24
+ * ```typescript
25
+ * console.log(handleKey('first-name')); // "'first-name'"
26
+ * console.log(handleKey('2nd-place')); // "'2nd-place'"
27
+ * console.log(handleKey('user name')); // "'user name'"
28
+ * console.log(handleKey('api-key')); // "'api-key'"
29
+ * console.log(handleKey('data.nested')); // "'data.nested'"
30
+ * ```
31
+ *
32
+ * @example Usage in object generation
33
+ * ```typescript
34
+ * class ObjectGenerator {
35
+ * generateObjectLiteral(properties: Record<string, string>) {
36
+ * const props = Object.entries(properties)
37
+ * .map(([key, value]) => `${handleKey(key)}: ${value}`)
38
+ * .join(', ');
39
+ *
40
+ * return `{ ${props} }`;
41
+ * }
42
+ * }
43
+ *
44
+ * const generator = new ObjectGenerator();
45
+ * const result = generator.generateObjectLiteral({
46
+ * 'name': 'string',
47
+ * 'first-name': 'string',
48
+ * 'age': 'number'
49
+ * });
50
+ *
51
+ * console.log(result);
52
+ * // { name: string, 'first-name': string, age: number }
53
+ * ```
54
+ *
55
+ * @example TypeScript interface generation
56
+ * ```typescript
57
+ * function generateInterface(name: string, properties: Record<string, string>) {
58
+ * const props = Object.entries(properties)
59
+ * .map(([key, type]) => ` ${handleKey(key)}: ${type};`)
60
+ * .join('\n');
61
+ *
62
+ * return `interface ${name} {\n${props}\n}`;
63
+ * }
64
+ *
65
+ * const userInterface = generateInterface('User', {
66
+ * 'id': 'string',
67
+ * 'first-name': 'string',
68
+ * 'last_name': 'string',
69
+ * '2fa-enabled': 'boolean'
70
+ * });
71
+ *
72
+ * console.log(userInterface);
73
+ * // interface User {
74
+ * // id: string;
75
+ * // 'first-name': string;
76
+ * // last_name: string;
77
+ * // '2fa-enabled': boolean;
78
+ * // }
79
+ * ```
80
+ */
2
81
  export const handleKey = (key) => {
3
82
  return isIdentifierName(key) ? key : `'${key}'`;
4
83
  };
84
+ /**
85
+ * Safely formats property access, using dot notation or bracket notation as appropriate.
86
+ *
87
+ * This function generates the appropriate property access syntax for JavaScript/TypeScript
88
+ * code. Valid identifiers use dot notation (`parent.property`), while invalid identifiers
89
+ * use bracket notation (`parent['property']`).
90
+ *
91
+ * @param name - The property name to access
92
+ * @param parent - The parent object expression
93
+ * @returns Properly formatted property access expression
94
+ *
95
+ * @example Valid identifiers (dot notation)
96
+ * ```typescript
97
+ * import { handlePropertyName } from '@skmtc/core';
98
+ *
99
+ * console.log(handlePropertyName('name', 'user')); // 'user.name'
100
+ * console.log(handlePropertyName('firstName', 'user')); // 'user.firstName'
101
+ * console.log(handlePropertyName('_id', 'document')); // 'document._id'
102
+ * console.log(handlePropertyName('$meta', 'data')); // 'data.$meta'
103
+ * ```
104
+ *
105
+ * @example Invalid identifiers (bracket notation)
106
+ * ```typescript
107
+ * console.log(handlePropertyName('first-name', 'user')); // "user['first-name']"
108
+ * console.log(handlePropertyName('2nd-place', 'result')); // "result['2nd-place']"
109
+ * console.log(handlePropertyName('user name', 'form')); // "form['user name']"
110
+ * console.log(handlePropertyName('api-key', 'config')); // "config['api-key']"
111
+ * ```
112
+ *
113
+ * @example Usage in accessor generation
114
+ * ```typescript
115
+ * class PropertyAccessor {
116
+ * generateGetter(objectName: string, propertyPath: string[]) {
117
+ * return propertyPath.reduce((acc, prop) =>
118
+ * handlePropertyName(prop, acc), objectName
119
+ * );
120
+ * }
121
+ * }
122
+ *
123
+ * const accessor = new PropertyAccessor();
124
+ * const path1 = accessor.generateGetter('data', ['user', 'profile', 'firstName']);
125
+ * const path2 = accessor.generateGetter('config', ['api-settings', 'retry-count']);
126
+ *
127
+ * console.log(path1); // 'data.user.profile.firstName'
128
+ * console.log(path2); // "config['api-settings']['retry-count']"
129
+ * ```
130
+ *
131
+ * @example Complex object navigation
132
+ * ```typescript
133
+ * function generateObjectAccess(base: string, properties: string[]) {
134
+ * let result = base;
135
+ *
136
+ * for (const prop of properties) {
137
+ * result = handlePropertyName(prop, result);
138
+ * }
139
+ *
140
+ * return result;
141
+ * }
142
+ *
143
+ * const access1 = generateObjectAccess('response', ['data', 'user-info', 'name']);
144
+ * const access2 = generateObjectAccess('settings', ['ui', 'theme', 'darkMode']);
145
+ *
146
+ * console.log(access1); // "response.data['user-info'].name"
147
+ * console.log(access2); // "settings.ui.theme.darkMode"
148
+ * ```
149
+ *
150
+ * @example Function generation
151
+ * ```typescript
152
+ * function generatePropertyGetter(objName: string, propName: string) {
153
+ * const access = handlePropertyName(propName, objName);
154
+ * return `function get${propName.replace(/[^a-zA-Z0-9]/g, '')}() { return ${access}; }`;
155
+ * }
156
+ *
157
+ * const getter1 = generatePropertyGetter('user', 'firstName');
158
+ * const getter2 = generatePropertyGetter('config', 'api-key');
159
+ *
160
+ * console.log(getter1); // "function getfirstName() { return user.firstName; }"
161
+ * console.log(getter2); // "function getapikey() { return config['api-key']; }"
162
+ * ```
163
+ */
5
164
  export const handlePropertyName = (name, parent) => {
6
165
  return isIdentifierName(name) ? `${parent}.${name}` : `${parent}['${name}']`;
7
166
  };
@@ -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]) => {