Last active
August 13, 2021 15:04
-
-
Save Gioni06/e9c6c8be356128fcb74ec0e1634a5b25 to your computer and use it in GitHub Desktop.
Revisions
-
Gioni06 revised this gist
Aug 13, 2021 . 1 changed file with 139 additions and 86 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,87 +1,140 @@ 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" );
-
Gioni06 revised this gist
Aug 13, 2021 . 1 changed file with 68 additions and 44 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,56 +1,80 @@ 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 roundRobinWithBreakAtDesiredLength = (arr, results, targetLength) => { if (arr.length === 0) return results const newResults = arr.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 = [] const s = JSON.parse(JSON.stringify(source)) // poor mans deep clone return roundRobinWithBreakAtDesiredLength(s, result, targetLength) } console.log(doStuff(sourceA, 20)) -
Gioni06 created this gist
Aug 13, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ 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 roundRobinWithBreakAtDesiredLength = (arr, results, targetLength) => { if (arr.length === 0) return results; const newResults = arr.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 = []; const 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)