Skip to content

Instantly share code, notes, and snippets.

@aizigao
Created January 9, 2018 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aizigao/e487ef82ff07196eefe8cf36c556646c to your computer and use it in GitHub Desktop.
Save aizigao/e487ef82ff07196eefe8cf36c556646c to your computer and use it in GitHub Desktop.
js 对比版本号
const isNewVersion = (oldStr, newStr, contrastIdx = 0) => {
const oldArr = [0, 0, 0]
const newArr = [0, 0, 0]
oldArr.splice(0, oldStr.split('.').length, ...oldStr.split('.'))
newArr.splice(0, newStr.split('.').length, ...newStr.split('.'))
const oldTag = parseInt(newArr[contrastIdx], 10)
const newTag = parseInt(oldArr[contrastIdx], 10)
if (oldTag === newTag) {
if (contrastIdx === 2) {
return false
}
return isNewVersion(oldStr, newStr, contrastIdx + 1)
} else if (oldTag < newTag) {
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment