JavaScript.coerceVarname()

Given a string, return a similar version of the string, but which is camel-cased and otherwise stripped of special characters, whitespace, etc. so that it is usable as an ECMAScript variable.

Usage

Hover over inputs/exits for more info.

var JavaScript = require('machinepack-javascript');


// Given a string, return a similar version of the string, but which is camel-cased and otherwise stripped of special characters, whitespace, etc. so that it is usable as an ECMAScript variable.

JavaScript.coerceVarname({

string: 'foo-bar-baz',

force: false,

}).execSync();

Inputs

string required

The string to convert.

'foo-bar-baz'
force

Return a best guess varname even if the conversion fails?

false

Exits

error

An unexpected error occurred.

collidesWithReservedWord

The specified string cannot be coerced into a valid ECMAScript 5.1-compatible variable name because it would collide with a JavaScript reserved word, or is otherwise invalid.

success

OK.

'fooBarBaz'