Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ exports[`existence of exported functions 1`] = `
"xGetTargetIndex",
"xHilbertTransform",
"xHistogram",
"xIsArray",
"xIsEquallySpaced",
"xIsMonotonic",
"xMassCenterVectorSimilarity",
Expand Down
10 changes: 8 additions & 2 deletions src/x/__tests__/xCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import { expect, test } from 'vitest';
import { xCheck } from '../xCheck.ts';

test('should throw on invalid value', () => {
expect(() => xCheck()).toThrow(/input must be an array/);
expect(() => xCheck(undefined)).toThrow(/input must be an array/);
expect(() => xCheck([])).toThrow(/input must not be empty/);
expect(() => xCheck([1])).not.toThrow();
expect(() => xCheck(['yo', 1])).toThrow(/input must contain numbers/);
expect(() => xCheck([1], { minLength: 2 })).toThrow(
/input must have a length of at least 2/,
);
});

test('should not throw on valid value', () => {
expect(() => xCheck([1])).not.toThrow();
expect(() => xCheck([1, 2], { minLength: 2 })).not.toThrow();
expect(() => xCheck([1, 2, 3], { minLength: 2 })).not.toThrow();
});
16 changes: 16 additions & 0 deletions src/x/__tests__/xIsArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from 'vitest';

import { xIsArray } from '../xIsArray.ts';

test('should return false on invalid value', () => {
expect(xIsArray(undefined)).toBe(false);
expect(xIsArray([])).toBe(false);
expect(xIsArray(['yo', 1])).toBe(false);
expect(xIsArray([1], { minLength: 2 })).toBe(false);
});

test('should return true on valid value', () => {
expect(xIsArray([1])).toBe(true);
expect(xIsArray([1, 2], { minLength: 2 })).toBe(true);
expect(xIsArray([1, 2, 3], { minLength: 2 })).toBe(true);
});
1 change: 1 addition & 0 deletions src/x/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from './xGetFromToIndex.ts';
export * from './xGetTargetIndex.ts';
export * from './xHilbertTransform.ts';
export * from './xHistogram.ts';
export * from './xIsArray.ts';
export * from './xIsEquallySpaced.ts';
export * from './xIsMonotonic.ts';
export * from './xMassCenterVectorSimilarity.ts';
Expand Down
9 changes: 5 additions & 4 deletions src/x/xCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { isAnyArray } from 'is-any-array';

export interface XCheckOptions {
/**
* Minimum length
* Minimum length.
* Must be at least 1 because the first element is always checked.
* @default 1
*/
minLength?: number;
Expand All @@ -16,10 +17,10 @@ export interface XCheckOptions {
* @param options - additional checks.
*/
export function xCheck(
input?: NumberArray,
input: unknown,
options: XCheckOptions = {},
): asserts input is NumberArray {
const { minLength } = options;
const { minLength = 1 } = options;
if (!isAnyArray(input)) {
throw new TypeError('input must be an array');
}
Expand All @@ -29,7 +30,7 @@ export function xCheck(
if (typeof input[0] !== 'number') {
throw new TypeError('input must contain numbers');
}
if (minLength && input.length < minLength) {
if (input.length < minLength) {
throw new Error(`input must have a length of at least ${minLength}`);
}
}
31 changes: 31 additions & 0 deletions src/x/xIsArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { NumberArray } from 'cheminfo-types';
import { isAnyArray } from 'is-any-array';

export interface XIsArrayOptions {
/**
* Minimum length.
* Must be at least 1 because the first element is always checked.
* @default 1
*/
minLength?: number;
}

/**
* Checks if the input is a non-empty array of numbers.
* Only checks the first element.
* @param input - array to check.
* @param options - additional checks.
* @returns Whether the value is a valid array of numbers.
*/
export function xIsArray(
input: unknown,
options: XIsArrayOptions = {},
): input is NumberArray {
const { minLength = 1 } = options;
return (
isAnyArray(input) &&
input.length > 0 &&
typeof input[0] === 'number' &&
input.length >= minLength
);
}
Loading