From 7575bf8aed3212bbb5706e9a38e267d00efff7e5 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Sat, 18 Mar 2023 02:45:51 -0400 Subject: [PATCH] fix(striker-ui-api): add function to disassemble entity id --- striker-ui-api/src/lib/disassembleEntityId.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 striker-ui-api/src/lib/disassembleEntityId.ts diff --git a/striker-ui-api/src/lib/disassembleEntityId.ts b/striker-ui-api/src/lib/disassembleEntityId.ts new file mode 100644 index 00000000..609d46f3 --- /dev/null +++ b/striker-ui-api/src/lib/disassembleEntityId.ts @@ -0,0 +1,20 @@ +export const getEntityName = (id: string) => id.replace(/\d*$/, ''); + +export const getEntityNumber = (id: string) => + Number.parseInt(id.replace(/^[^\d]*/, '')); + +export const getEntityParts = (id: string) => { + let name = ''; + let number = NaN; + + const matchResult = id.match(/^([^\d]*)(\d*)$/); + + if (matchResult) { + const parts = matchResult; + + name = parts[1]; + number = Number.parseInt(parts[2]); + } + + return { name, number }; +};