Skip to content

Instantly share code, notes, and snippets.

@sokolovstas
Created March 28, 2019 09:40
Show Gist options
  • Save sokolovstas/2d4694cfe89e90ed90bb5ea6c6c7165d to your computer and use it in GitHub Desktop.
Save sokolovstas/2d4694cfe89e90ed90bb5ea6c6c7165d to your computer and use it in GitHub Desktop.
static parseBoolean(str: any): boolean {
if (str === true) {
return true;
}
if (str === false) {
return false;
}
if (str === 1) {
return true;
}
if (str === 0) {
return false;
}
if (str === 'true') {
return true;
}
if (!str) {
return false;
}
switch (str.length) {
case 1: {
const ch0 = str.charAt(0);
if (ch0 === 'y' || ch0 === 'Y' ||
ch0 === 't' || ch0 === 'T' ||
ch0 === '1') {
return true;
}
if (ch0 === 'n' || ch0 === 'N' ||
ch0 === 'f' || ch0 === 'F' ||
ch0 === '0') {
return false;
}
break;
}
case 2: {
const ch0 = str.charAt(0);
const ch1 = str.charAt(1);
if ((ch0 === 'o' || ch0 === 'O') &&
(ch1 === 'n' || ch1 === 'N') ) {
return true;
}
if ((ch0 === 'n' || ch0 === 'N') &&
(ch1 === 'o' || ch1 === 'O') ) {
return false;
}
break;
}
case 3: {
const ch0 = str.charAt(0);
const ch1 = str.charAt(1);
const ch2 = str.charAt(2);
if ((ch0 === 'y' || ch0 === 'Y') &&
(ch1 === 'e' || ch1 === 'E') &&
(ch2 === 's' || ch2 === 'S') ) {
return true;
}
if ((ch0 === 'o' || ch0 === 'O') &&
(ch1 === 'f' || ch1 === 'F') &&
(ch2 === 'f' || ch2 === 'F') ) {
return false;
}
break;
}
case 4: {
const ch0 = str.charAt(0);
const ch1 = str.charAt(1);
const ch2 = str.charAt(2);
const ch3 = str.charAt(3);
if ((ch0 === 't' || ch0 === 'T') &&
(ch1 === 'r' || ch1 === 'R') &&
(ch2 === 'u' || ch2 === 'U') &&
(ch3 === 'e' || ch3 === 'E') ) {
return true;
}
break;
}
case 5: {
const ch0 = str.charAt(0);
const ch1 = str.charAt(1);
const ch2 = str.charAt(2);
const ch3 = str.charAt(3);
const ch4 = str.charAt(4);
if ((ch0 === 'f' || ch0 === 'F') &&
(ch1 === 'a' || ch1 === 'A') &&
(ch2 === 'l' || ch2 === 'L') &&
(ch3 === 's' || ch3 === 'S') &&
(ch4 === 'e' || ch4 === 'E') ) {
return false;
}
break;
}
default:
break;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment