You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @copyright 2023 Christopher Ng <chrng8@gmail.com>
  3. *
  4. * @author Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import camelCase from 'camelcase'
  23. import type { DAVResultResponseProps } from 'webdav'
  24. import type { ServerTag, Tag, TagWithId } from './types.js'
  25. export const parseTags = (tags: { props: DAVResultResponseProps }[]): TagWithId[] => {
  26. return tags.map(({ props }) => Object.fromEntries(
  27. Object.entries(props)
  28. .map(([key, value]) => [camelCase(key), value]),
  29. )) as TagWithId[]
  30. }
  31. /**
  32. * Parse id from `Content-Location` header
  33. * @param url URL to parse
  34. */
  35. export const parseIdFromLocation = (url: string): number => {
  36. const queryPos = url.indexOf('?')
  37. if (queryPos > 0) {
  38. url = url.substring(0, queryPos)
  39. }
  40. const parts = url.split('/')
  41. let result
  42. do {
  43. result = parts[parts.length - 1]
  44. parts.pop()
  45. // note: first result can be empty when there is a trailing slash,
  46. // so we take the part before that
  47. } while (!result && parts.length > 0)
  48. return Number(result)
  49. }
  50. export const formatTag = (initialTag: Tag | ServerTag): ServerTag => {
  51. const tag: any = { ...initialTag }
  52. if (tag.name && !tag.displayName) {
  53. return tag
  54. }
  55. tag.name = tag.displayName
  56. delete tag.displayName
  57. return tag
  58. }