Last active
September 7, 2020 11:14
-
-
Save petsel/a724952d229fbf07b6e94672a1c7c45a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* `Boolean.isTrue` / `Boolean.isFalse` is a fun implementation of Smalltalk's [https://en.wikipedia.org/wiki/Smalltalk] | |
* `ifTrue` / `ifFalse` control structures [https://en.wikipedia.org/wiki/Smalltalk#Control_structures]. | |
* | |
* It is nothing one would really desire in JavaScript unless one is looking for an alternative to its native `if...else` | |
* statement and has deeply fallen in love with using function expressions all along. | |
* | |
* EN: [https://en.wikipedia.org/wiki/Smalltalk#Control_structures] | |
* DE: [https://de.wikipedia.org/wiki/Smalltalk_(Programmiersprache)#IF-Anweisung] | |
*/ | |
/** | |
* The following gist implementation is due to a recently asked question on StackOverflow ... | |
* [https://stackoverflow.com/questions/63709001/alternative-approaches-to-multiple-if-else-conditions] | |
* ... which reads *"alternative approaches to multiple if else conditions"*. | |
* | |
* Someone was contributing a solution/approach for *"the most over-engineered solution" contest* ... | |
* [https://stackoverflow.com/questions/63709001/alternative-approaches-to-multiple-if-else-conditions/63709274#63709274] | |
* | |
* In order to respond to and counter it a 10 year old code base needed to be revitalized and polished up ... | |
* ... [https://github.com/petsel/javascript-api-extensions/blob/master/core/smalltalk-inspired-control-structures/Boolean.ifTrue.ifFalse.js] | |
*/ | |
(function (Boolean, Array, Object) { | |
const boolPrototype = Boolean.prototype; | |
const FUNCTION_TYPE = (typeof Boolean); | |
function isFunction(type) { | |
return ( | |
(typeof type == FUNCTION_TYPE) | |
&& (typeof type.call == FUNCTION_TYPE) | |
&& (typeof type.apply == FUNCTION_TYPE) | |
); | |
} | |
const isArray = Array.isArray; | |
const arrayFrom = Array.from; | |
function getSanitizedRestArgs(args) { | |
args = arrayFrom(args).slice(2); | |
if ((args.length === 1) && isArray(args[0])) { | |
args = args.flat(); | |
} | |
return args; | |
} | |
function getSanitizedTarget(target) { | |
/* eslint-disable */ | |
/* jshint ignore:start */ | |
/* ignore jslint start */ | |
/* no-eq-null */ | |
return ((target != null) && target) || null; | |
/* ignore jslint end */ | |
/* jshint ignore:end */ | |
/* eslint-enable */ | |
} | |
/** | |
* The reason for introducing `getSanitizedRestArgs` is to achieve a unified | |
* and more convinient handling of how to pass and handle/interpret the | |
* additionally passed (rest) arguments. | |
* | |
* e.g. | |
* (true).isTrue((...args) => console.log(args), null, 5, 6, 7, 8); // expect: [5, 6, 7, 8] | |
* (true).isTrue((...args) => console.log(args), null, [5, 6, 7, 8]); // expect: [5, 6, 7, 8] | |
* | |
* (true).isTrue((...args) => console.log(args), null, [5, 6, [5, 6, 7, 8], 7, 8]); // expect: [5, 6, [5, 6, 7, 8], 7, 8] | |
* (true).isTrue((...args) => console.log(args), null, 5, 6, [5, 6, 7, 8], 7, 8); // expect: [5, 6, [5, 6, 7, 8], 7, 8] | |
*/ | |
function isTrue(handler, target/*, ...args*/) { | |
const isTrue = this.valueOf(); | |
if (isTrue && isFunction(handler)) { | |
// handler.apply(getSanitizedTarget(target), args.flat()); // does not meet the goal. | |
handler.apply(getSanitizedTarget(target), getSanitizedRestArgs(arguments)); | |
} | |
return isTrue; | |
} | |
// isTrue.toString = () => 'isTrue() { [native code] }'; | |
// isTrue.toString = () => 'true() { [native code] }'; | |
function isFalse(handler, target/*, ...args*/) { | |
const isTrue = this.valueOf(); | |
if (!isTrue && isFunction(handler)) { | |
// handler.apply(getSanitizedTarget(target), args.flat()); // does not meet the goal. | |
handler.apply(getSanitizedTarget(target), getSanitizedRestArgs(arguments)); | |
} | |
return isTrue; | |
} | |
// isFalse.toString = () => 'isFalse() { [native code] }'; | |
// isFalse.toString = () => 'false() { [native code] }'; | |
Object.defineProperty(boolPrototype, 'isTrue', { | |
// Object.defineProperty(boolPrototype, 'true', { | |
configurable: true, | |
writable: true, | |
value: isTrue | |
}); | |
Object.defineProperty(boolPrototype, 'isFalse', { | |
// Object.defineProperty(boolPrototype, 'false', { | |
configurable: true, | |
writable: true, | |
value: isFalse | |
}); | |
}(Boolean, Array, Object)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// proof of concept in coming up with the likely most over-engineered approach answering a Q. on StackOverflow | |
// [https://stackoverflow.com/questions/63709001/alternative-approaches-to-multiple-if-else-conditions] | |
function assignTableName(name) { | |
table_name = name; | |
} | |
let table_name = ""; | |
let date, currentDate, source; | |
let isCurrent, isS3; | |
// test for 'Table4' | |
// | |
date = '* now *'; | |
currentDate = 'now'; | |
source = '* s3 *' | |
isCurrent = (date === currentDate); | |
isS3 = (source === 's3'); | |
isCurrent | |
.isTrue(() => isS3 | |
.isTrue(assignTableName, null, 'Table1') | |
.isFalse(assignTableName, null, 'Table2') | |
) | |
.isFalse(() => isS3 | |
.isTrue(assignTableName, null, 'Table3') | |
.isFalse(assignTableName, null, 'Table4') | |
); | |
console.log(table_name); | |
// test for 'Table3' | |
// | |
date = '* now *'; | |
currentDate = 'now'; | |
source = 's3' | |
isCurrent = (date === currentDate); | |
isS3 = (source === 's3'); | |
isCurrent | |
.isTrue(() => isS3 | |
.isTrue(assignTableName, null, 'Table1') | |
.isFalse(assignTableName, null, 'Table2') | |
) | |
.isFalse(() => isS3 | |
.isTrue(assignTableName, null, 'Table3') | |
.isFalse(assignTableName, null, 'Table4') | |
); | |
console.log(table_name); | |
// test for 'Table1' | |
// | |
date = 'now'; | |
currentDate = 'now'; | |
source = 's3' | |
isCurrent = (date === currentDate); | |
isS3 = (source === 's3'); | |
isCurrent | |
.isTrue(() => isS3 | |
.isTrue(assignTableName, null, 'Table1') | |
.isFalse(assignTableName, null, 'Table2') | |
) | |
.isFalse(() => isS3 | |
.isTrue(assignTableName, null, 'Table3') | |
.isFalse(assignTableName, null, 'Table4') | |
); | |
console.log(table_name); | |
// test for 'Table2' | |
// | |
date = 'now'; | |
currentDate = 'now'; | |
source = '* s3 *' | |
isCurrent = (date === currentDate); | |
isS3 = (source === 's3'); | |
isCurrent | |
.isTrue(() => isS3 | |
.isTrue(assignTableName, null, 'Table1') | |
.isFalse(assignTableName, null, 'Table2') | |
) | |
.isFalse(() => isS3 | |
.isTrue(assignTableName, null, 'Table3') | |
.isFalse(assignTableName, null, 'Table4') | |
); | |
console.log(table_name); |
Haha, you definitely won the contest 😄
Thanks, it means a lot to me. I accept the honor.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Haha, you definitely won the contest 😄