@mmb-digital/shared-libs-auto 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -83,6 +83,10 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
83
83
 
84
84
  ### Utils
85
85
 
86
+ #### Assertions
87
+ - **`assertValue(value: boolean, message?: string): asserts value`** / **`assertValue<T>(value: T | null | undefined, message?: string): asserts value is T`** - Throws an `Error` with the optional message if the value is `false`, `null`, or `undefined`; accepts other falsy values such as `0` and `''`.
88
+ - **`assertNonNullish<Value>(value: Value, message: string): asserts value is NonNullable<Value>`** - Throws an `Error` with the supplied message if the value is `null` or `undefined`; accepts `false` and other non-nullish values.
89
+
86
90
  #### Array Operations
87
91
  - **`chunkArray<T>(input: unknown, size: number): T[][]`** - Splits an array into chunks of specified size.
88
92
  - **`groupBy<T, K extends string | number | symbol>(arr: T[], getKey: (item: T) => K): Record<K, T[] | undefined>`** - Groups array elements by a specified key.
@@ -115,7 +119,7 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
115
119
  - **`getSafeMin(a: number | null | undefined, b: number | null | undefined): number | undefined`** - Returns min of two numbers with `null`/`undefined` fallback handling.
116
120
 
117
121
  #### Object Operations
118
- - **`cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): CleanedObject<T>`** - Deeply removes undefined, null, empty string values, empty array and empty objects from an object.
122
+ - **`cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): Partial<T>`** - Deeply removes undefined, null, empty string values, empty array and empty objects from an object.
119
123
  - **`getObjectDiff<T extends Record<string, unknown>, R extends Record<string, unknown>>(object: T | undefined | null, base: R | undefined | null): Partial<T>`** - Returns differences between two objects.
120
124
  - **`getObjectValue(obj: Record<string, unknown> | null | undefined, path: string | string[], defaultValue?: unknown): unknown`** - Gets nested object value by path string (e.g., 'user.address.city').
121
125
  - **`setObjectValue<T extends Record<string, unknown>>(obj: T, path: string, value: unknown): T`** - Sets nested object value by path string.
@@ -194,6 +198,7 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
194
198
  - **`isValidMrzCheckDigit(value: string | number | null | undefined, checkDigit: string | number | null | undefined): boolean`** - Validates MRZ check digit.
195
199
 
196
200
  #### Yup Validators
201
+ - **`yupNumberEmptyTransform(value: number, originalValue: unknown): number | undefined`** - Transforms an empty string to `undefined` so Yup `required()` or `default()` can handle empty number inputs without a `NaN` cast error.
197
202
  - **`isBirthNumberYupTest(message: string)`** - Creates Yup `test` config for Czech birth number validation.
198
203
  - **`isDateSameOrAfterYupWhen(message: string)(maxDate: unknown[], schema: StringSchema)`** - Creates Yup `when` condition for reference date being same or after a date.
199
204
  - **`isDateSameOrBeforeYupWhen(message: string)(minDate: unknown[], schema: StringSchema)`** - Creates Yup `when` condition for reference date being same or before a date.
@@ -1,11 +1,12 @@
1
1
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { act, renderHook } from '@testing-library/react-hooks';
2
+ import { act, cleanup, renderHook } from '@testing-library/react';
3
3
  import { useDebounce } from './useDebounce.js';
