From 4fb038ac90b9ed696f05b82fc6ef0ffb686b2988 Mon Sep 17 00:00:00 2001 From: Ido Kahlon Date: Tue, 2 Apr 2024 10:42:05 +0300 Subject: [PATCH] remove illegal characters adapter v2 --- libs/velo-external-db-core/src/converters/data_utils.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/velo-external-db-core/src/converters/data_utils.ts b/libs/velo-external-db-core/src/converters/data_utils.ts index cef555b7c..87eb6b55c 100644 --- a/libs/velo-external-db-core/src/converters/data_utils.ts +++ b/libs/velo-external-db-core/src/converters/data_utils.ts @@ -6,12 +6,19 @@ export const asWixData = (item: Item) => { return generateIdsIfNeeded(packDates(item)) } +const replaceNonAlphanumeric = (str: string) => { + // Replace non-alphanumeric characters with dashes + return str.replace(/[^a-zA-Z0-9]/g, '-') +} + export const generateIdsIfNeeded = (item: Item): ItemWithId => { if ('_id' in item) return item as ItemWithId const sha = crypto.createHash('sha1') const fieldsConcat = Object.values(item).join('') - return { ...item, _id: sha.update(fieldsConcat).digest('base64') } + const base64Digest = sha.update(fieldsConcat).digest('base64') + const validId = replaceNonAlphanumeric(base64Digest) + return { ...item, _id: validId } } const packDates = (item: Item) => Object.entries(item)