{"id":12153,"library":"toidentifier","title":"String to JavaScript Identifier Converter","description":"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.","status":"maintenance","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/component/toidentifier","tags":["javascript"],"install":[{"cmd":"npm install toidentifier","lang":"bash","label":"npm"},{"cmd":"yarn add toidentifier","lang":"bash","label":"yarn"},{"cmd":"pnpm add toidentifier","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module and exports a single function. Direct ES module import syntax (`import`) will not work without a bundler or Node.js's CJS interoperability settings (e.g., `\"type\": \"module\"` in package.json with `import toIdentifier from 'toidentifier';`).","wrong":"import toIdentifier from 'toidentifier'","symbol":"toIdentifier","correct":"const toIdentifier = require('toidentifier')"},{"note":"Even though it's a CommonJS module, TypeScript's default import interop (enabled with `\"esModuleInterop\": true` in `tsconfig.json`) allows this syntax for default CJS exports. Attempting a named import (`{ toIdentifier }`) will result in an error.","wrong":"import { toIdentifier } from 'toidentifier'","symbol":"toIdentifier (TypeScript)","correct":"import toIdentifier from 'toidentifier'"}],"quickstart":{"code":"const toIdentifier = require('toidentifier');\n\nconsole.log(toIdentifier('Bad Request'));\n// Expected output: \"BadRequest\"\n\nconsole.log(toIdentifier('user-name-id'));\n// Expected output: \"UserNameId\"\n\nconsole.log(toIdentifier('item name #1!'));\n// Expected output: \"ItemName1\"\n\nconsole.log(toIdentifier('another string with spaces and symbols @#$%^&*()'));\n// Expected output: \"AnotherStringWithSpacesAndSymbols\"","lang":"javascript","description":"Demonstrates how to import and use the `toIdentifier` function to convert various strings into valid JavaScript identifiers, highlighting its sanitization behavior."},"warnings":[{"fix":"For CommonJS environments, use `const Name = require('pkg')`. For ES module environments, either configure your build system to handle CommonJS imports or ensure `\"esModuleInterop\": true` is set in your `tsconfig.json` for TypeScript projects. If using Node.js with `\"type\": \"module\"`, `import toIdentifier from 'toidentifier'` will work due to Node's CJS interoperability.","message":"The package is a pure CommonJS module. Direct ES Module imports (`import ... from 'pkg'`) will not function in environments expecting native ESM without specific Node.js configuration (like 'type: module' in package.json) or a bundler to handle CJS-to-ESM transpilation.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review the transformation rules to ensure they align with your requirements. If not, consider a different utility or implement custom string sanitization logic.","message":"The `toIdentifier` function strictly removes all non-word characters (anything not `[0-9a-z_]`) after capitalization and joining. If you require a more permissive identifier or need to preserve certain symbols, this utility will not be suitable.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate if the package's existing functionality meets your long-term needs. For critical applications, consider vendoring the code or forking the repository if maintenance becomes a concern.","message":"This package has not been updated in several years (last publish over 4 years ago). While stable due to its simple functionality, it may not receive updates for newer JavaScript features, bug fixes, or security patches, though its minimal scope makes major vulnerabilities unlikely.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"When using `require`, the function is the direct export, so use `const toIdentifier = require('toidentifier')`. For TypeScript or ES modules with interop, use `import toIdentifier from 'toidentifier'`.","cause":"Attempting to access the function as a property of the imported module object (e.g., `require('toidentifier').toIdentifier`) instead of as the default export.","error":"TypeError: toIdentifier is not a function"},{"fix":"For native ES module environments, use `import toIdentifier from 'toidentifier'`. If `\"esModuleInterop\": true` is in your `tsconfig.json` for TypeScript, this will work. Otherwise, ensure your build process correctly transpiles CommonJS modules for ESM consumption.","cause":"Using `require()` syntax directly in a native ES module environment (e.g., a `.mjs` file or a project with `\"type\": \"module\"` in `package.json`) without a transpilation step.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}