Przeglądaj źródła

add toNonEmptyString and toNonBlankString

Yuki Takei 10 miesięcy temu
rodzic
commit
08c1d7d126

+ 58 - 0
packages/core/src/interfaces/primitive/string.spec.ts

@@ -2,8 +2,10 @@ import { describe, expect, it } from 'vitest';
 
 
 import {
 import {
   isNonEmptyString,
   isNonEmptyString,
+  toNonEmptyString,
   toNonEmptyStringOrUndefined,
   toNonEmptyStringOrUndefined,
   isNonBlankString,
   isNonBlankString,
+  toNonBlankString,
   toNonBlankStringOrUndefined,
   toNonBlankStringOrUndefined,
 } from './string';
 } from './string';
 
 
@@ -87,6 +89,62 @@ describe('toNonBlankStringOrUndefined', () => {
   });
   });
 });
 });
 
 
+describe('toNonEmptyString', () => {
+  /* eslint-disable indent */
+  it.each`
+    input         | expected      | description
+    ${'hello'}    | ${'hello'}    | ${'non-empty string'}
+    ${'world'}    | ${'world'}    | ${'non-empty string'}
+    ${'a'}        | ${'a'}        | ${'single character'}
+    ${'1'}        | ${'1'}        | ${'numeric string'}
+    ${' '}        | ${' '}        | ${'space character'}
+    ${'   '}      | ${'   '}      | ${'multiple spaces'}
+  `('should return $expected for valid $description: $input', ({ input, expected }) => {
+  /* eslint-enable indent */
+    expect(toNonEmptyString(input)).toBe(expected);
+  });
+
+  /* eslint-disable indent */
+  it.each`
+    input         | description
+    ${''}         | ${'empty string'}
+    ${null}       | ${'null'}
+    ${undefined}  | ${'undefined'}
+  `('should throw error for invalid $description: $input', ({ input }) => {
+  /* eslint-enable indent */
+    expect(() => toNonEmptyString(input)).toThrow('Expected a non-empty string, but received:');
+  });
+});
+
+describe('toNonBlankString', () => {
+  /* eslint-disable indent */
+  it.each`
+    input         | expected      | description
+    ${'hello'}    | ${'hello'}    | ${'non-blank string'}
+    ${'world'}    | ${'world'}    | ${'non-blank string'}
+    ${'a'}        | ${'a'}        | ${'single character'}
+    ${'1'}        | ${'1'}        | ${'numeric string'}
+  `('should return $expected for valid $description: $input', ({ input, expected }) => {
+  /* eslint-enable indent */
+    expect(toNonBlankString(input)).toBe(expected);
+  });
+
+  /* eslint-disable indent */
+  it.each`
+    input         | description
+    ${' '}        | ${'space character'}
+    ${'   '}      | ${'multiple spaces'}
+    ${'\t'}       | ${'tab character'}
+    ${'\n'}       | ${'newline character'}
+    ${''}         | ${'empty string'}
+    ${null}       | ${'null'}
+    ${undefined}  | ${'undefined'}
+  `('should throw error for invalid $description: $input', ({ input }) => {
+  /* eslint-enable indent */
+    expect(() => toNonBlankString(input)).toThrow('Expected a non-blank string, but received:');
+  });
+});
+
 describe('type safety', () => {
 describe('type safety', () => {
   it('should maintain type safety with branded types', () => {
   it('should maintain type safety with branded types', () => {
     const validString = 'test';
     const validString = 'test';

+ 16 - 3
packages/core/src/interfaces/primitive/string.ts

@@ -1,12 +1,13 @@
 export type NonEmptyString = string & { readonly __brand: unique symbol };
 export type NonEmptyString = string & { readonly __brand: unique symbol };
-export type NonBlankString = string & { readonly __brand: unique symbol };
 
 
 export const isNonEmptyString = (value: string | null | undefined): value is NonEmptyString => {
 export const isNonEmptyString = (value: string | null | undefined): value is NonEmptyString => {
   return value != null && value.length > 0;
   return value != null && value.length > 0;
 };
 };
 
 
-export const isNonBlankString = (value: string | null | undefined): value is NonBlankString => {
-  return value != null && value.trim().length > 0;
+export const toNonEmptyString = (value: string): NonEmptyString => {
+  // throw Error if the value is null, undefined or empty
+  if (!isNonEmptyString(value)) throw new Error(`Expected a non-empty string, but received: ${value}`);
+  return value;
 };
 };
 
 
 export const toNonEmptyStringOrUndefined = (value: string | null | undefined): NonEmptyString | undefined => {
 export const toNonEmptyStringOrUndefined = (value: string | null | undefined): NonEmptyString | undefined => {
@@ -15,6 +16,18 @@ export const toNonEmptyStringOrUndefined = (value: string | null | undefined): N
   return value;
   return value;
 };
 };
 
 
+export type NonBlankString = string & { readonly __brand: unique symbol };
+
+export const isNonBlankString = (value: string | null | undefined): value is NonBlankString => {
+  return value != null && value.trim().length > 0;
+};
+
+export const toNonBlankString = (value: string): NonBlankString => {
+  // throw Error if the value is null, undefined or empty
+  if (!isNonBlankString(value)) throw new Error(`Expected a non-blank string, but received: ${value}`);
+  return value;
+};
+
 export const toNonBlankStringOrUndefined = (value: string | null | undefined): NonBlankString | undefined => {
 export const toNonBlankStringOrUndefined = (value: string | null | undefined): NonBlankString | undefined => {
   // return undefined if the value is null, undefined or blank (empty or whitespace only)
   // return undefined if the value is null, undefined or blank (empty or whitespace only)
   if (!isNonBlankString(value)) return undefined;
   if (!isNonBlankString(value)) return undefined;