index.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {
  2. FLAG,
  3. getEmptyResult,
  4. IOptions,
  5. IResult,
  6. parseImpl,
  7. resetResult,
  8. } from 'tldts-core';
  9. import suffixLookup from './src/suffix-trie';
  10. // For all methods but 'parse', it does not make sense to allocate an object
  11. // every single time to only return the value of a specific attribute. To avoid
  12. // this un-necessary allocation, we use a global object which is re-used.
  13. const RESULT: IResult = getEmptyResult();
  14. export function parse(url: string, options: Partial<IOptions> = {}): IResult {
  15. return parseImpl(url, FLAG.ALL, suffixLookup, options, getEmptyResult());
  16. }
  17. export function getHostname(
  18. url: string,
  19. options: Partial<IOptions> = {},
  20. ): string | null {
  21. /*@__INLINE__*/ resetResult(RESULT);
  22. return parseImpl(url, FLAG.HOSTNAME, suffixLookup, options, RESULT).hostname;
  23. }
  24. export function getPublicSuffix(
  25. url: string,
  26. options: Partial<IOptions> = {},
  27. ): string | null {
  28. /*@__INLINE__*/ resetResult(RESULT);
  29. return parseImpl(url, FLAG.PUBLIC_SUFFIX, suffixLookup, options, RESULT)
  30. .publicSuffix;
  31. }
  32. export function getDomain(
  33. url: string,
  34. options: Partial<IOptions> = {},
  35. ): string | null {
  36. /*@__INLINE__*/ resetResult(RESULT);
  37. return parseImpl(url, FLAG.DOMAIN, suffixLookup, options, RESULT).domain;
  38. }
  39. export function getSubdomain(
  40. url: string,
  41. options: Partial<IOptions> = {},
  42. ): string | null {
  43. /*@__INLINE__*/ resetResult(RESULT);
  44. return parseImpl(url, FLAG.SUB_DOMAIN, suffixLookup, options, RESULT)
  45. .subdomain;
  46. }
  47. export function getDomainWithoutSuffix(
  48. url: string,
  49. options: Partial<IOptions> = {},
  50. ): string | null {
  51. /*@__INLINE__*/ resetResult(RESULT);
  52. return parseImpl(url, FLAG.ALL, suffixLookup, options, RESULT)
  53. .domainWithoutSuffix;
  54. }