Skip to content

Commit

Permalink
feat: add isArray function
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea authored Nov 30, 2023
1 parent 01798f4 commit eeb45a6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
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);
}

/**
* 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

0 comments on commit eeb45a6

Please sign in to comment.