mirror of
https://github.com/TangentFoxy/semver.lua.git
synced 2025-07-28 11:02:21 +00:00
refactor
This commit is contained in:
79
semver.lua
79
semver.lua
@@ -1,4 +1,4 @@
|
|||||||
-- semver.lua - v0.1 (2012-01)
|
-- semver.lua - v1.0 (2012-01)
|
||||||
-- Copyright (c) 2012 Enrique García Cota
|
-- Copyright (c) 2012 Enrique García Cota
|
||||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
@@ -15,37 +15,59 @@ local function present(value)
|
|||||||
return value and value ~= ''
|
return value and value ~= ''
|
||||||
end
|
end
|
||||||
|
|
||||||
local function parseExtra(extra)
|
-- splitByDot("a.bbc.d") == {"a", "bbc", "d"}
|
||||||
if not present(extra) then return end
|
local function splitByDot(str)
|
||||||
|
local t, count = {}, 0
|
||||||
|
str:gsub("([^%.]+)", function(c)
|
||||||
|
count = count + 1
|
||||||
|
t[count] = c
|
||||||
|
end)
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
local prereleaseWithSign, buildWithSign = extra:match("^(-[^+]+)(+.+)$")
|
local function parsePrereleaseAndBuildWithSign(str)
|
||||||
|
local prereleaseWithSign, buildWithSign = str:match("^(-[^+]+)(+.+)$")
|
||||||
if not (prereleaseWithSign and buildWithSign) then
|
if not (prereleaseWithSign and buildWithSign) then
|
||||||
prereleaseWithSign = extra:match("^(-.+)$")
|
prereleaseWithSign = str:match("^(-.+)$")
|
||||||
buildWithSign = extra:match("^(+.+)$")
|
buildWithSign = str:match("^(+.+)$")
|
||||||
end
|
end
|
||||||
assert(prereleaseWithSign or buildWithSign, ("The parameter %q must begin with + or - to denote a prerelease or a buld"):format(extra))
|
assert(prereleaseWithSign or buildWithSign, ("The parameter %q must begin with + or - to denote a prerelease or a build"):format(str))
|
||||||
|
return prereleaseWithSign, buildWithSign
|
||||||
local prerelease, build
|
end
|
||||||
|
|
||||||
|
local function parsePrerelease(prereleaseWithSign)
|
||||||
if prereleaseWithSign then
|
if prereleaseWithSign then
|
||||||
prerelease = prereleaseWithSign:match("^-(%w[%.%w-]*)$")
|
local prerelease = prereleaseWithSign:match("^-(%w[%.%w-]*)$")
|
||||||
assert(prerelease, ("The prerelease %q is not a slash followed by alphanumerics, dots and slashes"):format(prereleaseWithSign))
|
assert(prerelease, ("The prerelease %q is not a slash followed by alphanumerics, dots and slashes"):format(prereleaseWithSign))
|
||||||
|
return prerelease
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function parseBuild(buildWithSign)
|
||||||
if buildWithSign then
|
if buildWithSign then
|
||||||
build = buildWithSign:match("^%+(%w[%.%w-]*)$")
|
build = buildWithSign:match("^%+(%w[%.%w-]*)$")
|
||||||
assert(build, ("The build %q is not a + sign followed by alphanumerics, dots and slashes"):format(buildWithSign))
|
assert(build, ("The build %q is not a + sign followed by alphanumerics, dots and slashes"):format(buildWithSign))
|
||||||
|
return build
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function parsePrereleaseAndBuild(str)
|
||||||
|
if not present(str) then return nil, nil end
|
||||||
|
|
||||||
|
local prereleaseWithSign, buildWithSign = parsePrereleaseAndBuildWithSign(str)
|
||||||
|
|
||||||
|
local prerelease = parsePrerelease(prereleaseWithSign)
|
||||||
|
local build = parseBuild(buildWithSign)
|
||||||
|
|
||||||
return prerelease, build
|
return prerelease, build
|
||||||
end
|
end
|
||||||
|
|
||||||
local function parseString(str)
|
local function parseVersion(str)
|
||||||
local sMajor, sMinor, sPatch, extra = str:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
|
local sMajor, sMinor, sPatch, sPrereleaseAndBuild = str:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
|
||||||
assert(type(sMajor) == 'string', ("Could not extract version number(s) from %q"):format(str))
|
assert(type(sMajor) == 'string', ("Could not extract version number(s) from %q"):format(str))
|
||||||
local major, minor, patch = tonumber(sMajor), tonumber(sMinor), tonumber(sPatch)
|
local major, minor, patch = tonumber(sMajor), tonumber(sMinor), tonumber(sPatch)
|
||||||
local prerelease, build = parseExtra(extra)
|
local prerelease, build = parsePrereleaseAndBuild(sPrereleaseAndBuild)
|
||||||
return major,minor,patch,prerelease,build
|
return major, minor, patch, prerelease, build
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -71,29 +93,20 @@ local function compareIds(selfId, otherId)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function splitByDot(str)
|
local function smallerPrereleaseOrBuild(mine, his)
|
||||||
local t, count = {}, 0
|
if mine == his then return false end
|
||||||
str:gsub("([^%.]+)", function(c)
|
|
||||||
count = count + 1
|
|
||||||
t[count] = c
|
|
||||||
end)
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
|
|
||||||
local function smallerExtra(selfExtra, otherExtra)
|
local myIds, hisIds = splitByDot(mine), splitByDot(his)
|
||||||
if selfExtra == otherExtra then return false end
|
local myLength = #myIds
|
||||||
|
|
||||||
local selfIds, otherIds = splitByDot(selfExtra), splitByDot(otherExtra)
|
|
||||||
local selfLength = #selfIds
|
|
||||||
local comparison
|
local comparison
|
||||||
|
|
||||||
for i = 1, selfLength do
|
for i = 1, myLength do
|
||||||
comparison = compareIds(selfIds[i], otherIds[i])
|
comparison = compareIds(myIds[i], hisIds[i])
|
||||||
if comparison ~= 0 then return comparison == -1 end
|
if comparison ~= 0 then return comparison == -1 end
|
||||||
-- if comparison == 0, continue loop
|
-- if comparison == 0, continue loop
|
||||||
end
|
end
|
||||||
|
|
||||||
return selfLength < #otherIds
|
return myLength < #hisIds
|
||||||
end
|
end
|
||||||
|
|
||||||
local methods = {}
|
local methods = {}
|
||||||
@@ -121,9 +134,9 @@ function mt:__lt(other)
|
|||||||
self.minor < other.minor or
|
self.minor < other.minor or
|
||||||
self.patch < other.patch or
|
self.patch < other.patch or
|
||||||
(self.prerelease and not other.prerelease) or
|
(self.prerelease and not other.prerelease) or
|
||||||
smallerExtra(self.prerelease, other.prerelease) or
|
smallerPrereleaseOrBuild(self.prerelease, other.prerelease) or
|
||||||
(not self.build and other.build) or
|
(not self.build and other.build) or
|
||||||
smallerExtra(self.build, other.build)
|
smallerPrereleaseOrBuild(self.build, other.build)
|
||||||
end
|
end
|
||||||
function mt:__pow(other)
|
function mt:__pow(other)
|
||||||
return self.major == other.major and
|
return self.major == other.major and
|
||||||
@@ -142,7 +155,7 @@ version = function(major, minor, patch, prerelease, build)
|
|||||||
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
|
||||||
major,minor,patch,prerelease,build = parseString(major)
|
major,minor,patch,prerelease,build = parseVersion(major)
|
||||||
end
|
end
|
||||||
patch = patch or 0
|
patch = patch or 0
|
||||||
minor = minor or 0
|
minor = minor or 0
|
||||||
|
Reference in New Issue
Block a user