Skip to content

Instantly share code, notes, and snippets.

@Gioni06
Last active August 13, 2021 15:04
Show Gist options
  • Save Gioni06/e9c6c8be356128fcb74ec0e1634a5b25 to your computer and use it in GitHub Desktop.
Save Gioni06/e9c6c8be356128fcb74ec0e1634a5b25 to your computer and use it in GitHub Desktop.
Fun with Arrays
const sourceA = [
[...Array(5).keys()].map((value) => {
return { ref: "sourceA", value };
}),
[...Array(13).keys()].map((value) => {
return { ref: "sourceB", value };
}),
[...Array(22).keys()].map((value) => {
return { ref: "sourceC", value };
}),
];
const sourceB = [
[...Array(5).keys()].map((value) => {
return { ref: "sourceA", value };
}),
[...Array(5).keys()].map((value) => {
return { ref: "sourceB", value };
}),
[...Array(5).keys()].map((value) => {
return { ref: "sourceC", value };
}),
];
const sourceC = [
[...Array(1).keys()].map((value) => {
return { ref: "sourceA", value };
}),
[...Array(0).keys()].map((value) => {
return { ref: "sourceB", value };
}),
[...Array(100).keys()].map((value) => {
return { ref: "sourceC", value };
}),
];
const sourceD = [
[...Array(1).keys()].map((value) => {
return { ref: "sourceA", value };
}),
[...Array(2).keys()].map((value) => {
return { ref: "sourceB", value };
}),
[...Array(3).keys()].map((value) => {
return { ref: "sourceC", value };
}),
[...Array(8).keys()].map((value) => {
return { ref: "sourceD", value };
}),
[...Array(2).keys()].map((value) => {
return { ref: "sourceE", value };
}),
];
const sourceE = [
[...Array(100).keys()].map((value) => {
return { ref: "sourceA", value };
}),
[...Array(100).keys()].map((value) => {
return { ref: "sourceB", value };
}),
[...Array(100).keys()].map((value) => {
return { ref: "sourceC", value };
}),
[...Array(100).keys()].map((value) => {
return { ref: "sourceD", value };
}),
[...Array(100).keys()].map((value) => {
return { ref: "sourceE", value };
}),
];
const roundRobinWithBreakAtDesiredLength = (arr, results, targetLength) => {
if (arr.length === 0) return results;
const newResults = arr.reverse().reduce(
(acc, current) => {
if (current.length > 0 && acc.results.length < targetLength) {
acc.results.push(current.shift());
acc.arr.push(current);
}
return acc;
},
{ arr: [], results }
);
return roundRobinWithBreakAtDesiredLength(
newResults.arr,
newResults.results,
targetLength
);
};
function doStuff(source, targetLength) {
const result = [];
let s = JSON.parse(JSON.stringify(source)); // poor mans deep clone
return roundRobinWithBreakAtDesiredLength(s, result, targetLength);
}
/*
console.log(doStuff(sourceA, 20))
console.log(doStuff(sourceA, 20).length)
console.log(doStuff(sourceB, 20))
console.log(doStuff(sourceB, 20).length)
console.log(doStuff(sourceC, 20))
console.log(doStuff(sourceC, 20).length)
console.log(doStuff(sourceD, 20).length)
*/
var map = doStuff(sourceE, 21).reduce((acc, current) => {
acc = {
...acc,
[current.ref]: Object.prototype.hasOwnProperty.call(acc, current.ref)
? acc[current.ref] + 1
: 1,
};
return acc;
}, {});
console.log(doStuff(sourceE, 21));
console.log(
"\n---------------------------------------------------------------------------\n"
);
console.log(map);
console.log(
"\n---------------------------------------------------------------------------"
);
console.assert(
doStuff(sourceE, 21).length === 21,
"expect result array to have 21 items"
);
console.assert(map.sourceA === 4, "expect 4 elements form this array");
console.assert(map.sourceB === 4, "expect 4 elements form this array");
console.assert(map.sourceC === 4, "expect 4 elements form this array");
console.assert(map.sourceD === 4, "expect 4 elements form this array");
console.assert(
map.sourceE === 5,
"expect 21. element to come from the last array"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment