souce.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Source } from '../source'
  2. const isNullOrBlank = (string: string | null | undefined | number) => string == null || (string as string).length === 0 || /^\s+$/.test(string as string)
  3. const isBookSource = (source: Source) => "bookSourceName" in source
  4. export const isInvaildSource: (source: Source) => boolean = (source) => {
  5. if (isBookSource(source)) {
  6. return !isNullOrBlank(source.bookSourceName) &&
  7. !isNullOrBlank(source.bookSourceUrl) &&
  8. !isNullOrBlank(source.bookSourceType)
  9. }
  10. return !isNullOrBlank(source.sourceName) &&
  11. !isNullOrBlank(source.sourceName)
  12. }
  13. export const getSourceUniqueKey = (source: Source) => isBookSource(source) ? source.bookSourceUrl : source.sourceUrl;
  14. export const isSourceMatches: (source: Source, searchKey: string) => boolean = (source, searchKey) => {
  15. // TODO: 正则和普通字符串识别 识别 * . \ [ ] <= <! != = ?: () \d\w\s\...
  16. if (isBookSource(source)) {
  17. return (source.bookSourceName?.includes(searchKey) ||
  18. source.bookSourceUrl?.includes(searchKey) ||
  19. source.bookSourceGroup?.includes(searchKey) ||
  20. source.bookSourceComment?.includes(searchKey)) ?? false
  21. }
  22. return (source.sourceName?.includes(searchKey) ||
  23. source.sourceUrl?.includes(searchKey) ||
  24. source.sourceGroup?.includes(searchKey) ||
  25. source.sourceComment?.includes(searchKey)) ?? false
  26. }
  27. export const convertSourcesToMap = (sources: Source[]): Map<string, Source> => {
  28. const map = new Map();
  29. sources.forEach((source) =>
  30. map.set(getSourceUniqueKey(source), source)
  31. );
  32. return map;
  33. }
  34. export const emptyBookSource = {
  35. ruleSearch: {},
  36. ruleBookInfo: {},
  37. ruleToc: {},
  38. ruleContent: {},
  39. ruleReview: {},
  40. ruleExplore: {}
  41. }
  42. export const emptyRssSource = {}