Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add function to validate an identifier #351

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const semverCompareLoose = require('semver/functions/compare-loose')
const semverCompareBuild = require('semver/functions/compare-build')
const semverSort = require('semver/functions/sort')
const semverRsort = require('semver/functions/rsort')
const semverIdentifier = require('semver/functions/identifier')

// low-level comparators between versions
const semverGt = require('semver/functions/gt')
Expand Down Expand Up @@ -400,6 +401,7 @@ strings that they parse.
or comparators intersect.
* `parse(v)`: Attempt to parse a string as a semantic version, returning either
a `SemVer` object or `null`.
* `identifier(v)`: Return a valid prerelease identifier, or null if it's not valid.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `identifier(v)`: Return a valid prerelease identifier, or null if it's not valid.
* `preIdentifier(v)`: Return a valid prerelease identifier, or null if it's not valid.

let's make sure this is clearly a prerelease identifier


### Comparison

Expand Down
10 changes: 10 additions & 0 deletions functions/identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {t, src} = require('../internal/re')
const identifier = (version) => {
const split = version.split('\.');
const valid = split.every(part => new RegExp(`^${src[t.PRERELEASEIDENTIFIER]}$`).test(part));
if (!valid) {
return null;
}
return version;
}
module.exports = identifier
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const gte = require('./functions/gte')
const lte = require('./functions/lte')
const cmp = require('./functions/cmp')
const coerce = require('./functions/coerce')
const identifier = require('./functions/identifier')
const Comparator = require('./classes/comparator')
const Range = require('./classes/range')
const satisfies = require('./functions/satisfies')
Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = {
lte,
cmp,
coerce,
identifier,
Comparator,
Range,
satisfies,
Expand All @@ -85,4 +87,4 @@ module.exports = {
SEMVER_SPEC_VERSION: constants.SEMVER_SPEC_VERSION,
compareIdentifiers: identifiers.compareIdentifiers,
rcompareIdentifiers: identifiers.rcompareIdentifiers,
}
}
10 changes: 10 additions & 0 deletions test/functions/identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {test} = require('tap')
const identifier = require('../../functions/identifier.js')

test('identifier tests', t => {
t.equal(identifier('hotfix-410'), 'hotfix-410')
t.equal(identifier('hotfix/410'), null)
t.equal(identifier('123.123.123'), '123.123.123')
t.equal(identifier('007.james.bond'), null)
t.end()
})