|
| 1 | +import type { BlobType } from '@sirutils/core' |
| 2 | + |
| 3 | +export const isObject = (value: BlobType): value is object => |
| 4 | + typeof value === 'object' && !Array.isArray(value) && value !== null && !(value instanceof Date) |
| 5 | + |
| 6 | +export const unique = (data: BlobType[]) => [...new Set(data)] |
| 7 | + |
| 8 | +const objectKeys = (object: object): Set<string> => { |
| 9 | + return new Set(Object.keys(object)) |
| 10 | +} |
| 11 | + |
| 12 | +const areSetsEqual = <T>(set1: Set<T>, set2: Set<T>): boolean => { |
| 13 | + const difference = new Set(set1) |
| 14 | + for (const elem of set2) { |
| 15 | + if (difference.has(elem)) { |
| 16 | + difference.delete(elem) |
| 17 | + } else { |
| 18 | + return false |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return difference.size === 0 |
| 23 | +} |
| 24 | + |
| 25 | +export const filterUndefined = <T extends Sirutils.Seql.ValueRecord>(object: T): T => { |
| 26 | + return Object.fromEntries(Object.entries(object).filter(([_, v]) => v !== undefined)) as T |
| 27 | +} |
| 28 | + |
| 29 | +export const extractKeys = <T extends Sirutils.Seql.ValueRecord>(objects: T[]): string[] => { |
| 30 | + if (objects.length === 0) { |
| 31 | + throw new Error('Cannot call extractKeys on empty list') |
| 32 | + } |
| 33 | + |
| 34 | + // biome-ignore lint/style/noNonNullAssertion: <explanation> |
| 35 | + const keys = objectKeys(objects[0]!) |
| 36 | + |
| 37 | + for (const o of objects) { |
| 38 | + const oKeys = objectKeys(o) |
| 39 | + if (!areSetsEqual(keys, oKeys)) { |
| 40 | + throw new Error(`Objects have different keys: ${JSON.stringify(objects)}`) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return Array.from(keys) |
| 45 | +} |
0 commit comments