Skip to content

Instantly share code, notes, and snippets.

@albertpark
Created August 20, 2021 21:32
Show Gist options
  • Save albertpark/207a722bb119427f4d608d875af83b13 to your computer and use it in GitHub Desktop.
Save albertpark/207a722bb119427f4d608d875af83b13 to your computer and use it in GitHub Desktop.
Filter available domains on uniteddomains.com
/***
uniteddomains.com
collect domain names with the shortest tld first and sort it by price (also handles duplicate names)
collect all the available domain and group them by the length of the tld
then sort them by price and alphabetical order
***/
(() => {
"use strict";
Number.prototype.toMoney = function(decimals = 2, decimal_separator = '.', thousands_separator = ',') {
let n = this;
let c = isNaN(decimals) ? 2 : Math.abs(decimals);
let sign = n < 0 ? '-' : '';
let i = parseInt(n = Math.abs(n).toFixed(c)) + '';
let j = i.length;
let k = j > 3 ? j % 3 : 0;
let x = k ? i.substr(0, k) + thousands_separator : '';
let y = i.substr(k).replace(/(\d{3})(?=\d)/g, '$1' + thousands_separator);
let z = c ? decimal_separator + Math.abs(n - i).toFixed(c).slice(2) : '';
return sign + x + y + z;
};
let collect = {};
let unique = {};
// hide all the non-available domains
$('#search-results').find('tr.status-taken').css({visibility: 'visible', display: 'none'})
// go throught all the available domains and tidy them
$('#search-results').find('tr.status-available').each(function(idx) {
// only print the first one
// if (idx > 0) return;
let ele = $(this).find('td.domain');
// console.log(ele.get(0).innerText);
let can = ele.text().split(' ');
let name = can[0].slice(0, -2);
let tld = name.split('.');
// we do not want extra tld such as .com.us
tld.splice(0, 1);
tld = tld.join('.');
let price = can[3];
// convert to us dollars which is 1.09 at 2020-04-15
let usd = '$' + (convert(price) * 1.09).toMoney();
// console.log(tld);
const obj = {
name: name,
price: price,
usd: usd,
}
// console.log(tld);
if (collect[tld.length] === undefined) {
collect[tld.length] = [];
}
if (!(name in unique)) {
unique[name] = true;
collect[tld.length].push(obj);
}
});
// clean up the currency string to a float
function convert(currency) {
// Using replace() method to make currency string suitable
// for parseFloat() to convert
let temp = currency.replace(/[^0-9.-]+/g, '');
// Converting string to float or double and return
return parseFloat(temp);
}
// sort by price then name
function byprice(a, b) {
const af = parseFloat(a.price.replace(/€/g, '').replace(/,/g, ''));
const bf = parseFloat(b.price.replace(/€/g, '').replace(/,/g, ''));
// console.log([/af/, af, /bf/, bf])
if (af === bf)
return a.name.localeCompare(b.name);
else
return af - bf;
}
// making all the object iterable
Object.prototype[Symbol.iterator] = function() {
const ref = this;
let keys = [];
for (let key in this) {
keys.push(key);
}
return {
next: function() {
if (this._keys && this._obj && this._index < this._keys.length) {
const key = this._keys[this._index];
//console.log("Iteration " + this._index + ", key=" + key);
this._index++;
return { value: {key: key, value: this._obj[key]}, done: false };
} else {
//console.log("done, index=" + this._index + ", keys=" + this._keys);
return { done: true };
}
},
_index: 0,
_keys: keys,
_obj: ref,
};
};
// console.log(collect);
for (let each of collect) {
console.log(each.key, each.value.sort(byprice));
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment