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
39 changes: 39 additions & 0 deletions module/src/form/utils/keyChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
childKeyChainStringFromParent,
isArrayValue,
isMyKeyChainItem,
isValidPropertyKey,
keyStringFromKeyChain,
mergeDeepFromKeyChain,
valueByKeyChain,
Expand Down Expand Up @@ -204,6 +205,38 @@ describe('isArrayValue', () => {
});
});

describe('isValidPropertyKey', () => {
it('returns true for a valid string key', () => {
const key = 'validKey';
const result = isValidPropertyKey(key);
expect(result).toBeTruthy();
});

it('returns true for a valid number key', () => {
const key = 123;
const result = isValidPropertyKey(key);
expect(result).toBeTruthy();
});

it('returns true for 0 as a valid number key', () => {
const key = 0;
const result = isValidPropertyKey(key);
expect(result).toBeTruthy();
});

it('returns false for an empty string key', () => {
const key = '';
const result = isValidPropertyKey(key);
expect(result).toBeFalsy();
});

it('returns false for undefined key', () => {
const key = undefined;
const result = isValidPropertyKey(key);
expect(result).toBeFalsy();
});
});

/* validationKeyStringFromKeyChain */
describe('keyStringFromKeyChain', () => {
it('converts a keychain to dot notation to target an item stored by keychain string', () => {
Expand All @@ -212,6 +245,12 @@ describe('keyStringFromKeyChain', () => {
expect(result).toBe('test.value.3.target');
});

it('converts a keychain to dot notation to target an item stored by keychain string when the keychain containes a zero', () => {
const keyChain = ['test', 'value', 0, 'target'];
const result = keyStringFromKeyChain(keyChain, 'dots');
expect(result).toBe('test.value.0.target');
});

it('converts a keychain to bracket notation to target an item stored by keychain string', () => {
const keyChain = ['test', 'value', 3, 'target'];
const result = keyStringFromKeyChain(keyChain, 'brackets');
Expand Down
11 changes: 10 additions & 1 deletion module/src/form/utils/keyChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export function isArrayValue(value: unknown, attemptedAction: string): value is
return true;
}

/**
* Checks a possible keyChain to make sure it is a valid keyChain.
* @param keyChain The keyChain to check.
* @returns true if the keyChain is valid, false if not.
*/
export function isValidPropertyKey(propertyKey: PropertyKey | undefined): propertyKey is PropertyKey {
return typeof propertyKey === 'number' || (typeof propertyKey === 'string' && propertyKey.length > 0);
}

/**
* Converts a keyChain into a key string
* @param keyChain The chain of keys passed to `formProp` and used to access the property within a nested form object.
Expand All @@ -118,7 +127,7 @@ export function isArrayValue(value: unknown, attemptedAction: string): value is
export function keyStringFromKeyChain(keyChain: KeyChain | undefined, mode: 'dots' | 'brackets'): string {
switch (mode) {
case 'dots':
return keyChain?.filter(key => !!key).join('.') ?? '';
return keyChain?.filter(isValidPropertyKey).join('.') ?? '';
case 'brackets':
return (
keyChain?.reduce<string>((attrString, key) => {
Expand Down