identifierfy
Advanced tools
Comparing version 1.1.1 to 2.0.0
102
index.js
@@ -1,31 +0,12 @@ | ||
'use strict'; | ||
'use strict' | ||
const {keyword} = require('esutils') | ||
var _getIterator2 = require('babel-runtime/core-js/get-iterator'); | ||
var _getIterator3 = _interopRequireDefault(_getIterator2); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = identifierfy; | ||
var _esutils = require('esutils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Follow Babel's implementation: | ||
// <https://github.com/babel/babel/blob/add96d626d98133e26f62ec4c2aeee655bed069a/packages/babel-types/src/validators.js#L153:L164> | ||
function isValidIdentifier(name) { | ||
return !_esutils.keyword.isReservedWordES6(name, true) && _esutils.keyword.isIdentifierNameES6(name); | ||
function isValidIdentifier (name) { | ||
return !keyword.isReservedWordES6(name, true) && keyword.isIdentifierNameES6(name) | ||
} | ||
// Rewrite the name until it forms a valid identifier. | ||
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; | ||
module.exports = function identifierfy (name, {prefixInvalidIdentifiers = true, prefixReservedWords = true} = {}) { | ||
// Start with a valid character. This way if the first character in the name | ||
@@ -35,69 +16,46 @@ // is not allowed to be used as the first character it can be prefixed with | ||
// is a reserved word. | ||
var intermediate = '_'; | ||
let intermediate = '_' | ||
// Flag whether the previous character was invalid (and thus dropped). | ||
var prevWasInvalid = false; | ||
let prevWasInvalid = false | ||
// Use for/of to iterate over the code points. This way surrogate pairs can | ||
// be avoided. | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = (0, _getIterator3.default)(name), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var char = _step.value; | ||
// Try to uppercase the immediately following (not all characters have an | ||
// case equivalent though). Ignore if the dropped character was at the front | ||
// of the name. | ||
if (prevWasInvalid && intermediate !== '_') { | ||
char = char.toUpperCase(); | ||
} | ||
// Only include characters if the name remains valid. | ||
if (isValidIdentifier(intermediate + char)) { | ||
intermediate += char; | ||
prevWasInvalid = false; | ||
} else { | ||
prevWasInvalid = true; | ||
} | ||
for (let char of name) { | ||
// Try to uppercase the immediately following (not all characters have an | ||
// case equivalent though). Ignore if the dropped character was at the front | ||
// of the name. | ||
if (prevWasInvalid && intermediate !== '_') { | ||
char = char.toUpperCase() | ||
} | ||
// Return `null` if no characters from the original name survive the process. | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
// Only include characters if the name remains valid. | ||
if (isValidIdentifier(intermediate + char)) { | ||
intermediate += char | ||
prevWasInvalid = false | ||
} else { | ||
prevWasInvalid = true | ||
} | ||
} | ||
if (intermediate === '_') return null; | ||
// Return `null` if no characters from the original name survive the process. | ||
if (intermediate === '_') return null | ||
// If the name is valid without the underscore prefix return it as such, | ||
// otherwise retain it, unless directed otherwise. | ||
var withoutPrefix = intermediate.slice(1); | ||
const withoutPrefix = intermediate.slice(1) | ||
if (isValidIdentifier(withoutPrefix)) { | ||
return withoutPrefix; | ||
return withoutPrefix | ||
} else if (prefixInvalidIdentifiers && prefixReservedWords) { | ||
return intermediate; | ||
return intermediate | ||
} else { | ||
var isIdentifierName = _esutils.keyword.isIdentifierNameES6(withoutPrefix); | ||
var isReservedWord = _esutils.keyword.isReservedWordES6(withoutPrefix, true); | ||
if (!isIdentifierName && !prefixInvalidIdentifiers || isReservedWord && !prefixReservedWords) { | ||
return withoutPrefix; | ||
const isIdentifierName = keyword.isIdentifierNameES6(withoutPrefix) | ||
const isReservedWord = keyword.isReservedWordES6(withoutPrefix, true) | ||
if ((!isIdentifierName && !prefixInvalidIdentifiers) || | ||
(isReservedWord && !prefixReservedWords)) { | ||
return withoutPrefix | ||
} else { | ||
return intermediate; | ||
return intermediate | ||
} | ||
} | ||
} | ||
module.exports = exports['default']; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "identifierfy", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Rewrites an identifier string so its valid according to ES2015", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">= 6" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.js.map" | ||
"index.js" | ||
], | ||
"scripts": { | ||
"clean": "rimraf index.js index.js.map", | ||
"prebuild": "npm run clean", | ||
"build": "babel src --out-dir ./ --source-maps", | ||
"prepublish": "npm run build", | ||
"coverage": "nyc npm test", | ||
"test": "ava", | ||
"posttest": "standard" | ||
"lint": "as-i-preach", | ||
"test": "as-i-preach && nyc ava" | ||
}, | ||
@@ -35,15 +32,16 @@ "repository": { | ||
"devDependencies": { | ||
"ava": "^0.11.0", | ||
"babel-cli": "^6.4.0", | ||
"babel-plugin-add-module-exports": "^0.1.2", | ||
"babel-plugin-transform-runtime": "^6.4.0", | ||
"babel-preset-es2015": "^6.3.13", | ||
"nyc": "^5.3.0", | ||
"rimraf": "^2.5.0", | ||
"standard": "^5.4.1" | ||
"@novemberborn/as-i-preach": "^11.0.0", | ||
"ava": "^1.0.0-beta.8", | ||
"nyc": "^13.0.1" | ||
}, | ||
"dependencies": { | ||
"babel-runtime": "^6.3.19", | ||
"esutils": "^2.0.2" | ||
}, | ||
"nyc": { | ||
"reporter": [ | ||
"lcov", | ||
"html", | ||
"text" | ||
] | ||
} | ||
} |
# identifierfy | ||
Rewrites an identifier string so its valid according to ES2015. Tested with | ||
Node.js 0.10 and above. | ||
Rewrites an identifier string so it's valid according to ES2015. Works in | ||
Node.js 6 and above. | ||
@@ -13,3 +13,3 @@ Please see [this awesome article by Mathias | ||
``` | ||
npm install --save identifierfy | ||
npm install identifierfy | ||
``` | ||
@@ -22,3 +22,3 @@ | ||
```js | ||
var identifierfy = require('identifierfy') | ||
const identifierfy = require('identifierfy') | ||
``` | ||
@@ -64,3 +64,3 @@ | ||
```js | ||
identifierfy(input, { prefixReservedWords: false }) | ||
identifierfy(input, {prefixReservedWords: false}) | ||
``` | ||
@@ -73,3 +73,3 @@ Input|Resulting identifier|Reason | ||
```js | ||
identifierfy(input, { prefixInvalidIdentifiers: false }) | ||
identifierfy(input, {prefixInvalidIdentifiers: false}) | ||
``` | ||
@@ -82,3 +82,3 @@ Input|Resulting identifier|Reason | ||
```js | ||
identifierfy(input, { prefixInvalidIdentifiers: false, prefixReservedWords: false }) | ||
identifierfy(input, {prefixInvalidIdentifiers: false, prefixReservedWords: false}) | ||
``` | ||
@@ -85,0 +85,0 @@ Input|Resulting identifier|Reason |
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
1
3
6815
4
53
- Removedbabel-runtime@^6.3.19
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedregenerator-runtime@0.11.1(transitive)