Writing code
FUNCTION
@skmtc/lang-typescript 0.19.1keyValues
Converts a properties object into a formatted TypeScript object literal string.
This utility function takes a record of key-value pairs and generates a properly formatted TypeScript object literal. It automatically filters out empty values and handles proper spacing and comma placement. If all properties are empty, it returns the EMPTY constant instead of an empty object.
The function is particularly useful for generating TypeScript interface properties, object type definitions, and literal object expressions in code generation scenarios.
Signature
import { keyValues } from '@skmtc/lang-typescript'
keyValues(properties: Record<string, Stringable>): stringParameters
Record of property names to their stringable values
Basic object generation
import { keyValues } from '@skmtc/lang-typescript';
const props = {
name: { toString: () => 'string' },
age: { toString: () => 'number' },
email: { toString: () => 'string' }
};
const result = keyValues(props);
console.log(result);
// {name: string,
// age: number,
// email: string}With empty values filtered
const propsWithEmpty = {
name: { toString: () => 'string' },
description: { toString: () => '' }, // Empty, will be filtered
age: { toString: () => 'number' },
notes: { toString: () => 'EMPTY' } // EMPTY constant, will be filtered
};
const result = keyValues(propsWithEmpty);
console.log(result);
// {name: string,
// age: number}All empty properties
const emptyProps = {
description: { toString: () => '' },
notes: { toString: () => 'EMPTY' }
};
const result = keyValues(emptyProps);
console.log(result === EMPTY); // trueTypeScript interface generation
class InterfaceGenerator {
generateInterface(name: string, properties: Record<string, Stringable>) {
const propsString = keyValues(properties);
if (propsString === EMPTY) {
return `interface ${name} {}`;
}
return `interface ${name} ${propsString}`;
}
}
const generator = new InterfaceGenerator();
const userInterface = generator.generateInterface('User', {
id: { toString: () => 'string' },
name: { toString: () => 'string' },
age: { toString: () => 'number' }
});
console.log(userInterface);
// interface User {id: string,
// name: string,
// age: number}Object literal generation
function generateObjectLiteral(values: Record<string, string>) {
const stringableValues = Object.fromEntries(
Object.entries(values).map(([k, v]) => [k, { toString: () => `'${v}'` }])
);
const result = keyValues(stringableValues);
return result === EMPTY ? '{}' : result;
}
const config = generateObjectLiteral({
apiUrl: 'https://api.example.com',
timeout: '5000'
});
console.log(config);
// {apiUrl: 'https://api.example.com',
// timeout: '5000'}Conditional property inclusion
class ConditionalGenerator {
generateObjectType(fields: Array<{name: string, type: string, optional?: boolean}>) {
const properties: Record<string, Stringable> = {};
fields.forEach(field => {
if (field.type) { // Only include fields with types
const optionalMarker = field.optional ? '?' : '';
properties[field.name] = {
toString: () => `${optionalMarker}: ${field.type}`
};
}
});
return keyValues(properties);
}
}Returns
string
Formatted object literal string or EMPTY if no valid properties