validate-element-name
Advanced tools
Comparing version 0.3.1 to 0.4.0
15
cli.js
@@ -28,7 +28,12 @@ #!/usr/bin/env node | ||
try { | ||
validate(input); | ||
console.log('Valid element name 👍'); | ||
} catch (err) { | ||
console.error(err.message); | ||
var res = validate(input); | ||
if (res.isValid) { | ||
console.log('👍 Valid element name'); | ||
if (res.message) { | ||
console.log('\nWarning:\n' + res.message); | ||
} | ||
} else { | ||
console.error('👎 Invalid element name\n\n' + res.message); | ||
} |
57
index.js
@@ -15,41 +15,66 @@ 'use strict'; | ||
module.exports = function (name) { | ||
function hasError(name) { | ||
if (!name) { | ||
throw new Error('Missing element name'); | ||
return 'Missing element name.'; | ||
} | ||
if (name.indexOf('-') === -1) { | ||
throw new Error('Custom element names must contain a hyphen. Example: unicorn-cake'); | ||
return 'Custom element names must contain a hyphen. Example: unicorn-cake'; | ||
} | ||
if (/^\d/i.test(name)) { | ||
return 'Custom element names must not start with a digit.'; | ||
} | ||
if (/^-/i.test(name)) { | ||
return 'Custom element names must not start with a hyphen.'; | ||
} | ||
// http://www.w3.org/TR/custom-elements/#concepts | ||
if (!ncname.test(name)) { | ||
return 'Invalid element name.'; | ||
} | ||
if (reservedNames.indexOf(name) !== -1) { | ||
return 'The supplied element name is reserved and can\'t be used.\nSee: http://www.w3.org/TR/custom-elements/#concepts'; | ||
} | ||
}; | ||
function hasWarning(name) { | ||
if (/^polymer-/.test(name)) { | ||
throw new Error('Custom element names should not start with `polymer-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element'); | ||
return 'Custom element names should not start with `polymer-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element'; | ||
} | ||
if (/^x-/.test(name)) { | ||
throw new Error('Custom element names should not start with `x-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element/'); | ||
return 'Custom element names should not start with `x-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element/'; | ||
} | ||
if (/^ng-/.test(name)) { | ||
throw new Error('Custom element names should not start with `ng-`.\nSee: http://docs.angularjs.org/guide/directive#creating-directives'); | ||
return 'Custom element names should not start with `ng-`.\nSee: http://docs.angularjs.org/guide/directive#creating-directives'; | ||
} | ||
if (/^\d/i.test(name)) { | ||
throw new Error('Custom element names must not start with a digit'); | ||
if (/-$/.test(name)) { | ||
return 'Custom element names should not end with an hyphen.'; | ||
} | ||
if (/^-/i.test(name)) { | ||
throw new Error('Custom element names must not start with a hyphen'); | ||
if (/[^\x20-\x7E]+/.test(name)) { | ||
return 'Custom element names should not contain non-ASCII characters.'; | ||
} | ||
// http://www.w3.org/TR/custom-elements/#concepts | ||
if (!ncname.test(name)) { | ||
throw new Error('Invalid element name.'); | ||
if (/--/.test(name)) { | ||
return 'Custom element names should not contain consecutive hyphens.'; | ||
} | ||
if (reservedNames.indexOf(name) !== -1) { | ||
throw new Error('The supplied element name is reserved and can\'t be used.\nSee: http://www.w3.org/TR/custom-elements/#concepts'); | ||
if (/[^a-z0-9]{2}/.test(name)) { | ||
return 'Custom element names should not contain consecutive non-alpha characters.'; | ||
} | ||
} | ||
return true; | ||
module.exports = function (name) { | ||
var errMsg = hasError(name); | ||
return { | ||
isValid: !errMsg, | ||
message: errMsg || hasWarning(name) | ||
}; | ||
}; |
{ | ||
"name": "validate-element-name", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Validate the name of a custom element", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -21,11 +21,7 @@ # validate-element-name [![Build Status](https://travis-ci.org/sindresorhus/validate-element-name.svg?branch=master)](https://travis-ci.org/sindresorhus/validate-element-name) | ||
try { | ||
validate('unicorn'); | ||
} catch (err) { | ||
console.log(err.message); | ||
//=> 'Custom element names must contain a hyphen. Example: unicorn-cake' | ||
} | ||
validate('unicorn'); | ||
//=> {isValid: false, message: 'Custom element names must contain a hyphen. Example: unicorn-cake'} | ||
``` | ||
Throws an error if the custom element name is invalid. | ||
See [cli.js](cli.js) for real-world usage. | ||
@@ -51,3 +47,3 @@ | ||
$ validate-element-name s-slider | ||
Valid element name 👍 | ||
👍 Valid element name | ||
``` | ||
@@ -54,0 +50,0 @@ |
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
5018
95
53