fix(index): Apply various fixes to the index engine (#12501)
This commit is contained in:
committed by
GitHub
parent
32be40a2c0
commit
59bbff62d8
@@ -0,0 +1,26 @@
|
||||
import { compressName } from "../compress-name"
|
||||
|
||||
describe("compressName", () => {
|
||||
it("should remove consecutive duplicate names in sequence if it exceds 58 chars", () => {
|
||||
const name =
|
||||
"product_product_variant_id_order_order_id_long_long_long_long_name"
|
||||
const result = compressName(name)
|
||||
expect(result).toBe("product_variant_id_order_id_long_name33bb7b344")
|
||||
})
|
||||
|
||||
it("should remove duplicate names and truncate name to append a hash", () => {
|
||||
const name = "product_product_variant_id_order_order_id"
|
||||
const result = compressName(name, 30)
|
||||
expect(result).toBe("product_variant_id_ord91d0cda8")
|
||||
})
|
||||
|
||||
it("handles very long names with truncation and appends hash when necessary", () => {
|
||||
const name =
|
||||
"CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule"
|
||||
const result = compressName(name)
|
||||
expect(result).toHaveLength(58)
|
||||
expect(result).toBe(
|
||||
"cust_modu_impl_cont_area_big_name_that_exce_posg_1f1cc72da"
|
||||
)
|
||||
})
|
||||
})
|
||||
39
packages/core/utils/src/common/compress-name.ts
Normal file
39
packages/core/utils/src/common/compress-name.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { camelToSnakeCase } from "./camel-to-snake-case"
|
||||
import { simpleHash } from "./simple-hash"
|
||||
|
||||
export function compressName(name: string, limit = 58) {
|
||||
if (name.length <= limit) {
|
||||
return name.toLowerCase()
|
||||
}
|
||||
|
||||
const hash = simpleHash(name).toLowerCase()
|
||||
const hashLength = hash.length
|
||||
|
||||
// Remove consecutive duplicates
|
||||
const parts = name.toLowerCase().split("_")
|
||||
const deduplicatedParts: string[] = []
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
if (i === 0 || parts[i] !== parts[i - 1]) {
|
||||
deduplicatedParts.push(parts[i])
|
||||
}
|
||||
}
|
||||
|
||||
let result = deduplicatedParts.join("_")
|
||||
|
||||
// If still greater than the limit, truncate each part to 4 characters
|
||||
if (result.length > limit) {
|
||||
const allParts = camelToSnakeCase(name).split("_")
|
||||
result = allParts.map((part) => part.substring(0, 4)).join("_")
|
||||
}
|
||||
|
||||
name = result
|
||||
const nameLength = name.length
|
||||
const diff = nameLength + hashLength - limit
|
||||
|
||||
if (diff > 0) {
|
||||
return name.slice(0, nameLength - diff) + hash
|
||||
}
|
||||
|
||||
return (name + hash).toLowerCase()
|
||||
}
|
||||
@@ -4,6 +4,7 @@ export * from "./array-intersection"
|
||||
export * from "./build-query"
|
||||
export * from "./build-regexp-if-valid"
|
||||
export * from "./camel-to-snake-case"
|
||||
export * from "./compress-name"
|
||||
export * from "./container"
|
||||
export * from "./convert-item-response-to-update-request"
|
||||
export * from "./create-container-like"
|
||||
@@ -80,11 +81,11 @@ export * from "./to-handle"
|
||||
export * from "./to-kebab-case"
|
||||
export * from "./to-pascal-case"
|
||||
export * from "./to-unix-slash"
|
||||
export * from "./try-convert-to-number"
|
||||
export * from "./trim-zeros"
|
||||
export * from "./try-convert-to-boolean"
|
||||
export * from "./try-convert-to-number"
|
||||
export * from "./unflatten-object-keys"
|
||||
export * from "./upper-case-first"
|
||||
export * from "./validate-handle"
|
||||
export * from "./wrap-handler"
|
||||
export * from "./validate-module-name"
|
||||
export * from "./try-convert-to-boolean"
|
||||
export * from "./wrap-handler"
|
||||
|
||||
Reference in New Issue
Block a user