|
| 1 | +import type { BlobType } from '@sirutils/core' |
| 2 | + |
| 3 | +import { isGenerated, join } from './builder' |
| 4 | + |
| 5 | +export const escapeIdentifier = (identifier: string) => { |
| 6 | + let result = identifier |
| 7 | + |
| 8 | + if (result.charAt(0) === '"' && result.charAt(result.length - 1) === '"') { |
| 9 | + result = result.slice(1, result.length - 2) |
| 10 | + } |
| 11 | + if (result.includes('"')) { |
| 12 | + throw new Error(`Invalid identifier: ${result}`) |
| 13 | + } |
| 14 | + return `"${result}"` |
| 15 | +} |
| 16 | + |
| 17 | +export const filterUndefined = <Value = BlobType>( |
| 18 | + object: Sirutils.Seql.ValueRecord<Value> |
| 19 | +): Sirutils.Seql.ValueRecord<Value> => { |
| 20 | + const filteredEntries = Object.entries(object).filter(([_, v]) => v !== undefined) |
| 21 | + |
| 22 | + return Object.fromEntries(filteredEntries) |
| 23 | +} |
| 24 | + |
| 25 | +export const mergeLists = <T>(list1: readonly T[], list2: readonly T[]): T[] => { |
| 26 | + const result: T[] = [] |
| 27 | + |
| 28 | + for (let i = 0; i < Math.max(list1.length, list2.length); i++) { |
| 29 | + const elem1 = list1[i] |
| 30 | + if (elem1 !== undefined) { |
| 31 | + result.push(elem1) |
| 32 | + } |
| 33 | + |
| 34 | + const elem2 = list2[i] |
| 35 | + if (elem2 !== undefined) { |
| 36 | + const generated = (elem2 as BlobType).values |
| 37 | + .filter((value: BlobType) => isGenerated(value)) |
| 38 | + .map((value: BlobType) => value.builder) as BlobType |
| 39 | + ;(elem2 as BlobType).values = (elem2 as BlobType).values.filter( |
| 40 | + (value: BlobType) => !isGenerated(value) |
| 41 | + ) |
| 42 | + |
| 43 | + if (generated.length > 0) { |
| 44 | + result.push(join(generated) as T) |
| 45 | + } else { |
| 46 | + result.push(elem2) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return result |
| 52 | +} |
| 53 | + |
| 54 | +const areSetsEqual = <T>(set1: Set<T>, set2: Set<T>): boolean => { |
| 55 | + const difference = new Set(set1) |
| 56 | + for (const elem of set2) { |
| 57 | + if (difference.has(elem)) { |
| 58 | + difference.delete(elem) |
| 59 | + } else { |
| 60 | + return false |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return difference.size === 0 |
| 65 | +} |
| 66 | + |
| 67 | +const objectKeys = (object: object): Set<string> => { |
| 68 | + return new Set(Object.keys(object)) |
| 69 | +} |
| 70 | + |
| 71 | +export const extractKeys = <Value = BlobType>( |
| 72 | + objects: Sirutils.Seql.ValueRecord<Value>[] |
| 73 | +): string[] => { |
| 74 | + if (objects.length === 0) { |
| 75 | + throw new Error('Cannot call extractKeys on empty list') |
| 76 | + } |
| 77 | + |
| 78 | + // biome-ignore lint/style/noNonNullAssertion: <explanation> |
| 79 | + const keys = objectKeys(objects[0]!) |
| 80 | + |
| 81 | + for (const o of objects) { |
| 82 | + const oKeys = objectKeys(o) |
| 83 | + if (!areSetsEqual(keys, oKeys)) { |
| 84 | + throw new Error(`Objects have different keys: ${JSON.stringify(objects)}`) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return Array.from(keys) |
| 89 | +} |
0 commit comments