4
4
  describe('useDebounce', () => {
5
5
  beforeEach(() => {
6
6
  vi.useFakeTimers();
7
7
  });
8
8
  afterEach(() => {
9
+ cleanup();
9
10
  vi.useRealTimers();
10
11
  });
11
12
  it('returns the initial value immediately', () => {
@@ -5,7 +5,7 @@ import { isEqual } from '../../validators/isEqual/isEqual.js';
5
5
  * Useful for comparing previous and current values in functional components.
6
6
  */
7
7
  export const usePreviousValue = (value) => {
8
- const ref = useRef();
8
+ const ref = useRef(undefined);
9
9
  useEffect(() => {
10
10
  if (!isEqual(value, ref.current)) {
11
11
  ref.current = value;
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'vitest';
2
- import { renderHook } from '@testing-library/react-hooks';
2
+ import { renderHook } from '@testing-library/react';
3
3
  import { usePreviousValue } from './usePreviousValue.js';
4
4
  describe('usePreviousValue', () => {
5
5
  test('should return undefined on first render', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'vitest';
2
- import { renderHook } from '@testing-library/react-hooks';
2
+ import { act, renderHook } from '@testing-library/react';
3
3
  import { useToggle } from './useToggle.js';
4
4
  describe('useToggle', () => {
5
5
  test('should initialize with defaultState = false', () => {
@@ -18,23 +18,35 @@ describe('useToggle', () => {
18
18
  });
19
19
  test('should toggle state', () => {
20
20
  const { result } = renderHook(() => useToggle());
21
- result.current[1]();
21
+ act(() => {
22
+ result.current[1]();
23
+ });
22
24
  expect(result.current[0]).toBe(true);
23
- result.current[1]();
25
+ act(() => {
26
+ result.current[1]();
27
+ });
24
28
  expect(result.current[0]).toBe(false);
25
29
  });
26
30
  test('should open and close', () => {
27
31
  const { result } = renderHook(() => useToggle());
28
- result.current[2].open();
32
+ act(() => {
33
+ result.current[2].open();
34
+ });
29
35
  expect(result.current[0]).toBe(true);
30
- result.current[2].close();
36
+ act(() => {
37
+ result.current[2].close();
38
+ });
31
39
  expect(result.current[0]).toBe(false);
32
40
  });
33
41
  test('should set state directly', () => {
34
42
  const { result } = renderHook(() => useToggle());
35
- result.current[3](true);
43
+ act(() => {
44
+ result.current[3](true);
45
+ });
36
46
  expect(result.current[0]).toBe(true);
37
- result.current[3](false);
47
+ act(() => {
48
+ result.current[3](false);
49
+ });
38
50
  expect(result.current[0]).toBe(false);
39
51
  });
40
52
  });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Asserts that a value is not nullish (null or undefined).
3
+ */
4
+ export declare function assertNonNullish<Value>(value: Value, message: string): asserts value is NonNullable<Value>;
@@ -0,0 +1,10 @@
1
+ import { isNull } from '../../validators/isNull/isNull.js';
2
+ import { isUndefined } from '../../validators/isUndefined/isUndefined.js';
3
+ /**
4
+ * Asserts that a value is not nullish (null or undefined).
5
+ */
6
+ export function assertNonNullish(value, message) {
7
+ if (isUndefined(value) || isNull(value)) {
8
+ throw new Error(message);
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import { describe, expect, expectTypeOf, it } from 'vitest';
2
+ import { assertNonNullish } from './assertNonNullish.js';
3
+ describe('assertNonNullish', () => {
4
+ it.each([null, undefined])('throws the supplied message for %s', value => {
5
+ expect(() => assertNonNullish(value, 'Value is required')).toThrow('Value is required');
6
+ });
7
+ it.each([false, true, 0, '', Number.NaN, 0n, [], {}])('accepts %s', value => {
8
+ expect(() => assertNonNullish(value, 'Value is required')).not.toThrow();
9
+ });
10
+ it('narrows nullable values while preserving false', () => {
11
+ const useValue = (value) => {
12
+ assertNonNullish(value, 'Value is required');
13
+ expectTypeOf(value).toEqualTypeOf();
14
+ return value;
15
+ };
16
+ expect(useValue(false)).toBe(false);
17
+ expect(useValue('value')).toBe('value');
18
+ });
19
+ });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Asserts that a value is not nullish (null or undefined) or false.
3
+ */
4
+ export declare function assertValue(value: boolean, message?: string): asserts value;
5
+ export declare function assertValue<T>(value: T | null | undefined, message?: string): asserts value is T;
@@ -0,0 +1,7 @@
1
+ import { isNull } from '../../validators/isNull/isNull.js';
2
+ import { isUndefined } from '../../validators/isUndefined/isUndefined.js';
3
+ export function assertValue(value, message) {
4
+ if (value === false || isNull(value) || isUndefined(value)) {
5
+ throw new Error(message);
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { describe, expect, expectTypeOf, it } from 'vitest';
2
+ import { assertValue } from './assertValue.js';
3
+ describe('assertValue', () => {
4
+ it.each([null, undefined, false])('throws the supplied message for %s', value => {
5
+ expect(() => assertValue(value, 'Value is required')).toThrow('Value is required');
6
+ });
7
+ it('throws an error without a message when none is supplied', () => {
8
+ expect(() => assertValue(false)).toThrow(new Error());
9
+ });
10
+ it.each([true, 0, '', Number.NaN, 0n, [], {}])('accepts %s', value => {
11
+ expect(() => assertValue(value)).not.toThrow();
12
+ });
13
+ it('narrows nullable values and boolean conditions', () => {
14
+ const useValue = (value) => {
15
+ assertValue(value);
16
+ expectTypeOf(value).toEqualTypeOf();
17
+ return value.toUpperCase();
18
+ };
19
+ const checkCondition = (condition) => {
20
+ assertValue(condition);
21
+ expectTypeOf(condition).toEqualTypeOf();
22
+ return condition;
23
+ };
24
+ expect(useValue('value')).toBe('VALUE');
25
+ expect(checkCondition(true)).toBe(true);
26
+ });
27
+ });
@@ -1,5 +1,4 @@
1
- import type { CleanedObject } from './types.js';
2
1
  /**
3
2
  * Deeply removes undefined, null, empty string values, empty array and empty objects from an object.
4
3
  */
5
- export declare function cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): CleanedObject<T>;
4
+ export declare function cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): Partial<T>;
@@ -1,6 +1,8 @@
1
1
  export type { Gender } from './birthNumber/types.js';
2
2
  export type { Range } from './range/types.js';
3
3
  export { arrayBufferToBase64 } from './arrayBufferToBase64/arrayBufferToBase64.js';
4
+ export { assertNonNullish } from './assertions/assertNonNullish.js';
5
+ export { assertValue } from './assertions/assertValue.js';
4
6
  export { base64ToArrayBuffer } from './base64ToArrayBuffer/base64ToArrayBuffer.js';
5
7
  export { blobToBase64 } from './blobToBase64/blobToBase64.js';
6
8
  export { chunkArray } from './chunkArray/chunkArray.js';
@@ -1,4 +1,6 @@
1
1
  export { arrayBufferToBase64 } from './arrayBufferToBase64/arrayBufferToBase64.js';
2
+ export { assertNonNullish } from './assertions/assertNonNullish.js';
3
+ export { assertValue } from './assertions/assertValue.js';
2
4
  export { base64ToArrayBuffer } from './base64ToArrayBuffer/base64ToArrayBuffer.js';
3
5
  export { blobToBase64 } from './blobToBase64/blobToBase64.js';
4
6
  export { chunkArray } from './chunkArray/chunkArray.js';
@@ -25,7 +25,7 @@ describe('openFile', () => {
25
25
  const base64 = 'ZmFrZQ=='; // "fake"
26
26
  const arrayBuffer = new Uint8Array([102, 97, 107, 101]).buffer;
27
27
  openFile(base64);
28
- expect(URL.createObjectURL).toHaveBeenCalledWith(new Blob([arrayBuffer]));
28
+ expect(URL.createObjectURL).toHaveBeenCalledWith(new Blob([new Uint8Array(arrayBuffer)]));
29
29
  expect(globalThis.window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
30
30
  });
31
31
  it('should open ArrayBuffer in a new tab', () => {
@@ -1,4 +1,4 @@
1
- import { describe, expect, it, vi } from 'vitest';
1
+ import { describe, expect, it } from 'vitest';
2
2
  import { isValidDate } from './isValidDate.js';
3
3
  describe('Should validate date', () => {
4
4
  it('Should validate datetime', () => {
@@ -8,16 +8,6 @@ describe('Should validate date', () => {
8
8
  expect(isValidDate('05/21/2025')).toStrictEqual(true);
9
9
  expect(isValidDate('2025-09-03T13:32:21.582Z')).toStrictEqual(true);
10
10
  });
11
- it('Validates date-only ISO strings in a negative UTC offset', () => {
12
- vi.stubEnv('TZ', 'America/New_York');
13
- try {
14
- expect(new Date('2025-05-21').getDate()).toBe(20);
15
- expect(isValidDate('2025-05-21')).toBe(true);
16
- }
17
- finally {
18
- vi.unstubAllEnvs();
19
- }
20
- });
21
11
  });
22
12
  describe('Invalid values', () => {
23
13
  it('Should reject impossible calendar dates', () => {
@@ -11,3 +11,4 @@ export { isNumberSameOrLowerYupWhen } from './isNumberSameOrLower/isNumberSameOr
11
11
  export { isDateAtLeastYearsAgoYupTest } from './isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.js';
12
12
  export { maxDigitsLengthYupTest } from './maxDigitsLength/maxDigitsLengthYupTest.js';
13
13
  export { minDigitsLengthYupTest } from './minDigitsLength/minDigitsLengthYupTest.js';
14
+ export { yupNumberEmptyTransform } from './yupNumberEmptyTransform/yupNumberEmptyTransform.js';
package/dist/yup/index.js CHANGED
@@ -11,3 +11,4 @@ export { isNumberSameOrLowerYupWhen } from './isNumberSameOrLower/isNumberSameOr
11
11
  export { isDateAtLeastYearsAgoYupTest } from './isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.js';
12
12
  export { maxDigitsLengthYupTest } from './maxDigitsLength/maxDigitsLengthYupTest.js';
13
13
  export { minDigitsLengthYupTest } from './minDigitsLength/minDigitsLengthYupTest.js';
14
+ export { yupNumberEmptyTransform } from './yupNumberEmptyTransform/yupNumberEmptyTransform.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Yup `number()` transform that turns an empty string into `undefined`,
3
+ * so empty inputs are reported by `required()` (or fall back to `default()`)
4
+ * instead of failing with a `NaN` cast type error.
5
+ */
6
+ export declare const yupNumberEmptyTransform: (value: number, originalValue: unknown) => number | undefined;
@@ -0,0 +1,7 @@
1
+ import { isEmptyString } from '../../validators/isEmptyString/isEmptyString.js';
2
+ /**
3
+ * Yup `number()` transform that turns an empty string into `undefined`,
4
+ * so empty inputs are reported by `required()` (or fall back to `default()`)
5
+ * instead of failing with a `NaN` cast type error.
6
+ */
7
+ export const yupNumberEmptyTransform = (value, originalValue) => (isEmptyString(originalValue) ? undefined : value);
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { number } from 'yup';
3
+ import { yupNumberEmptyTransform } from './yupNumberEmptyTransform.js';
4
+ describe('yupNumberEmptyTransform', () => {
5
+ const requiredSchema = number().transform(yupNumberEmptyTransform).required('Required');
6
+ it.each([null, undefined, ''])('reports required error for empty value %s', async (value) => {
7
+ const error = await requiredSchema.validate(value).catch((e) => e);
8
+ expect(error).toMatchObject({ message: 'Required' });
9
+ expect(error).not.toMatchObject({ type: 'typeError' });
10
+ });
11
+ it('falls back to default for an empty string on a nullable schema', () => {
12
+ const schema = number().transform(yupNumberEmptyTransform).nullable().default(null);
13
+ expect(schema.validateSync('')).toBeNull();
14
+ });
15
+ it.each([
16
+ [0, 0],
17
+ ['42', 42]
18
+ ])('keeps numeric value %s', async (value, expected) => {
19
+ await expect(requiredSchema.validate(value)).resolves.toBe(expected);
20
+ });
21
+ it('still rejects non-numeric strings with a type error', async () => {
22
+ await expect(requiredSchema.validate('abc')).rejects.toMatchObject({ type: 'typeError' });
23
+ });
24
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmb-digital/shared-libs-auto",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "MMB shared libs",
5
5
  "private": false,
6
6
  "license": "UNLICENSED",
@@ -32,42 +32,40 @@
32
32
  "not ie > 0",
33
33
  "not ie_mob > 0"
34
34
  ],
35
- "dependencies": {
36
- "date-fns": "4.4.0",
37
- "react": "17.0.2",
38
- "react-dom": "17.0.2",
39
- "yup": "1.7.1"
40
- },
41
35
  "devDependencies": {
42
36
  "@eslint/js": "10.0.1",
43
- "@testing-library/react-hooks": "8.0.1",
37
+ "@testing-library/dom": "10.4.2",
38
+ "@testing-library/react": "16.3.3",
44
39
  "@types/node": "22.17.0",
45
- "@types/react": "17.0.2",
46
- "@types/react-dom": "17.0.2",
47
- "@vitest/coverage-v8": "4.1.11",
48
- "eslint": "10.11.0",
40
+ "@types/react": "18.3.1",
41
+ "@types/react-dom": "18.3.1",
42
+ "@vitest/coverage-v8": "5.0.5",
43
+ "date-fns": "4.4.0",
44
+ "eslint": "10.13.0",
49
45
  "eslint-config-prettier": "10.1.8",
50
- "eslint-plugin-prettier": "5.5.6",
46
+ "eslint-plugin-prettier": "5.5.7",
51
47
  "eslint-plugin-react": "7.37.5",
52
48
  "eslint-plugin-react-hooks": "7.1.1",
53
49
  "eslint-plugin-simple-import-sort": "14.0.0",
54
50
  "eslint-plugin-unused-imports": "4.4.1",
55
- "globals": "17.11.0",
56
- "happy-dom": "20.14.5",
51
+ "globals": "17.13.0",
52
+ "happy-dom": "21.0.4",
57
53
  "husky": "9.1.7",
58
- "knip": "6.32.3",
59
- "prettier": "3.9.9",
60
- "react-test-renderer": "17.0.2",
54
+ "knip": "6.43.0",
55
+ "prettier": "3.9.10",
56
+ "react": "18.3.1",
57
+ "react-dom": "18.3.1",
61
58
  "rimraf": "6.1.3",
62
59
  "typescript": "6.0.3",
63
- "typescript-eslint": "8.71.0",
64
- "vite": "8.3.1",
65
- "vitest": "4.1.11"
60
+ "typescript-eslint": "8.72.1",
61
+ "vite": "8.3.5",
62
+ "vitest": "5.0.5",
63
+ "yup": "1.7.1"
66
64
  },
67
65
  "peerDependencies": {
68
66
  "date-fns": ">=4",
69
- "react": ">=17",
70
- "react-dom": ">=17",
67
+ "react": ">=18",
68
+ "react-dom": ">=18",
71
69
  "yup": ">=1"
72
70
  },
73
71
  "packageManager": "yarn@1.22.22"