mirror of
https://github.com/TangentFoxy/semver.lua.git
synced 2025-07-28 02:52:21 +00:00
started working on the prerelease
This commit is contained in:
29
semver.lua
29
semver.lua
@@ -12,6 +12,16 @@ local function checkPositiveInteger(number, name)
|
|||||||
assert(math.floor(number) == number, name .. ' must be an integer')
|
assert(math.floor(number) == number, name .. ' must be an integer')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function present(value)
|
||||||
|
return value and value ~= ''
|
||||||
|
end
|
||||||
|
|
||||||
|
local function filterPrerelease(prerelease)
|
||||||
|
local identifiers = prerelease:match("-([%w-][%.%w-]+)")
|
||||||
|
assert(identifiers, ("The prerelease %q must start with a dash and be followed by dashes, alphanumerics or dots."):format(prerelease))
|
||||||
|
return identifiers
|
||||||
|
end
|
||||||
|
|
||||||
local methods = {}
|
local methods = {}
|
||||||
|
|
||||||
function methods:nextMajor()
|
function methods:nextMajor()
|
||||||
@@ -28,7 +38,8 @@ local mt = { __index = methods }
|
|||||||
function mt:__eq(other)
|
function mt:__eq(other)
|
||||||
return self.major == other.major and
|
return self.major == other.major and
|
||||||
self.minor == other.minor and
|
self.minor == other.minor and
|
||||||
self.patch == other.patch
|
self.patch == other.patch and
|
||||||
|
self.prerelease == other.prerelease
|
||||||
end
|
end
|
||||||
function mt:__lt(other)
|
function mt:__lt(other)
|
||||||
return self.major < other.major or
|
return self.major < other.major or
|
||||||
@@ -40,18 +51,23 @@ function mt:__pow(other)
|
|||||||
self.minor <= other.minor
|
self.minor <= other.minor
|
||||||
end
|
end
|
||||||
function mt:__tostring()
|
function mt:__tostring()
|
||||||
return ("%d.%d.%d"):format(self.major, self.minor, self.patch)
|
local buffer = { ("%d.%d.%d"):format(self.major, self.minor, self.patch) }
|
||||||
|
if self.prerelease then table.insert(buffer, "-" .. self.prerelease) end
|
||||||
|
return table.concat(buffer)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- defined as local at the begining of the file
|
-- defined as local at the begining of the file
|
||||||
version = function(major, minor, patch)
|
version = function(major, minor, patch, prerelease)
|
||||||
assert(major, "At least one parameter is needed")
|
assert(major, "At least one parameter is needed")
|
||||||
|
|
||||||
if type(major) == 'string' then
|
if type(major) == 'string' then
|
||||||
local sMajor, sMinor, sPatch = major:match("^(%d+)%.?(%d*)%.?(%d*)$")
|
local sMajor, sMinor, sPatch, sPrerelease = major:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
|
||||||
assert(type(sMajor) == 'string', ("Could not version number(s) from %q"):format(major))
|
assert(type(sMajor) == 'string', ("Could not extract version number(s) from %q"):format(major))
|
||||||
major, minor, patch = tonumber(sMajor), tonumber(sMinor), tonumber(sPatch)
|
major, minor, patch = tonumber(sMajor), tonumber(sMinor), tonumber(sPatch)
|
||||||
|
if present(sPrerelease) then
|
||||||
|
prerelease = filterPrerelease(sPrerelease)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
patch = patch or 0
|
patch = patch or 0
|
||||||
@@ -61,7 +77,8 @@ version = function(major, minor, patch)
|
|||||||
checkPositiveInteger(minor, "minor")
|
checkPositiveInteger(minor, "minor")
|
||||||
checkPositiveInteger(patch, "patch")
|
checkPositiveInteger(patch, "patch")
|
||||||
|
|
||||||
return setmetatable({major=major, minor=minor, patch=patch}, mt)
|
local result = {major=major, minor=minor, patch=patch, prerelease=prerelease}
|
||||||
|
return setmetatable(result, mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
return version
|
return version
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
local v = require 'semver'
|
local v = require 'semver'
|
||||||
|
|
||||||
local function checkVersion(ver, major, minor, patch)
|
local function checkVersion(ver, major, minor, patch, prerelease)
|
||||||
assert_equal(major, ver.major)
|
assert_equal(major, ver.major)
|
||||||
assert_equal(minor, ver.minor)
|
assert_equal(minor, ver.minor)
|
||||||
assert_equal(patch, ver.patch)
|
assert_equal(patch, ver.patch)
|
||||||
|
assert_equal(prerelease, ver.prerelease)
|
||||||
end
|
end
|
||||||
|
|
||||||
context('semver', function()
|
context('semver', function()
|
||||||
@@ -22,6 +23,10 @@ context('semver', function()
|
|||||||
it('parses 1 number correctly', function()
|
it('parses 1 number correctly', function()
|
||||||
checkVersion(v(1), 1,0,0)
|
checkVersion(v(1), 1,0,0)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('parses prereleases, if they exist', function()
|
||||||
|
checkVersion(v(1,2,3,"alpha"), 1,2,3,"alpha")
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("from strings", function()
|
describe("from strings", function()
|
||||||
@@ -37,6 +42,9 @@ context('semver', function()
|
|||||||
test("5", function()
|
test("5", function()
|
||||||
checkVersion( v'5', 5,0,0)
|
checkVersion( v'5', 5,0,0)
|
||||||
end)
|
end)
|
||||||
|
test("1.2.3-alpha", function()
|
||||||
|
checkVersion( v'1.2.3-alpha', 1,2,3,'alpha' )
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('errors', function()
|
describe('errors', function()
|
||||||
@@ -72,6 +80,10 @@ context('semver', function()
|
|||||||
it("works with major, minor and patch", function()
|
it("works with major, minor and patch", function()
|
||||||
assert_equal("1.2.3", tostring(v(1,2,3)))
|
assert_equal("1.2.3", tostring(v(1,2,3)))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("works with a prerelease", function()
|
||||||
|
assert_equal("1.2.3-beta", tostring(v(1,2,3,'beta')))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("==", function()
|
describe("==", function()
|
||||||
@@ -81,6 +93,9 @@ context('semver', function()
|
|||||||
it("is false when major, minor and patch are not the same", function()
|
it("is false when major, minor and patch are not the same", function()
|
||||||
assert_not_equal(v(1,2,3), v(4,5,6))
|
assert_not_equal(v(1,2,3), v(4,5,6))
|
||||||
end)
|
end)
|
||||||
|
it("false if all is the same except the prerelease", function()
|
||||||
|
assert_not_equal(v(1,2,3), v'1.2.3-alpha')
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("<", function()
|
describe("<", function()
|
||||||
|
Reference in New Issue
Block a user