Writing code

FUNCTION

handleKey

@skmtc/lang-typescript 0.19.1

Safely formats a key for use in object literals, adding quotes if needed.

This function determines whether a property key needs to be quoted in JavaScript/TypeScript object literals based on identifier naming rules. Valid identifiers are left unquoted, while invalid identifiers (containing spaces, special characters, starting with numbers, etc.) are wrapped in single quotes, with \, ' and line terminators escaped.

Signature

import { handleKey } from '@skmtc/lang-typescript'

handleKey(key: string): string

Parameters

key, string required

The property key to format

Valid identifiers (no quotes needed)
import { handleKey } from '@skmtc/lang-typescript';

console.log(handleKey('name'));      // 'name'
console.log(handleKey('userName'));  // 'userName'
console.log(handleKey('_private'));  // '_private'
console.log(handleKey('$special'));  // '$special'
Invalid identifiers (quotes added)
console.log(handleKey('first-name'));  // "'first-name'"
console.log(handleKey('2nd-place'));   // "'2nd-place'"
console.log(handleKey('user name'));   // "'user name'"
console.log(handleKey('api-key'));     // "'api-key'"
console.log(handleKey('data.nested')); // "'data.nested'"
Usage in object generation
class ObjectGenerator {
  generateObjectLiteral(properties: Record<string, string>) {
    const props = Object.entries(properties)
      .map(([key, value]) => `${handleKey(key)}: ${value}`)
      .join(', ');

    return `{ ${props} }`;
  }
}

const generator = new ObjectGenerator();
const result = generator.generateObjectLiteral({
  'name': 'string',
  'first-name': 'string',
  'age': 'number'
});

console.log(result);
// { name: string, 'first-name': string, age: number }
TypeScript interface generation
function generateInterface(name: string, properties: Record<string, string>) {
  const props = Object.entries(properties)
    .map(([key, type]) => `  ${handleKey(key)}: ${type};`)
    .join('\n');

  return `interface ${name} {\n${props}\n}`;
}

const userInterface = generateInterface('User', {
  'id': 'string',
  'first-name': 'string',
  'last_name': 'string',
  '2fa-enabled': 'boolean'
});

console.log(userInterface);
// interface User {
//   id: string;
//   'first-name': string;
//   last_name: string;
//   '2fa-enabled': boolean;
// }

Returns

string

The key as-is if it's a valid identifier, otherwise wrapped in quotes