Writing code

FUNCTION

toStringLiteral

@skmtc/lang-typescript 0.19.1

Any string as a TypeScript string literal: wrapped in single quotes, with \, ', \n, \r, U+2028 and U+2029 escaped. Use it wherever a schema string (a property name, a description, an enum value) is written into generated code as a string, in place of '${value}' or "${value}", which break on a quote or a line break in the value.

Signature

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

toStringLiteral(value: string): string

Parameters

value, string required
Example
toStringLiteral('a.b')  // "'a.b'"
toStringLiteral('a"b')  // `'a"b'`
toStringLiteral("it's") // "'it\\'s'"
toStringLiteral('a\nb') // "'a\\nb'"

Returns

string