Skip to content

Instantly share code, notes, and snippets.

@deenison
Last active April 29, 2016 03:04
Show Gist options
  • Save deenison/1924c23f27fe27fe7da68375d71c5ae8 to your computer and use it in GitHub Desktop.
Save deenison/1924c23f27fe27fe7da68375d71c5ae8 to your computer and use it in GitHub Desktop.
Gist that proposes have some fun with values (strings) that may or may not be a boolean.
/**
* Method that tests a string to check if it's value can be assumed as a (bool), regardless of being true/false.
* Note: If the string is empty, this function will return false since there's "no value" to be tested.
* @examples:
* "1".isBool() // returns true
* "yEs".isBool() // returns true
* "off".isBool() // returns true
* "".isBool() // returns false
* " ".isBool() // returns false
*
* @author Denison Martins <contact@denison.me>
*
* @return boolean
*/
String.prototype.isBool = function() {
"use strict";
var subject = this.toString().trim().toLowerCase();
return subject.isTrue() || subject.isFalse();
};
/**
* Method that tests a string to check if it's value can be assumed as a (bool)false.
* Note: If the string is empty, this function will return false since there's "no value" to be tested.
* @examples:
* "0".isFalse() // returns true
* "no".isFalse() // returns true
* "true".isFalse() // returns false
* "".isFalse() // returns false
* " oFF".isFalse() // returns true
*
* @author Denison Martins <contact@denison.me>
*
* @return boolean
*/
String.prototype.isFalse = function() {
"use strict";
var subject = this.toString().trim().toLowerCase();
switch (subject) {
case "0":
case "false":
case "off":
case "no":
case "n":
case "nil":
case "null":
return true;
default:
return false;
}
};
/*
This script tests the performance of three different implementations of the method `String.prototype.isFalse()`:
- the default one (using switch);
- using regexp;
- using array/lists;
*/
(function() {
"use strict";
const FALSE_VALUES = ["0", "false", "off", "no", "n", "nil", "null"];
const FALSE_VALUES_LENGTH = FALSE_VALUES.length;
const SUBJECTS_POOL_COUNT = 1000000;
const SUBJECT_MAX_LENGTH = 10;
// There's no need to extend Math using `Math.prototype` since Math is not a constructor and as such it doesn't own a `prototype` property.
Math.getRandomIntBetweenRange = function(minValue = 1, maxValue = 100) {
return this.floor(this.random() * (maxValue - minValue + 1)) + minValue;
};
Math.throwChance = function(successRate) {
return this.getRandomIntBetweenRange(1, 100) <= successRate;
}
function generateRandomStringOfLength(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
let methodsToBeTested = {
isFalse_switch: function(subject) {
subject = subject.trim().toLowerCase();
switch (subject) {
case "0":
case "false":
case "off":
case "no":
case "n":
case "nil":
case "null":
return true;
default:
return false;
}
},
isFalse_regex: function(subject) {
subject = subject.trim();
let falseValidatorRule = new RegExp('(?:^)('+ FALSE_VALUES.join("|") +')(?:(?= )|$)', "i");
let matches = falseValidatorRule.exec(subject);
return matches !== null && matches.length > 0;
},
isFalse_list: function(subject) {
subject = subject.trim().toLowerCase();
return FALSE_VALUES.indexOf(subject) >= 0;
}
};
function createSubjectsPool(forceNegativeValueChance = 25) {
let pool = [];
for (let i = 0; i < SUBJECTS_POOL_COUNT; i++) {
let subject;
if (Math.throwChance(forceNegativeValueChance)) {
subject = FALSE_VALUES[(Math.getRandomIntBetweenRange(0, FALSE_VALUES_LENGTH - 1))];
} else {
subject = generateRandomStringOfLength(Math.getRandomIntBetweenRange(1, SUBJECT_MAX_LENGTH));
}
pool.push(subject);
}
return pool;
}
let subjectsPool = createSubjectsPool();
let subjectsNegatives = 0;
function runTestOverMethod(methodName) {
let methodInstance = methodsToBeTested[methodName];
if (typeof methodInstance === "function") {
console.time(methodName);
for (let subjectIndex = 0; subjectIndex < SUBJECTS_POOL_COUNT; subjectIndex++) {
let subject = subjectsPool[subjectIndex];
if (methodInstance(subject)) {
subjectsNegatives++;
}
}
console.timeEnd(methodName);
} else {
console.error("Method not found.");
}
}
(function() {
console.log('Subjects count: '+ SUBJECTS_POOL_COUNT);
for (let methodName in methodsToBeTested) {
runTestOverMethod(methodName);
}
console.log('Total Negatives: '+ subjectsNegatives);
})();
})();
/**
* Method that tests a string to check if it's value can be assumed as a (bool)true.
* Note: If the string is empty, this function will return false since there's "no value" to be tested.
* @examples:
* "1".isTrue() // returns true
* "yes".isTrue() // returns true
* "True".isTrue() // returns false
* "".isTrue() // returns false
* " off".isTrue() // returns false
*
* @author Denison Martins <contact@denison.me>
*
* @return boolean
*/
String.prototype.isTrue = function() {
"use strict";
var subject = this.toString().trim().toLowerCase();
switch (subject) {
case "1":
case "true":
case "on":
case "yes":
case "y":
return true;
default:
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment