Writing code
FUNCTION
@skmtc/lang-typescript 0.19.1sanitizePropertyName
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
classandimport, get no new name. - The properties of
Object.prototype, such asconstructorandtoString, get the source of a function as their variable. - For other names, the variable is not valid:
1stgives'1st': 1st,-defaultgives'-default': default, and-gives no variable.
Signature
import { sanitizePropertyName } from '@skmtc/lang-typescript'
sanitizePropertyName(propertyName: string): string | StringableParameters
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