common.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { isNil, isPlainObject, set, startCase, transform } from 'lodash-es'
  2. import crypto from 'node:crypto'
  3. /* eslint-disable promise/param-names */
  4. export function createDeferred () {
  5. let result, resolve, reject
  6. return {
  7. resolve: function (value) {
  8. if (resolve) {
  9. resolve(value)
  10. } else {
  11. result = result || new Promise(function (r) { r(value) })
  12. }
  13. },
  14. reject: function (reason) {
  15. if (reject) {
  16. reject(reason)
  17. } else {
  18. result = result || new Promise(function (x, j) { j(reason) })
  19. }
  20. },
  21. promise: new Promise(function (r, j) {
  22. if (result) {
  23. r(result)
  24. } else {
  25. resolve = r
  26. reject = j
  27. }
  28. })
  29. }
  30. }
  31. /**
  32. * Decode a tree path
  33. *
  34. * @param {string} str String to decode
  35. * @returns Decoded tree path
  36. */
  37. export function decodeTreePath (str) {
  38. return str?.replaceAll('.', '/')
  39. }
  40. /**
  41. * Encode a tree path
  42. *
  43. * @param {string} str String to encode
  44. * @returns Encoded tree path
  45. */
  46. export function encodeTreePath (str) {
  47. return str?.toLowerCase()?.replaceAll('/', '.') || ''
  48. }
  49. /**
  50. * Encode a folder path (to support legacy PostgresSQL ltree)
  51. *
  52. * @param {string} val String to encode
  53. * @returns Encoded folder path
  54. */
  55. export function encodeFolderPath (val) {
  56. return WIKI.db.LEGACY ? val?.replaceAll('-', '_') : val
  57. }
  58. /**
  59. * Decode a folder path (to support legacy PostgresSQL ltree)
  60. *
  61. * @param {string} val String to decode
  62. * @returns Decoded folder path
  63. */
  64. export function decodeFolderPath (val) {
  65. return WIKI.db.LEGACY ? val?.replaceAll('_', '-') : val
  66. }
  67. /**
  68. * Generate SHA-1 Hash of a string
  69. *
  70. * @param {string} str String to hash
  71. * @returns Hashed string
  72. */
  73. export function generateHash (str) {
  74. return crypto.createHash('sha1').update(str).digest('hex')
  75. }
  76. /**
  77. * Get default value of type
  78. *
  79. * @param {any} type primitive type name
  80. * @returns Default value
  81. */
  82. export function getTypeDefaultValue (type) {
  83. switch (type.toLowerCase()) {
  84. case 'string':
  85. return ''
  86. case 'number':
  87. return 0
  88. case 'boolean':
  89. return false
  90. }
  91. }
  92. export function parseModuleProps (props) {
  93. return transform(props, (result, value, key) => {
  94. let defaultValue = ''
  95. if (isPlainObject(value)) {
  96. defaultValue = !isNil(value.default) ? value.default : getTypeDefaultValue(value.type)
  97. } else {
  98. defaultValue = getTypeDefaultValue(value)
  99. }
  100. set(result, key, {
  101. default: defaultValue,
  102. type: (value.type || value).toLowerCase(),
  103. title: value.title || startCase(key),
  104. hint: value.hint || '',
  105. enum: value.enum || false,
  106. enumDisplay: value.enumDisplay || 'select',
  107. multiline: value.multiline || false,
  108. sensitive: value.sensitive || false,
  109. icon: value.icon || 'rename',
  110. order: value.order || 100,
  111. if: value.if ?? []
  112. })
  113. return result
  114. }, {})
  115. }
  116. export function getDictNameFromLocale (locale) {
  117. const loc = locale.length > 2 ? locale.substring(0, 2) : locale
  118. if (loc in WIKI.config.search.dictOverrides) {
  119. return WIKI.config.search.dictOverrides[loc]
  120. } else {
  121. return WIKI.data.tsDictMappings[loc] ?? 'simple'
  122. }
  123. }