I wonder if this could be simplified by not joining the paths, and instead keep adding files to each chunk while counting the length of each entry being added. If `current + next.length > maxChars`, start a new chunk. That way you don't need the `lastIndexOf` and other complexities.