ProjectHelper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let ProjectHelper
  15. const ENGINE_TO_COMPILER_MAP = {
  16. latex_dvipdf: 'latex',
  17. pdflatex: 'pdflatex',
  18. xelatex: 'xelatex',
  19. lualatex: 'lualatex'
  20. }
  21. const { ObjectId } = require('../../infrastructure/mongojs')
  22. module.exports = ProjectHelper = {
  23. compilerFromV1Engine(engine) {
  24. return ENGINE_TO_COMPILER_MAP[engine]
  25. },
  26. isArchived(project, userId) {
  27. userId = ObjectId(userId)
  28. if (Array.isArray(project.archived)) {
  29. return project.archived.find(id => id.equals(userId)) !== undefined
  30. } else {
  31. return !!project.archived
  32. }
  33. },
  34. isTrashed(project, userId) {
  35. userId = ObjectId(userId)
  36. if (project.trashed) {
  37. return project.trashed.find(id => id.equals(userId)) !== undefined
  38. } else {
  39. return false
  40. }
  41. },
  42. isArchivedOrTrashed(project, userId) {
  43. return (
  44. ProjectHelper.isArchived(project, userId) ||
  45. ProjectHelper.isTrashed(project, userId)
  46. )
  47. },
  48. ensureNameIsUnique(nameList, name, suffixes, maxLength, callback) {
  49. // create a set of all project names
  50. if (suffixes == null) {
  51. suffixes = []
  52. }
  53. if (callback == null) {
  54. callback = function(error, name, changed) {}
  55. }
  56. const allNames = new Set(nameList)
  57. const isUnique = x => !allNames.has(x)
  58. // check if the supplied name is already unique
  59. if (isUnique(name)) {
  60. return callback(null, name, false)
  61. }
  62. // the name already exists, try adding the user-supplied suffixes to generate a unique name
  63. for (let suffix of Array.from(suffixes)) {
  64. const candidateName = ProjectHelper._addSuffixToProjectName(
  65. name,
  66. suffix,
  67. maxLength
  68. )
  69. if (isUnique(candidateName)) {
  70. return callback(null, candidateName, true)
  71. }
  72. }
  73. // if there are no (more) suffixes, use a numeric one
  74. const uniqueName = ProjectHelper._addNumericSuffixToProjectName(
  75. name,
  76. allNames,
  77. maxLength
  78. )
  79. if (uniqueName != null) {
  80. return callback(null, uniqueName, true)
  81. } else {
  82. return callback(
  83. new Error(`Failed to generate a unique name for: ${name}`)
  84. )
  85. }
  86. },
  87. _addSuffixToProjectName(name, suffix, maxLength) {
  88. // append the suffix and truncate the project title if needed
  89. if (suffix == null) {
  90. suffix = ''
  91. }
  92. const truncatedLength = maxLength - suffix.length
  93. return name.substr(0, truncatedLength) + suffix
  94. },
  95. _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
  96. const NUMERIC_SUFFIX_MATCH = / \((\d+)\)$/
  97. const suffixedName = function(basename, number) {
  98. const suffix = ` (${number})`
  99. return basename.substr(0, maxLength - suffix.length) + suffix
  100. }
  101. const match = name.match(NUMERIC_SUFFIX_MATCH)
  102. let basename = name
  103. let n = 1
  104. const last = allProjectNames.size + n
  105. if (match != null) {
  106. basename = name.replace(NUMERIC_SUFFIX_MATCH, '')
  107. n = parseInt(match[1])
  108. }
  109. while (n <= last) {
  110. const candidate = suffixedName(basename, n)
  111. if (!allProjectNames.has(candidate)) {
  112. return candidate
  113. }
  114. n += 1
  115. }
  116. return null
  117. }
  118. }