| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- // @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;
|