Skip to content

Commit

Permalink
remove illegal characters adapter v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Idokah committed Apr 2, 2024
1 parent 218c0f0 commit 4fb038a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion libs/velo-external-db-core/src/converters/data_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4fb038a

Please sign in to comment.