mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-24 14:05:45 +03:00
Move tiny-version-compare module to helpers
This commit is contained in:
parent
c427034e27
commit
9173b0ee7a
4 changed files with 67 additions and 10 deletions
11
client/package-lock.json
generated
vendored
11
client/package-lock.json
generated
vendored
|
@ -6551,7 +6551,7 @@
|
|||
},
|
||||
"html-webpack-plugin": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz",
|
||||
"resolved": "http://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz",
|
||||
"integrity": "sha1-sBq71yOsqqeze2r0SS69oD2d03s=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
|
@ -6601,7 +6601,7 @@
|
|||
},
|
||||
"readable-stream": {
|
||||
"version": "1.0.34",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz",
|
||||
"resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz",
|
||||
"integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
|
@ -14930,7 +14930,7 @@
|
|||
},
|
||||
"through": {
|
||||
"version": "2.3.8",
|
||||
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
||||
"resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
||||
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=",
|
||||
"dev": true
|
||||
},
|
||||
|
@ -14965,11 +14965,6 @@
|
|||
"setimmediate": "^1.0.4"
|
||||
}
|
||||
},
|
||||
"tiny-version-compare": {
|
||||
"version": "0.9.1",
|
||||
"resolved": "https://registry.npmjs.org/tiny-version-compare/-/tiny-version-compare-0.9.1.tgz",
|
||||
"integrity": "sha512-kYim94l7ptSmj9rqxUMkrcMCJ448CS+hwqjA7OFcRi0ISdi0zjgdSUklQ4velVVECCjCo5frU3tNZ3oSgIKzsA=="
|
||||
},
|
||||
"tmp": {
|
||||
"version": "0.0.33",
|
||||
"resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz",
|
||||
|
|
1
client/package.json
vendored
1
client/package.json
vendored
|
@ -30,7 +30,6 @@
|
|||
"redux-actions": "^2.4.0",
|
||||
"redux-thunk": "^2.3.0",
|
||||
"svg-url-loader": "^2.3.2",
|
||||
"tiny-version-compare": "^0.9.1",
|
||||
"whatwg-fetch": "2.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
63
client/src/helpers/versionCompare.js
Normal file
63
client/src/helpers/versionCompare.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Project: tiny-version-compare https://github.com/bfred-it/tiny-version-compare
|
||||
* License (MIT) https://github.com/bfred-it/tiny-version-compare/blob/master/LICENSE
|
||||
*/
|
||||
const split = v => String(v).replace(/^[vr]/, '') // Drop initial 'v' or 'r'
|
||||
.replace(/([a-z]+)/gi, '.$1.') // Sort each word separately
|
||||
.replace(/[-.]+/g, '.') // Consider dashes as separators (+ trim multiple separators)
|
||||
.split('.');
|
||||
|
||||
// Development versions are considered "negative",
|
||||
// but localeCompare doesn't handle negative numbers.
|
||||
// This offset is applied to reset the lowest development version to 0
|
||||
const offset = (part) => {
|
||||
// Not numeric, return as is
|
||||
if (Number.isNaN(part)) {
|
||||
return part;
|
||||
}
|
||||
return 5 + Number(part);
|
||||
};
|
||||
|
||||
const parsePart = (part) => {
|
||||
// Missing, consider it zero
|
||||
if (typeof part === 'undefined') {
|
||||
return 0;
|
||||
}
|
||||
// Sort development versions
|
||||
switch (part.toLowerCase()) {
|
||||
case 'dev':
|
||||
return -5;
|
||||
case 'alpha':
|
||||
return -4;
|
||||
case 'beta':
|
||||
return -3;
|
||||
case 'rc':
|
||||
return -2;
|
||||
case 'pre':
|
||||
return -1;
|
||||
default:
|
||||
}
|
||||
// Return as is, it’s either a plain number or text that will be sorted alphabetically
|
||||
return part;
|
||||
};
|
||||
|
||||
const versionCompare = (prev, next) => {
|
||||
const a = split(prev);
|
||||
const b = split(next);
|
||||
for (let i = 0; i < a.length || i < b.length; i += 1) {
|
||||
const ai = offset(parsePart(a[i]));
|
||||
const bi = offset(parsePart(b[i]));
|
||||
const sort = String(ai).localeCompare(bi, 'en', {
|
||||
numeric: true,
|
||||
});
|
||||
// Once the difference is found,
|
||||
// stop comparing the rest of the parts
|
||||
if (sort !== 0) {
|
||||
return sort;
|
||||
}
|
||||
}
|
||||
// No difference found
|
||||
return 0;
|
||||
};
|
||||
|
||||
export default versionCompare;
|
|
@ -1,8 +1,8 @@
|
|||
import { combineReducers } from 'redux';
|
||||
import { handleActions } from 'redux-actions';
|
||||
import { loadingBarReducer } from 'react-redux-loading-bar';
|
||||
import versionCompare from 'tiny-version-compare';
|
||||
import nanoid from 'nanoid';
|
||||
import versionCompare from '../helpers/versionCompare';
|
||||
|
||||
import * as actions from '../actions';
|
||||
|
||||
|
|
Loading…
Reference in a new issue