From 417d8b9478b01174c7fde62940c26a40249c5a24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Schmitz=20von=20H=C3=BClst?= Date: Wed, 11 Nov 2020 11:59:01 +0100 Subject: [PATCH 1/4] add identifier function --- functions/identifier.js | 10 ++++++++++ test/functions/identifier.js | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 functions/identifier.js create mode 100644 test/functions/identifier.js diff --git a/functions/identifier.js b/functions/identifier.js new file mode 100644 index 00000000..db4f62f4 --- /dev/null +++ b/functions/identifier.js @@ -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 \ No newline at end of file diff --git a/test/functions/identifier.js b/test/functions/identifier.js new file mode 100644 index 00000000..0e121f8c --- /dev/null +++ b/test/functions/identifier.js @@ -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() +}) From a8a49b87fe6e08a14fad321ab9b43ad62cfce7b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Schmitz=20von=20H=C3=BClst?= Date: Wed, 11 Nov 2020 12:04:49 +0100 Subject: [PATCH 2/4] add docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9bef045a..b9214f94 100644 --- a/README.md +++ b/README.md @@ -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') @@ -398,6 +399,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. ### Comparison From 911dd490fc43366b4ab9dd7e9e224752d6be0629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Schmitz=20von=20H=C3=BClst?= Date: Wed, 11 Nov 2020 12:06:22 +0100 Subject: [PATCH 3/4] add empty line --- functions/identifier.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/identifier.js b/functions/identifier.js index db4f62f4..ae4b480c 100644 --- a/functions/identifier.js +++ b/functions/identifier.js @@ -7,4 +7,4 @@ const identifier = (version) => { } return version; } -module.exports = identifier \ No newline at end of file +module.exports = identifier From aea03ba1e0bc153a0f9f8eb290f5faecf986210d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Schmitz=20von=20H=C3=BClst?= Date: Wed, 11 Nov 2020 12:13:36 +0100 Subject: [PATCH 4/4] export function --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 57e2ae64..b2a646f7 100644 --- a/index.js +++ b/index.js @@ -45,4 +45,5 @@ module.exports = { intersects: require('./ranges/intersects'), simplifyRange: require('./ranges/simplify'), subset: require('./ranges/subset'), + identifier: require('./functions/identifier'), }