Help me write a cleaner method?

Hi all, I'm writing a function to break a list of assets into chunks with a max size. I have an array of objects with the following signature: {url: string, size: number}. I want to end up with the following:
{name: 'chunk-1', files: [url01, url01...]},
{name: 'chunk-2', files: [url01, url01...]},
]
{name: 'chunk-1', files: [url01, url01...]},
{name: 'chunk-2', files: [url01, url01...]},
]
If I have a single chunk, I'd like it to just be named chunk.
let acc = 0;
const chunks = [];
for (const song of songs) {
if (acc + song.size > CHUNK_SIZE) {
chunks.push({
name: "chunk" + "-" + (chunks.length + 1),
files: [song.url],
});
acc = 0;
} else {
if (chunks.length === 0) {
chunks.push({
filename: 'chunk' + "-" + (chunks.length + 1),
files: [song.url],
});
acc = song.size;
} else {
chunks[chunks.length - 1].files.push(song.url);
chunks[chunks.length - 1].size += song.size;
acc += song.size;
}
}
}
if(chunks.length === 1) {
chunks[0].filename = 'chunk';
}
let acc = 0;
const chunks = [];
for (const song of songs) {
if (acc + song.size > CHUNK_SIZE) {
chunks.push({
name: "chunk" + "-" + (chunks.length + 1),
files: [song.url],
});
acc = 0;
} else {
if (chunks.length === 0) {
chunks.push({
filename: 'chunk' + "-" + (chunks.length + 1),
files: [song.url],
});
acc = song.size;
} else {
chunks[chunks.length - 1].files.push(song.url);
chunks[chunks.length - 1].size += song.size;
acc += song.size;
}
}
}
if(chunks.length === 1) {
chunks[0].filename = 'chunk';
}
The way I'm naming chunks is slightly more complicated but irrelevant to the question. I'm wondering if there is a cleaner way to do this. This works fine and is fast, but to me feels a bit hacky. I'm not sure if that's because it is, or if I'm just being overly self-conscious.
13 Replies
Amos
Amos3y ago
Why does it feel hacky to you
benten
bentenOP3y ago
I think just because of the nested if/elses and logic duplication
Amos
Amos3y ago
I think it's fine 🤷‍♂️ would just rename acc to chunkSize or w/e to make it more readable. Don't waste time on stuff that's not important 😛
benten
bentenOP3y ago
I appreciate that, I need to stop second guessing my code
Amos
Amos3y ago
If it becomes a problem in the future (don't see why) then you can fix it then, if it works it works
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
benten
bentenOP3y ago
Yes you're right
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
benten
bentenOP3y ago
I ended up writing it as a reducer function and I think I prefer it
const chunks = songs.reduce(
(prev, curr) => {
if (prev[prev.length -1].size + curr.size > MAX_CHUNK_SIZE) {
prev.push({
filename: uniqueName + "-" + (prev.length + 1),
files: [curr.url],
size: curr.size,
});
prev[0].filename = uniqueName + "-1"
} else {
prev[prev.length -1].files.push(curr.url);
prev[prev.length -1].size += curr.size;
}
return prev
},
[{ filename: uniqueName, files: [], size: 0 }]
);
const chunks = songs.reduce(
(prev, curr) => {
if (prev[prev.length -1].size + curr.size > MAX_CHUNK_SIZE) {
prev.push({
filename: uniqueName + "-" + (prev.length + 1),
files: [curr.url],
size: curr.size,
});
prev[0].filename = uniqueName + "-1"
} else {
prev[prev.length -1].files.push(curr.url);
prev[prev.length -1].size += curr.size;
}
return prev
},
[{ filename: uniqueName, files: [], size: 0 }]
);
LOL we had the same idea
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
benten
bentenOP3y ago
Reducers are like 4x slower than for loops but considering my array is maxed at like 200 it won't be an issue
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
benten
bentenOP3y ago
I appreciate the help from both of you The first one was fine, but there was a cleaner way
Want results from more Discord servers?
Add your server