Skip to content

Instantly share code, notes, and snippets.

View helabenkhalfallah's full-sized avatar
🎯
Focusing

Héla Ben Khalfallah helabenkhalfallah

🎯
Focusing
View GitHub Profile
@helabenkhalfallah
helabenkhalfallah / quick-sort.js
Last active May 24, 2021 20:45
Quick Sort (JS)
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++) {
@helabenkhalfallah
helabenkhalfallah / vintage-binary-search.js
Last active July 10, 2022 16:55
Vintage Binary Search
// 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
@helabenkhalfallah
helabenkhalfallah / async-js-generator.js
Last active May 17, 2021 13:19
Async call using ES6 Generator
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
@helabenkhalfallah
helabenkhalfallah / httpFifoRequestsExecutor.js
Last active March 9, 2022 02:24
Async Http Requests Executor (FiFo) using ES6 Generator
function httpFiFoRequestsExecutor({
onTaskSuccess,
onTaskFail,
}) {
async function* execute(taskInfos, props) {
const {
taskIdentifier,
taskFn
} = taskInfos || {};
try {
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 !"));
}
});
@helabenkhalfallah
helabenkhalfallah / run-to-completion-function.js
Last active May 16, 2021 11:12
Run To Completion Function
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;
}
@helabenkhalfallah
helabenkhalfallah / js-curried-function-custom-logger.js
Created May 15, 2021 22:30
JS Curried Function (custom logger)
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)
@helabenkhalfallah
helabenkhalfallah / js-curried-function-universal-sort-by.js
Created May 15, 2021 22:28
JS Curried Function (universal sort by)
const universalSortBy = property => (a, b) => {
if (a[property] < b[property]) {
return -1;
}
if (a[property] > b[property]) {
return 1;
}
return 0;
};
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]$/;
@helabenkhalfallah
helabenkhalfallah / curried-function-2-converter.js
Last active May 15, 2021 23:11
JS Curried Function (Converter 2)
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"