import React from 'react'
import { type Meta, type StoryObj } from '@storybook/react-vite'
import { compareArrays } from '../../services/FilterHelperServiceTyped'
import { DocsTemplate } from '../../../.storybook'
const CompareArraysDisplay = ({ first, second, label }) => {
const result = compareArrays(first, second)
return (
{label}
First Array: {JSON.stringify(first)}
Second Array: {JSON.stringify(second)}
Result: {result ? 'True ✓' : 'False ✗'}
)
}
export default {
title: 'Helper Functions/compareArrays',
component: CompareArraysDisplay,
parameters: {
viewMode: 'docs',
previewTabs: {
canvas: { hidden: true },
},
docs: {
page: () => (
<>
Core features:
-
Checks if two arrays contain exactly the same elements
- Order of elements does not matter for equality check
- Safely handles null or undefined values
,
Implementation details:
-
Uses
every() and includes(){' '}
methods to check bidirectional inclusion
-
Validates that every element in the first array exists in
the second and vice versa
,
Common use cases:
- Comparing filter selections
- Detecting changes in multiselect inputs
- Validating array equality in form submissions
-
Checking if filter states have changed from initial values
,
Best practices:
-
Use for content equality checks when order doesn't matter
-
Consider performance implications with very large arrays
-
Combine with other filter helpers for comprehensive state
management
,
]}
/>
>
),
source: {
code: false,
},
},
},
argTypes: {
label: {
control: 'text',
description: 'Label for the comparison',
},
first: {
control: 'object',
description: 'First array to compare',
},
second: {
control: 'object',
description: 'Second array to compare',
},
},
} as Meta
type Story = StoryObj
const Template: Story = {
render: (args) => {
return
},
}
export const SameElementsDifferentOrder: Story = {
...Template,
args: {
label: 'Same Elements Different Order',
first: [1, 2, 3, 4],
second: [4, 3, 2, 1],
},
}
export const DifferentElements: Story = {
...Template,
args: {
label: 'Different Elements',
first: [1, 2, 3],
second: [1, 2, 4],
},
}
export const EmptyArrays: Story = {
...Template,
args: {
label: 'Empty Arrays',
first: [],
second: [],
},
}
export const NullFirstArray: Story = {
...Template,
args: {
label: 'Null First Array',
first: null,
second: [1, 2, 3],
},
}
export const StringArrays: Story = {
...Template,
args: {
label: 'String Arrays',
first: ['apple', 'banana', 'cherry'],
second: ['cherry', 'banana', 'apple'],
},
}