Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isArray function #8

Merged
merged 2 commits into from
Nov 30, 2023
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
15 changes: 6 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions packages/spacecat-shared-utils/src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
const REGEX_ISO_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/;
const REGEX_TIME_OFFSET_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}(Z|[+-]\d{2}:\d{2})/;

/**
* Determines if the given parameter is an array.
*
* @param {*} value - The value to check.
* @returns {boolean} True if the parameter is an array, false otherwise.
*/
function isArray(value) {
return Array.isArray(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the roundtrip through the utils for this? Are we planning any additional checks in the future?

Also we could reuse the util function in isObject and arrayEquals

}

/**
* Determines case-insensitively if the given value is a boolean or a string
* representation of a boolean.
Expand Down Expand Up @@ -53,7 +63,7 @@ function isNumber(value) {
* @returns {boolean} True if the parameter is an object, false otherwise.
*/
function isObject(value) {
return !Array.isArray(value) && value !== null && typeof value === 'object';
return !isArray(value) && value !== null && typeof value === 'object';
}

/**
Expand Down Expand Up @@ -150,14 +160,15 @@ function toBoolean(value) {
* @param {Array} b - The second array to compare.
* @returns {boolean} True if the arrays are equal, false otherwise.
*/
const arrayEquals = (a, b) => Array.isArray(a)
&& Array.isArray(b)
const arrayEquals = (a, b) => isArray(a)
&& isArray(b)
&& a.length === b.length
&& a.every((val, index) => val === b[index]);

export {
arrayEquals,
hasText,
isArray,
isBoolean,
isInteger,
isValidDate,
Expand Down
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
export {
arrayEquals,
hasText,
isArray,
isBoolean,
isInteger,
isValidDate,
Expand Down
19 changes: 19 additions & 0 deletions packages/spacecat-shared-utils/test/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { expect } from 'chai';
import {
hasText,
isBoolean,
isArray,
isInteger,
isValidDate,
isIsoDate,
Expand Down Expand Up @@ -81,6 +82,24 @@ describe('Shared functions', () => {
expect(hasText('a12dsamklda')).to.be.true;
});

it('is array', () => {
const invalidArrays = [
true,
{},
{ asd: 'dsa' },
'',
'dasd',
NaN,
Infinity,
-Infinity,
123,
];

invalidArrays.forEach((value) => expect(isArray(value)).to.be.false);
expect(isArray([])).to.be.true;
expect(isArray(['abc'])).to.be.true;
});

it('is boolean', () => {
const invalidBooleans = [
null,
Expand Down
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Index Exports', () => {
const expectedExports = [
'arrayEquals',
'hasText',
'isArray',
'isBoolean',
'isInteger',
'isValidDate',
Expand Down
Loading