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