|
1 | 1 | import { DEFAULT_INVALID_TEXT } from '@ifrc-go/ui/utils'; |
2 | | -import { isTruthyString } from '@togglecorp/fujs'; |
| 2 | +import { |
| 3 | + isDefined, |
| 4 | + isNotDefined, |
| 5 | + isTruthyString, |
| 6 | +} from '@togglecorp/fujs'; |
3 | 7 |
|
4 | 8 | import type { GoApiResponse } from '#utils/restRequest'; |
5 | 9 |
|
@@ -52,3 +56,40 @@ export function getFirstTruthyString( |
52 | 56 |
|
53 | 57 | return invalidText; |
54 | 58 | } |
| 59 | + |
| 60 | +// TODO: write tests for the function |
| 61 | +export function doArraysContainSameElements( |
| 62 | + newArray: unknown[] | undefined, |
| 63 | + oldArray: unknown[] | undefined, |
| 64 | +): boolean { |
| 65 | + if (isNotDefined(newArray) && isNotDefined(oldArray)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + if (isDefined(newArray) && isDefined(oldArray)) { |
| 69 | + if (newArray.length !== oldArray.length) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + return newArray.every((id) => oldArray.includes(id)); |
| 73 | + } |
| 74 | + return false; |
| 75 | +} |
| 76 | + |
| 77 | +// TODO: write tests for the function |
| 78 | +export function flattenObject<T extends Record<string, unknown>>( |
| 79 | + inputObject: T, |
| 80 | + prefix?: string, |
| 81 | +): Record<string, unknown> { |
| 82 | + return Object.entries(inputObject).reduce((acc, [key, value]) => { |
| 83 | + const newKey = prefix ? `${prefix}.${key}` : key; |
| 84 | + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { |
| 85 | + return { ...acc, ...flattenObject(value as Record<string, unknown>, newKey) }; |
| 86 | + } |
| 87 | + return { ...acc, [newKey]: value }; |
| 88 | + }, {} as Record<string, unknown>); |
| 89 | +} |
| 90 | + |
| 91 | +// TODO: write tests for the function |
| 92 | +export function getLastSegment(str: string, delimiter: string) { |
| 93 | + const parts = str.split(delimiter); |
| 94 | + return parts[parts.length - 1]; |
| 95 | +} |
0 commit comments