This file contains hidden or 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
const quickSort = (items) => { | |
if (!items || items.length < 2) { | |
return items | |
} | |
const pivot = items[items.length - 1]; | |
const left = []; | |
const right = []; | |
for (let i = 0; i < items.length - 1; i++) { |
This file contains hidden or 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
// binary search need a pre-sorted array | |
const binarySearch = (items, itemToFind, start, end) => { | |
// Base Condition | |
if (start > end) return null; | |
// Find the middle index | |
const mid = Math.floor((start + end) / 2); | |
// Compare mid with given key itemToFind |
This file contains hidden or 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
function* fetchUsers() { | |
const response = yield fetch('https://jsonplaceholder.typicode.com/users'); | |
const jsonResponse = yield response.json(); | |
} | |
const fetchUsersTask = fetchUsers(); // nothing happens here | |
const fetchUsersPromise = fetchUsersTask.next().value; // the first yiled will return a promise | |
// launch the first promise | |
fetchUsersPromise |
This file contains hidden or 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
function httpFiFoRequestsExecutor({ | |
onTaskSuccess, | |
onTaskFail, | |
}) { | |
async function* execute(taskInfos, props) { | |
const { | |
taskIdentifier, | |
taskFn | |
} = taskInfos || {}; | |
try { |
This file contains hidden or 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
const doLongOperation = (operation) => new Promise((resolve, reject) => { | |
if (operation) { | |
console.log('Lets resolve the operation'); | |
resolve(operation); | |
} else { | |
console.log('Lets reject the operation'); | |
reject(new Error("Empty operation !")); | |
} | |
}); |
This file contains hidden or 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
const doOperations = (x, y) => { | |
const operation1 = x + y; | |
console.log('operation1 : ', operation1); | |
const operation2 = operation1 - y; | |
console.log('operation2 : ', operation2); | |
const operation3 = (operation1 * operation2) + 2 * (x - y); | |
console.log('operation3 : ', operation3); | |
return operation3; | |
} |
This file contains hidden or 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
const log = level => (message, ...optionals) => { | |
switch (level) { | |
case 'INFO': | |
API.info(message, optionals) | |
break | |
case 'WARN': | |
API.warn(message, optionals) | |
break | |
case 'ERROR': | |
API.error(message, optionals) |
This file contains hidden or 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
const universalSortBy = property => (a, b) => { | |
if (a[property] < b[property]) { | |
return -1; | |
} | |
if (a[property] > b[property]) { | |
return 1; | |
} | |
return 0; | |
}; |
This file contains hidden or 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
const universalMatcher = pattern => value => ( | |
value ? | |
(value.match(pattern)&& value.match(pattern).length >= 1 ? true : false) | |
:false | |
); | |
const floatPattern = /^[0-9]*[,.][0-9]+$/; | |
const intPattern = /^[0-9]*$/; | |
const alphaNumeric = /^[A-Za-z0-9]$/; |
This file contains hidden or 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
const converterOld2 = (toUnit) => (factor) => (offset) => (input) => { | |
const converterOffset = offset || 0; | |
return [((converterOffset + input) * factor).toFixed(2), toUnit].join(' '); | |
}; | |
const kmConverterWithUnit = converterOld2('km'); // function with a single params | |
const kmConverterWithFactor = kmConverterWithUnit(1.60936); // function with a single params | |
const kmConverter = kmConverterWithFactor(undefined); // function with a single params | |
console.log('kmConverter : ', kmConverter); | |
console.log('kmConverter(10) : ', kmConverter(10)); // "16.09 km" |