string.ts 3.2 KB

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