Use classes

This commit is contained in:
Florine W. Dekker 2019-06-13 19:32:23 +02:00
parent 1ae1d5d328
commit 726f32c4dd
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
1 changed files with 82 additions and 74 deletions

View File

@ -22,34 +22,38 @@ const modulo = (a, b) => (((a % b) + b) % b);
/// Name generator /// Name generator
function Name(size) { class Name {
constructor(size) {
size = (size === undefined ? 10 : size); size = (size === undefined ? 10 : size);
this.count_ = 0; this.count_ = 0;
this.history_ = []; this.history_ = [];
this.size_ = size; this.size_ = size;
} }
// Returns random name
Name.prototype.generate_ = () => nameList[Math.floor(Math.random() * nameList.length)];
// Adds given name to history // Returns a random name
Name.prototype.add = function (name) { static generate_() {
return nameList[Math.floor(Math.random() * nameList.length)];
}
// Adds given name to history
add(name) {
if (name !== undefined) { if (name !== undefined) {
this.history_[modulo(this.count_, this.size_)] = name; this.history_[modulo(this.count_, this.size_)] = name;
this.count_++; this.count_++;
} }
return name; return name;
}; }
// Adds a random name to history // Adds a random name to history
Name.prototype.addRandom = function () { addRandom() {
return this.add(this.generate_()); return this.add(Name.generate_());
}; };
// Returns history // Returns history
Name.prototype.getHistory = function (separator, count) { getHistory(separator, count) {
count = (count === undefined ? Math.min(this.size_, this.count_) : count); count = (count === undefined ? Math.min(this.size_, this.count_) : count);
const out = []; const out = [];
@ -62,30 +66,33 @@ Name.prototype.getHistory = function (separator, count) {
} }
return out.join(separator); return out.join(separator);
}; };
}
/// Jaro-Winkler similarity /// Jaro-Winkler similarity
function JaroWinkler(name) { class JaroWinkler {
constructor(name) {
this.algo_ = jaro_winkler;
this.name_ = name; this.name_ = name;
this.reset_(); this.reset_();
this.populate_(); this.populate_();
} }
// Set string algorithm
JaroWinkler.prototype.algo_ = jaro_winkler;
// Comparator for values returned by algorithm // Comparator for values returned by algorithm
JaroWinkler.prototype.comparator_ = (a, b) => (b[1] - a[1]); static comparator_(a, b) {
return b[1] - a[1];
}
// Resets object // Resets object
JaroWinkler.prototype.reset_ = function () { reset_() {
this.hits_ = []; this.hits_ = [];
}; };
// Populates hits // Populates hits
JaroWinkler.prototype.populate_ = function () { populate_() {
const listSize = nameList.length; const listSize = nameList.length;
for (let i = 0; i < listSize; i++) { for (let i = 0; i < listSize; i++) {
@ -95,18 +102,19 @@ JaroWinkler.prototype.populate_ = function () {
this.hits_[i] = [name, value]; this.hits_[i] = [name, value];
} }
this.hits_.sort(this.comparator_); this.hits_.sort(JaroWinkler.comparator_);
}; };
// Returns best matches, limited to amount // Returns `amount` best matches
JaroWinkler.prototype.getSimilar = function (amount) { getSimilar(amount) {
const similar = []; const similar = [];
for (let i = 0; i < amount; i++) for (let i = 0; i < amount; i++)
similar[i] = this.hits_[i][0]; similar[i] = this.hits_[i][0];
return similar; return similar;
}; };
}
/// DOM elements /// DOM elements