Limits.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. module.exports = {
  2. // compute the total size of the document in chararacters, including newlines
  3. getTotalSizeOfLines(lines) {
  4. let size = 0
  5. for (const line of lines) {
  6. size += line.length + 1 // include the newline
  7. }
  8. return size
  9. },
  10. // check whether the total size of the document in characters exceeds the
  11. // maxDocLength.
  12. //
  13. // The estimated size should be an upper bound on the true size, typically
  14. // it will be the size of the JSON.stringified array of lines. If the
  15. // estimated size is less than the maxDocLength then we know that the total
  16. // size of lines will also be less than maxDocLength.
  17. docIsTooLarge(estimatedSize, lines, maxDocLength) {
  18. if (estimatedSize <= maxDocLength) {
  19. return false // definitely under the limit, no need to calculate the total size
  20. }
  21. // calculate the total size, bailing out early if the size limit is reached
  22. let size = 0
  23. for (const line of lines) {
  24. size += line.length + 1 // include the newline
  25. if (size > maxDocLength) return true
  26. }
  27. // since we didn't hit the limit in the loop, the document is within the allowed length
  28. return false
  29. },
  30. }