# safe-identifier

> Sanitize strings for use as JavaScript identifiers & property names

Latest version **0.4.2** (published 2020-08-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install safe-identifier
pnpm add safe-identifier
yarn add safe-identifier
bun add safe-identifier
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.4.2 |
| Published | 2020-08-09 |
| First published | 2019-04-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Eemeli Aro |
| Maintainers | eemeli |
| Keywords | es3, es5, esnext, reserved, words, code, generation, identifier, property |

## Links

- npm: https://www.npmjs.com/package/safe-identifier
- Repository: https://github.com/eemeli/safe-identifier
- Homepage: https://github.com/eemeli/safe-identifier#readme
- Issues: https://github.com/eemeli/safe-identifier/issues
- npm.io page: https://npm.io/package/safe-identifier

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.4.2 (latest) — 2020-08-09
- 0.4.1 — 2020-04-05
- 0.4.0 — 2020-04-05
- 0.3.1 — 2019-12-04
- 0.3.0 — 2019-12-04
- 0.2.0 — 2019-08-04
- 0.1.0 — 2019-04-22

## README

# safe-identifier

Sanitize strings for use as JavaScript identifiers & property names.

```
npm install --save safe-identifier
```

```js
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](https://github.com/tc39/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.

---
_Source: https://npm.io/package/safe-identifier · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
