-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(esm): add ES6 module version of script
- Loading branch information
1 parent
2aa74e8
commit 326f88f
Showing
10 changed files
with
370 additions
and
2,395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ root = true | |
[*] | ||
indent_style = spaces | ||
indent_size = 2 | ||
quote_type = single |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
export default function compareVersions(v1, v2) { | ||
// validate input and split into segments | ||
const n1 = validateAndParseVersion(v1); | ||
const n2 = validateAndParseVersion(v2); | ||
|
||
// pop off the patch | ||
const p1 = n1.pop(); | ||
const p2 = n2.pop(); | ||
|
||
// validate numbers | ||
const r = compareSegments(n1, n2); | ||
if (r !== 0) return r; | ||
|
||
// validate pre-release | ||
if (p1 && p2) { | ||
return compareSegments(p1.split('.'), p2.split('.')); | ||
} else if (p1 || p2) { | ||
return p1 ? -1 : 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
export const validate = (version) => | ||
typeof version === 'string' && semver.test(version); | ||
|
||
export const compare = (v1, v2, operator) => { | ||
// validate input operator | ||
assertValidOperator(operator); | ||
|
||
// since result of compareVersions can only be -1 or 0 or 1 | ||
// a simple map can be used to replace switch | ||
const res = compareVersions(v1, v2); | ||
|
||
return operatorResMap[operator].includes(res); | ||
}; | ||
|
||
const semver = | ||
/^v?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i; | ||
|
||
const validateAndParseVersion = (v) => { | ||
if (typeof v !== 'string') { | ||
throw new TypeError('Invalid argument expected string'); | ||
} | ||
const match = v.match(semver); | ||
if (!match) { | ||
throw new Error(`Invalid argument not valid semver ('${v}' received)`); | ||
} | ||
match.shift(); | ||
return match; | ||
}; | ||
|
||
const tryParse = (v) => { | ||
const n = parseInt(v, 10); | ||
return isNaN(n) ? v : n; | ||
}; | ||
|
||
const forceType = (a, b) => | ||
typeof a !== typeof b ? [String(a), String(b)] : [a, b]; | ||
|
||
const compareStrings = (a, b) => { | ||
const [ap, bp] = forceType(tryParse(a), tryParse(b)); | ||
if (ap > bp) return 1; | ||
if (ap < bp) return -1; | ||
return 0; | ||
}; | ||
|
||
const compareSegments = (a, b) => { | ||
for (let i = 0; i < Math.max(a.length, b.length); i++) { | ||
const r = compareStrings(a[i] || 0, b[i] || 0); | ||
if (r !== 0) return r; | ||
} | ||
return 0; | ||
}; | ||
|
||
const operatorResMap = { | ||
'>': [1], | ||
'>=': [0, 1], | ||
'=': [0], | ||
'<=': [-1, 0], | ||
'<': [-1], | ||
}; | ||
|
||
const allowedOperators = Object.keys(operatorResMap); | ||
|
||
const assertValidOperator = (op) => { | ||
if (typeof op !== 'string') { | ||
throw new TypeError( | ||
`Invalid operator type, expected string but got ${typeof op}` | ||
); | ||
} | ||
if (allowedOperators.indexOf(op) === -1) { | ||
throw new Error( | ||
`Invalid operator, expected one of ${allowedOperators.join('|')}` | ||
); | ||
} | ||
}; |
Oops, something went wrong.