-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isSemVer): add semver validator (#1246)
* feat: add isSemVer validator to check for Semantic Versioning * style: modify function docs * push compiled modules after function docs change * moe multilineRegexp function to util for re-usability * add more invalid/bad testcases
- Loading branch information
1 parent
c770678
commit a87165a
Showing
10 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = isSemVer; | ||
|
||
var _assertString = _interopRequireDefault(require("./util/assertString")); | ||
|
||
var _multilineRegex = _interopRequireDefault(require("./util/multilineRegex")); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* Regular Expression to match | ||
* semantic versioning (SemVer) | ||
* built from multi-line, multi-parts regexp | ||
* Reference: https://semver.org/ | ||
*/ | ||
var semanticVersioningRegex = (0, _multilineRegex.default)(['^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)', '(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))', '?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$']); | ||
|
||
function isSemVer(str) { | ||
(0, _assertString.default)(str); | ||
return semanticVersioningRegex.test(str); | ||
} | ||
|
||
module.exports = exports.default; | ||
module.exports.default = exports.default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = multilineRegexp; | ||
|
||
/** | ||
* Build RegExp object from an array | ||
* of multiple/multi-line regexp parts | ||
* | ||
* @param {string[]} parts | ||
* @param {string} flags | ||
* @return {object} - RegExp object | ||
*/ | ||
function multilineRegexp(parts) { | ||
var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var regexpAsStringLiteral = parts.join(''); | ||
return new RegExp(regexpAsStringLiteral, flags); | ||
} | ||
|
||
module.exports = exports.default; | ||
module.exports.default = exports.default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import assertString from './util/assertString'; | ||
import multilineRegexp from './util/multilineRegex'; | ||
|
||
/** | ||
* Regular Expression to match | ||
* semantic versioning (SemVer) | ||
* built from multi-line, multi-parts regexp | ||
* Reference: https://semver.org/ | ||
*/ | ||
const semanticVersioningRegex = multilineRegexp([ | ||
'^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)', | ||
'(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))', | ||
'?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$', | ||
]); | ||
|
||
export default function isSemVer(str) { | ||
assertString(str); | ||
|
||
return semanticVersioningRegex.test(str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Build RegExp object from an array | ||
* of multiple/multi-line regexp parts | ||
* | ||
* @param {string[]} parts | ||
* @param {string} flags | ||
* @return {object} - RegExp object | ||
*/ | ||
export default function multilineRegexp(parts, flags = '') { | ||
const regexpAsStringLiteral = parts.join(''); | ||
|
||
return new RegExp(regexpAsStringLiteral, flags); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.