{"library":"safe-identifier","title":"Safe Identifier String Sanitization","description":"safe-identifier is a focused utility library designed to sanitize arbitrary strings for safe use as JavaScript identifiers and object property names. It currently stands at version 0.4.2 and has shown an active release cadence, with several minor versions released recently. The library's core functionality includes replacing invalid characters with underscores, pre-pending an underscore if the resulting identifier conflicts with a JavaScript reserved word (covering ES3 to ES2018 and active proposals), and optionally appending a unique hash to generated identifiers. For property names, it intelligently uses dot notation (`obj.key`) or bracket notation (`obj[\"key\"]`) based on key validity and ECMAScript 3rd Edition reserved words, ensuring compatibility down to IE8. Its primary differentiator is its comprehensive standard coverage and explicit handling for browser compatibility and uniqueness.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install safe-identifier"],"cli":null},"imports":["import { identifier } from 'safe-identifier'","import { property } from 'safe-identifier'","import * as safeIdentifier from 'safe-identifier'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { identifier, property } from 'safe-identifier';\n\n// Basic identifier sanitization\nconsole.log(`'Foo' -> ${identifier('Foo')}`); // 'Foo'\nconsole.log(`'enum' (reserved word) -> ${identifier('enum')}`); // '_enum'\n\n// Identifier with uniqueness hash\nconst keyWithSpaces = 'my var';\nconst uniqueId1 = identifier(keyWithSpaces, true);\nconst uniqueId2 = identifier(' another\\tkey ', true);\nconsole.log(`'${keyWithSpaces}' (unique) -> ${uniqueId1}`); // Example: 'my_var_hk17pp'\nconsole.log(`' another\\tkey ' (unique) -> ${uniqueId2}`); // Example: 'another_key_1d8fi3'\n\n// Property name sanitization\nconsole.log(`'Foo', 'bar' -> ${property('Foo', 'bar')}`); // 'Foo.bar'\nconsole.log(`'Foo', 'bar\\nbar' -> ${property('Foo', 'bar\\nbar')}`); // 'Foo[\"bar\\nbar\"]'\nconsole.log(`null, 'foo' -> ${property(null, 'foo')}`); // 'foo'\nconsole.log(`null, 'void' (ES3 reserved) -> ${property(null, 'void')}`); // '\"void\"' (quoted for ES3 compatibility)","lang":"typescript","description":"Demonstrates basic usage of `identifier` for sanitizing string keys to be valid JavaScript identifiers, including handling reserved words and generating unique hashes, and `property` for generating safe object property accessors with ES3 compatibility.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}