From 6529b49c3ca2e0e71504daea605e8565bf3ce371 Mon Sep 17 00:00:00 2001 From: Arnav Nath <130316679+ArnavNath550@users.noreply.github.com> Date: Mon, 26 Aug 2024 22:15:02 +1200 Subject: [PATCH] feat: bookingFields identifier supports - and _ characters (#16353) * now bookingFields identifier supports - and _ characters but no special characters * fix: unit test --------- Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com> --- packages/lib/getValidRhfFieldName.test.ts | 14 +++++++++----- packages/lib/getValidRhfFieldName.ts | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/lib/getValidRhfFieldName.test.ts b/packages/lib/getValidRhfFieldName.test.ts index 7d3d673809..a7855ead2d 100644 --- a/packages/lib/getValidRhfFieldName.test.ts +++ b/packages/lib/getValidRhfFieldName.test.ts @@ -8,9 +8,8 @@ describe("getValidRhfFieldName", () => { expect(getValidRhfFieldName("HELLO")).toEqual("HELLO"); }); - it("should convert spaces, _, and any other special character to -", () => { + it("should convert spaces and any other special character to -", () => { expect(getValidRhfFieldName("hello there")).toEqual("hello-there"); - expect(getValidRhfFieldName("hello_there")).toEqual("hello-there"); expect(getValidRhfFieldName("hello$there")).toEqual("hello-there"); expect(getValidRhfFieldName("$hello$there")).toEqual("-hello-there"); expect(getValidRhfFieldName("$hello.there")).toEqual("-hello-there"); @@ -20,9 +19,14 @@ describe("getValidRhfFieldName", () => { // We don't really care about a hyphen in the end it("should not remove dashes from start and end.", () => { expect(getValidRhfFieldName("hello-there-")).toEqual("hello-there-"); - expect(getValidRhfFieldName("hello-there_")).toEqual("hello-there-"); - expect(getValidRhfFieldName("_hello-there_")).toEqual("-hello-there-"); - expect(getValidRhfFieldName("$hello-there_")).toEqual("-hello-there-"); + expect(getValidRhfFieldName("-hello-there")).toEqual("-hello-there"); + expect(getValidRhfFieldName("$hello-there-")).toEqual("-hello-there-"); + }); + + it("should not remove underscore from start and end.", () => { + expect(getValidRhfFieldName("hello-there_")).toEqual("hello-there_"); + expect(getValidRhfFieldName("_hello-there_")).toEqual("_hello-there_"); + expect(getValidRhfFieldName("$hello-there_")).toEqual("-hello-there_"); }); it("should remove unicode and emoji characters", () => { diff --git a/packages/lib/getValidRhfFieldName.ts b/packages/lib/getValidRhfFieldName.ts index a052381390..bb2a38fe2c 100644 --- a/packages/lib/getValidRhfFieldName.ts +++ b/packages/lib/getValidRhfFieldName.ts @@ -1,5 +1,5 @@ export const getValidRhfFieldName = (fieldName: string) => { // Remember that any transformation that you do here would run on System Field names as well. So, be careful and avoiding doing anything here that would modify the SystemField names. // e.g. SystemField name currently have uppercases in them. So, no need to lowercase unless absolutely needed. - return fieldName.replace(/[^a-zA-Z0-9]/g, "-"); + return fieldName.replace(/[^a-zA-Z0-9-_]/g, "-"); };