identifierfy
Advanced tools
Comparing version 1.0.1 to 1.1.0
23
index.js
@@ -24,2 +24,9 @@ 'use strict'; | ||
function identifierfy(name) { | ||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var _ref$prefixInvalidIde = _ref.prefixInvalidIdentifiers; | ||
var prefixInvalidIdentifiers = _ref$prefixInvalidIde === undefined ? true : _ref$prefixInvalidIde; | ||
var _ref$prefixReservedWo = _ref.prefixReservedWords; | ||
var prefixReservedWords = _ref$prefixReservedWo === undefined ? true : _ref$prefixReservedWo; | ||
// Start with a valid character. This way if the first character in the name | ||
@@ -79,6 +86,18 @@ // is not allowed to be used as the first character it can be prefixed with | ||
// If the name is valid without the underscore prefix return it as such, | ||
// otherwise retain it. | ||
// otherwise retain it, unless directed otherwise. | ||
var withoutPrefix = intermediate.slice(1); | ||
return isValidIdentifier(withoutPrefix) ? withoutPrefix : intermediate; | ||
if (isValidIdentifier(withoutPrefix)) { | ||
return withoutPrefix; | ||
} else if (prefixInvalidIdentifiers && prefixReservedWords) { | ||
return intermediate; | ||
} else { | ||
var isIdentifierName = _esutils.keyword.isIdentifierNameES6(withoutPrefix); | ||
var isReservedWord = _esutils.keyword.isReservedWordES6(withoutPrefix, true); | ||
if (!isIdentifierName && !prefixInvalidIdentifiers || isReservedWord && !prefixReservedWords) { | ||
return withoutPrefix; | ||
} else { | ||
return intermediate; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "identifierfy", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Rewrites an identifier string so its valid according to ES2015", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -36,2 +36,9 @@ # identifierfy | ||
This behavior can be overridden by passing a second `options` argument. This | ||
should be an object. Set `prefixReservedWords` to `false` to avoid reserved | ||
words being prefixed. Set `prefixInvalidIdentifiers` to `false` to avoid | ||
prefixing identifiers where the first character cannot be used. These options | ||
are useful if the resulting value is concatenated with other characters, making | ||
it valid after all. | ||
Note that `null` is returned if all characters from the original string are | ||
@@ -42,2 +49,6 @@ dropped. | ||
```js | ||
identifierfy(input) | ||
``` | ||
Input|Resulting identifier|Reason | ||
@@ -51,1 +62,25 @@ :---|:---|:--- | ||
`'💩'`|`null`|Sadly emojis cannot be used as identifiers 😢 | ||
```js | ||
identifierfy(input, { prefixReservedWords: false }) | ||
``` | ||
Input|Resulting identifier|Reason | ||
:---|:---|:--- | ||
`'class`|`'class'`|Reserved word, but prefixing is disabled | ||
`'42'`|`'_42'`|Identifiers cannot start with a number | ||
```js | ||
identifierfy(input, { prefixInvalidIdentifiers: false }) | ||
``` | ||
Input|Resulting identifier|Reason | ||
:---|:---|:--- | ||
`'class`|`'_class'`|Reserved word | ||
`'42'`|`'42'`|Identifiers cannot start with a number, however prefixing is disabled so it's OK | ||
```js | ||
identifierfy(input, { prefixInvalidIdentifiers: false, prefixReservedWords: false }) | ||
``` | ||
Input|Resulting identifier|Reason | ||
:---|:---|:--- | ||
`'class`|`'class'`|Reserved word, but prefixing is disabled | ||
`'42'`|`'42'`|Identifiers cannot start with a number, however prefixing is disabled so it's OK |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12010
83
84