1. safe-identifier
Sanitize strings for use as JavaScript identifiers & property names
safe-identifier
Package: safe-identifier
Created by: eemeli
Last modified: Mon, 16 May 2022 10:12:57 GMT
Version: 0.4.2
License: ISC
Downloads: 4,896,502
Repository: https://github.com/eemeli/safe-identifier

Install

npm install safe-identifier
yarn add safe-identifier

safe-identifier

Sanitize strings for use as JavaScript identifiers & property names.

npm install --save safe-identifier
 import { identifier, property } from 'safe-identifier'

identifier('Foo') === 'Foo'
identifier('enum') === '_enum'
identifier('my var', true) === 'my_var_hk17pp'
identifier(' my \0var ', true) === 'my_var_1d8fi3'

property('Foo', 'bar') === 'Foo.bar'
property('Foo', 'bar\nbar') === 'Foo["bar\\nbar"]'
property(null, 'foo') === 'foo'
property(null, 'void') === '"void"'

identifier(key: string, unique: boolean): string

Sanitize a string for use as an identifier name

Replaces invalid character sequences with _ and may add a _ prefix if the
resulting name would conflict with a JavaScript reserved name, covering all
standards from ES3 up to ES2018, along with current
active proposals.

If unique is true, a 32-bit hash of the key is appended to the result in
order to help ensure that different inputs produce different outputs.

property(obj: string?, key: string): string

Sanitize a string for use as a property name

By default uses obj.key notation, falling back to obj["key"] if the key
contains invalid characters or is an ECMAScript 3rd Edition reserved word
(required for IE8 compatibility). If obj is empty, returns only the possibly
quoted property key. The correctness of obj is not checked.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code