add an underscorize helper and use it for form fields where they need to be python identifiers

This commit is contained in:
burnettk 2022-12-26 22:54:51 -05:00
parent b0b5ccf74d
commit f6c5c005d9
3 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,8 @@
import { convertSecondsToFormattedDateString, slugifyString } from './helpers'; import {
convertSecondsToFormattedDateString,
slugifyString,
underscorizeString,
} from './helpers';
test('it can slugify a string', () => { test('it can slugify a string', () => {
expect(slugifyString('hello---world_ and then Some such-')).toEqual( expect(slugifyString('hello---world_ and then Some such-')).toEqual(
@ -6,6 +10,12 @@ test('it can slugify a string', () => {
); );
}); });
test('it can underscorize a string', () => {
expect(underscorizeString('hello---world_ and then Some such-')).toEqual(
'hello_world_and_then_some_such'
);
});
test('it can keep the correct date when converting seconds to date', () => { test('it can keep the correct date when converting seconds to date', () => {
const dateString = convertSecondsToFormattedDateString(1666325400); const dateString = convertSecondsToFormattedDateString(1666325400);
expect(dateString).toEqual('2022-10-21'); expect(dateString).toEqual('2022-10-21');

View File

@ -20,6 +20,10 @@ export const slugifyString = (str: any) => {
.replace(/-+$/g, ''); .replace(/-+$/g, '');
}; };
export const underscorizeString = (inputString: string) => {
return slugifyString(inputString).replace(/-/g, '_');
};
export const capitalizeFirstLetter = (string: any) => { export const capitalizeFirstLetter = (string: any) => {
return string.charAt(0).toUpperCase() + string.slice(1); return string.charAt(0).toUpperCase() + string.slice(1);
}; };

View File

@ -3,7 +3,11 @@ import { useEffect, useState } from 'react';
import { Button, Select, SelectItem, TextInput } from '@carbon/react'; import { Button, Select, SelectItem, TextInput } from '@carbon/react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { FormField } from '../interfaces'; import { FormField } from '../interfaces';
import { modifyProcessIdentifierForPathParam, slugifyString } from '../helpers'; import {
modifyProcessIdentifierForPathParam,
slugifyString,
underscorizeString,
} from '../helpers';
import HttpService from '../services/HttpService'; import HttpService from '../services/HttpService';
export default function JsonSchemaFormBuilder() { export default function JsonSchemaFormBuilder() {
@ -75,7 +79,7 @@ export default function JsonSchemaFormBuilder() {
formFieldIdHasBeenUpdatedByUser formFieldIdHasBeenUpdatedByUser
); );
if (!formFieldIdHasBeenUpdatedByUser) { if (!formFieldIdHasBeenUpdatedByUser) {
setFormFieldId(slugifyString(newFormFieldTitle)); setFormFieldId(underscorizeString(newFormFieldTitle));
} }
setFormFieldTitle(newFormFieldTitle); setFormFieldTitle(newFormFieldTitle);
}; };