Skip to content

Instantly share code, notes, and snippets.

@helabenkhalfallah
Created April 12, 2023 16:05
Show Gist options
  • Save helabenkhalfallah/721d8b5c7a78d04422f65020d30fd337 to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/721d8b5c7a78d04422f65020d30fd337 to your computer and use it in GitHub Desktop.
Algorithme Simple
const getItems = (data, start, end) => {
if (data &&
data.length > 0 &&
start >= 0 &&
end <= data.length &&
start < end) {
return data.slice(start, end); // immutable
}
return ([]);
};
const transform = (data, f) => {
if (data && data.length && f) {
return data.map(item => f(item)); // immutable
}
return ([]);
};
const getNextItem = (data, length, f) => {
if (data && data.length && f) {
const values = transform(getItems(data, length, length + 1), f);
return values && values.length ? values[0] : null;
}
return null;
}
// Affichage des premiers éléments
const pageSize = 3;
const dataSource = [1, 2, 3, 5, 6, 7];
const dataTransform = (item) => item * 2;
const items = getItems(dataSource, 0, pageSize); // [1, 2, 3]
console.log(items);
const dataInput = transform(items, dataTransform); // [1, 2, 3]
console.log(dataInput);
// Affichage de l'élément suivant
const nextItem = getNextItem(dataSource, dataInput.length, dataTransform);
console.log(nextItem); // 10
dataInput.push(nextItem);
console.log(dataInput); // [2, 4, 6, 10]
// Affichage de l'élément suivant
const nextItem1 = getNextItem(dataSource, dataInput.length, dataTransform);
console.log(nextItem1); // 12
dataInput.push(nextItem1);
console.log(dataInput); // [2, 4, 6, 10, 12]
// Affichage de l'élément suivant
const nextItem2 = getNextItem(dataSource, dataInput.length, dataTransform);
console.log(nextItem2); // 14
dataInput.push(nextItem2); //
console.log(dataInput); // [2, 4, 6, 10, 12, 14]
// Affichage de l'élément suivant
const nextItem3 = getNextItem(dataSource, dataInput.length, dataTransform);
console.log(nextItem3); // null
dataInput.push(nextItem3);
console.log(dataInput); // [2, 4, 6, 10, 12, 14, null]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment