mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
88 lines
1.7 KiB
JavaScript
88 lines
1.7 KiB
JavaScript
// @ts-check
|
|
/**
|
|
* @param {string | null} a
|
|
* @param {string | null} b
|
|
* @returns {0 | 1 | -1}
|
|
*/
|
|
const compare = (a, b) => {
|
|
if (a === b) return 0;
|
|
if (b == null) {
|
|
return 1;
|
|
}
|
|
if (a == null) {
|
|
return -1;
|
|
}
|
|
|
|
if (a.length !== b.length) {
|
|
const r = a.length - b.length;
|
|
if (r > 0) {
|
|
return 1;
|
|
}
|
|
if (r < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
for (let i = 0; i < a.length; i++) {
|
|
if (b[i] == null) {
|
|
return 1;
|
|
}
|
|
if (a[i] < b[i]) {
|
|
return -1;
|
|
}
|
|
if (a[i] > b[i]) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
};
|
|
|
|
/**
|
|
* @param {import('gorhill-publicsuffixlist').default | null} [gorhill]
|
|
*/
|
|
const createDomainSorter = (gorhill = null) => {
|
|
const cached = require('./cached-tld-parse');
|
|
|
|
if (gorhill) {
|
|
/**
|
|
* @param {string} input
|
|
*/
|
|
const getDomain = cached.createCachedGorhillGetDomain(gorhill);
|
|
|
|
/**
|
|
* @param {string} a
|
|
* @param {string} b
|
|
* @returns {0 | 1 | -1}
|
|
*/
|
|
return (a, b) => {
|
|
if (a === b) return 0;
|
|
|
|
const aDomain = getDomain(a);
|
|
const bDomain = getDomain(b);
|
|
|
|
const resultDomain = compare(aDomain, bDomain);
|
|
return resultDomain !== 0 ? resultDomain : compare(a, b);
|
|
};
|
|
}
|
|
|
|
const tldts = cached;
|
|
/**
|
|
* @param {string} a
|
|
* @param {string} b
|
|
* @returns {0 | 1 | -1}
|
|
*/
|
|
return (a, b) => {
|
|
if (a === b) return 0;
|
|
|
|
const aDomain = tldts.parse(a).domain;
|
|
const bDomain = tldts.parse(b).domain;
|
|
|
|
const resultDomain = compare(aDomain, bDomain);
|
|
return resultDomain !== 0 ? resultDomain : compare(a, b);
|
|
};
|
|
};
|
|
|
|
module.exports = createDomainSorter();
|
|
module.exports.createDomainSorter = createDomainSorter;
|