Writing code

FUNCTION

sanitizePropertyName

@skmtc/lang-typescript 0.19.1

Makes a property name into one item of an object destructuring pattern. It adds a variable name for a name that is not an identifier, and for some keywords. To make a name for a declaration, use sanitizeIdentifier.

Some names give a pattern that does not parse:

  • Some reserved words, such as class and import, get no new name.
  • The properties of Object.prototype, such as constructor and toString, get the source of a function as their variable.
  • For other names, the variable is not valid: 1st gives '1st': 1st, -default gives '-default': default, and - gives no variable.

Signature

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

sanitizePropertyName(propertyName: string): string | Stringable

Parameters

propertyName, string required
Destructure the properties of an object
import { List, sanitizePropertyName } from '@skmtc/lang-typescript'

const names = { id: true, 'my-prop': true, default: true }

List.fromKeys(names).toObject(sanitizePropertyName).toString()
// {id, 'my-prop': myProp, default: defaultValue}

Returns

string | Stringable