string.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * A branded type representing a string that is guaranteed to be non-empty (length > 0).
  3. * This type allows distinguishing non-empty strings from regular strings at compile time.
  4. */
  5. export type NonEmptyString = string & { readonly __brand: unique symbol };
  6. /**
  7. * Checks if a value is a non-empty string.
  8. * @param value - The value to check
  9. * @returns True if the value is a string with length > 0, false otherwise
  10. */
  11. export const isNonEmptyString = (
  12. value: string | null | undefined,
  13. ): value is NonEmptyString => {
  14. return value != null && value.length > 0;
  15. };
  16. /**
  17. * Converts a string to NonEmptyString type.
  18. * @param value - The string to convert
  19. * @returns The string as NonEmptyString type
  20. * @throws Error if the value is null, undefined, or empty string
  21. */
  22. export const toNonEmptyString = (value: string): NonEmptyString => {
  23. // throw Error if the value is null, undefined or empty
  24. if (!isNonEmptyString(value))
  25. throw new Error(`Expected a non-empty string, but received: ${value}`);
  26. return value;
  27. };
  28. /**
  29. * Converts a string to NonEmptyString type or returns undefined.
  30. * @param value - The string to convert
  31. * @returns The string as NonEmptyString type, or undefined if the value is null, undefined, or empty
  32. */
  33. export const toNonEmptyStringOrUndefined = (
  34. value: string | null | undefined,
  35. ): NonEmptyString | undefined => {
  36. // return undefined if the value is null, undefined or empty
  37. if (!isNonEmptyString(value)) return undefined;
  38. return value;
  39. };
  40. /**
  41. * A branded type representing a string that is guaranteed to be non-blank.
  42. * A non-blank string contains at least one non-whitespace character.
  43. * This type allows distinguishing non-blank strings from regular strings at compile time.
  44. */
  45. export type NonBlankString = string & { readonly __brand: unique symbol };
  46. /**
  47. * Checks if a value is a non-blank string.
  48. * A non-blank string is a string that contains at least one non-whitespace character.
  49. * @param value - The value to check
  50. * @returns True if the value is a string with trimmed length > 0, false otherwise
  51. */
  52. export const isNonBlankString = (
  53. value: string | null | undefined,
  54. ): value is NonBlankString => {
  55. return value != null && value.trim().length > 0;
  56. };
  57. /**
  58. * Converts a string to NonBlankString type.
  59. * @param value - The string to convert
  60. * @returns The string as NonBlankString type
  61. * @throws Error if the value is null, undefined, empty string, or contains only whitespace characters
  62. */
  63. export const toNonBlankString = (value: string): NonBlankString => {
  64. // throw Error if the value is null, undefined or empty
  65. if (!isNonBlankString(value))
  66. throw new Error(`Expected a non-blank string, but received: ${value}`);
  67. return value;
  68. };
  69. /**
  70. * Converts a string to NonBlankString type or returns undefined.
  71. * @param value - The string to convert
  72. * @returns The string as NonBlankString type, or undefined if the value is null, undefined, empty, or contains only whitespace characters
  73. */
  74. export const toNonBlankStringOrUndefined = (
  75. value: string | null | undefined,
  76. ): NonBlankString | undefined => {
  77. // return undefined if the value is null, undefined or blank (empty or whitespace only)
  78. if (!isNonBlankString(value)) return undefined;
  79. return value;
  80. };