String to JavaScript Identifier Converter

1.0.1 · maintenance · verified Sun Apr 19

The `toidentifier` package is a minimalist utility designed to convert human-readable strings into valid JavaScript identifiers. It operates by splitting the input string into words, capitalizing the first letter of each word, joining them without separators, and finally removing any non-word characters (anything outside `[0-9a-z_]`). This results in a camelCase-like string suitable for variable names or object properties. The current stable version is 1.0.1, and given its focused and complete functionality, it maintains a very low release cadence, emphasizing stability over frequent updates. Its primary differentiation lies in its simplicity and predictable transformation rules, making it a reliable choice for basic identifier sanitization.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `toIdentifier` function to convert various strings into valid JavaScript identifiers, highlighting its sanitization behavior.

const toIdentifier = require('toidentifier');

console.log(toIdentifier('Bad Request'));
// Expected output: "BadRequest"

console.log(toIdentifier('user-name-id'));
// Expected output: "UserNameId"

console.log(toIdentifier('item name #1!'));
// Expected output: "ItemName1"

console.log(toIdentifier('another string with spaces and symbols @#$%^&*()'));
// Expected output: "AnotherStringWithSpacesAndSymbols"

view raw JSON →