{"version":3,"file":"common.mjs","names":[],"sources":["../src/common.ts"],"sourcesContent":["/* eslint-disable no-bitwise */\nimport { compress, decompress } from '@sarakusha/lzma';\nimport { ComplexLEDDisplayInfo } from '@novastar/native/ComplexLEDDisplayInfo';\nimport { LEDDisplyTypeEnum } from '@novastar/native/LEDDisplyType';\nimport { ScanBoardConnectTypeEnum } from '@novastar/native/ScanBoardConnectType';\nimport { SimpleLEDDisplayInfo } from '@novastar/native/SimpleLEDDisplayInfo';\nimport { StandardLEDDisplayInfo } from '@novastar/native/StandardLEDDisplayInfo';\nimport groupBy from 'lodash/groupBy';\n\n/**\n * 'Not Empty' type guard\n * @param value\n */\nexport function notEmpty<TValue>(value: TValue | null | undefined | void): value is TValue {\n  return (\n    value !== undefined && value !== null && (typeof value !== 'number' || !Number.isNaN(value))\n  );\n}\n\nexport type Id<T> = {} & { [P in keyof T]: T[P] };\n\nexport const crc16 = (data: Buffer, initial: number): number =>\n  data.reduce((acc, value) => (acc + value) & 0xffff, initial);\n\nexport const crc8 = (data: Buffer, initial = 0): number =>\n  data.reduce((acc, value) => (acc + value) & 0xff, initial);\n/*\nprops[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);\n\nSRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)\n{\n  UInt32 dicSize;\n  Byte d;\n\n  if (size < LZMA_PROPS_SIZE)\n    return SZ_ERROR_UNSUPPORTED;\n  else\n    dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);\n\n  if (dicSize < LZMA_DIC_MIN)\n    dicSize = LZMA_DIC_MIN;\n  p->dicSize = dicSize;\n\n  d = data[0];\n  if (d >= (9 * 5 * 5))\n    return SZ_ERROR_UNSUPPORTED;\n\n  p->lc = (Byte)(d % 9);\n  d /= 9;\n  p->pb = (Byte)(d / 5);\n  p->lp = (Byte)(d % 5);\n\n  return SZ_OK;\n}\n */\n\nexport const unpack = (props: Buffer | string, length: number, buffer: Buffer): Promise<string> => {\n  const propsBuf = Buffer.isBuffer(props) ? props : Buffer.from(props, 'binary');\n  if (propsBuf.length !== 5) throw new TypeError('Invalid props length');\n  const lengthBuf = Buffer.alloc(8);\n  if (length === -1) lengthBuf.fill(0xff);\n  else lengthBuf.writeUInt32LE(length);\n  const data = Buffer.concat([propsBuf, lengthBuf, buffer]);\n  return new Promise<string>((resolve, reject) => {\n    decompress(data, (res, err) => {\n      if (res) resolve(Buffer.from(res).toString());\n      else reject(err);\n    });\n  });\n};\n\nexport const pack = async (data: Buffer | string): Promise<[string, Buffer]> => {\n  const compressed = await new Promise<Buffer>((resolve, reject) => {\n    compress(data, 8, (res, err) => {\n      if (res) resolve(Buffer.from(res));\n      else reject(err);\n    });\n  });\n  return [compressed.slice(0, 5).toString('binary'), compressed.slice(5 + 8)];\n};\n\nexport const isHorizontalConnection = (\n  connectType: ScanBoardConnectTypeEnum,\n): connectType is\n  | ScanBoardConnectTypeEnum.LeftTop_Horizontal\n  | ScanBoardConnectTypeEnum.LeftBottom_Horizontal\n  | ScanBoardConnectTypeEnum.RightTop_Horizontal\n  | ScanBoardConnectTypeEnum.RightBottom_Horizontal =>\n  connectType === ScanBoardConnectTypeEnum.LeftTop_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.LeftBottom_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.RightTop_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.RightBottom_Horizontal;\n\nexport const isTopConnection = (\n  connectType: ScanBoardConnectTypeEnum,\n): connectType is\n  | ScanBoardConnectTypeEnum.LeftTop_Horizontal\n  | ScanBoardConnectTypeEnum.RightTop_Horizontal\n  | ScanBoardConnectTypeEnum.LeftTop_Vertical\n  | ScanBoardConnectTypeEnum.RightTop_Vertical =>\n  connectType === ScanBoardConnectTypeEnum.LeftTop_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.RightTop_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.LeftTop_Vertical ||\n  connectType === ScanBoardConnectTypeEnum.RightTop_Vertical;\n\nexport const isLeftConnection = (\n  connectType: ScanBoardConnectTypeEnum,\n): connectType is\n  | ScanBoardConnectTypeEnum.LeftTop_Horizontal\n  | ScanBoardConnectTypeEnum.LeftTop_Vertical\n  | ScanBoardConnectTypeEnum.LeftBottom_Horizontal\n  | ScanBoardConnectTypeEnum.LeftBottom_Vertical =>\n  connectType === ScanBoardConnectTypeEnum.LeftTop_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.LeftTop_Vertical ||\n  connectType === ScanBoardConnectTypeEnum.LeftBottom_Horizontal ||\n  connectType === ScanBoardConnectTypeEnum.LeftBottom_Vertical;\n\nexport const minimax = (min: number, max: number, value: number): number =>\n  Math.min(Math.max(value, min), max);\n\nexport type LEDDisplayInfo = SimpleLEDDisplayInfo | StandardLEDDisplayInfo | ComplexLEDDisplayInfo;\n\nexport const isSimpleScreen = (screen: LEDDisplayInfo): screen is SimpleLEDDisplayInfo =>\n  screen.Type === LEDDisplyTypeEnum.SimpleSingleType;\n\nexport const isStandardScreen = (screen: LEDDisplayInfo): screen is StandardLEDDisplayInfo =>\n  screen.Type === LEDDisplyTypeEnum.StandardType;\n\nexport const isComplexScreen = (screen: LEDDisplayInfo): screen is ComplexLEDDisplayInfo =>\n  screen.Type === LEDDisplyTypeEnum.ComplexType;\n\ntype ExcludeUndefined<T> = {\n  [P in keyof T]-?: Exclude<T[P], undefined>;\n};\n\ntype MakeRequired<T, K extends keyof T> = Id<T & ExcludeUndefined<Pick<T, K>>>;\n\nexport const hasProps =\n  <K extends string>(...props: K[]) =>\n  <T extends Partial<Record<K, unknown>>>(y: T): y is MakeRequired<T, K> =>\n    notEmpty(y) && props.reduce((acc: boolean, name: keyof T) => acc && notEmpty(y[name]), true);\n\nconst zipProps =\n  <K extends string>(props: K[]) =>\n  <T extends Record<K, number>>(item: T): string =>\n    props.map((name) => item[name]).join(':');\n\nconst unzipProps = <K extends string>(props: K[], key: string): Record<K, number> =>\n  key\n    .split(':')\n    .map(Number)\n    .reduce<Record<K, number>>(\n      (acc, value, i) => ({\n        ...acc,\n        [props[i]]: value,\n      }),\n      {} as Record<K, number>,\n    );\n\nexport const groupByProps =\n  <K extends string>(...props: K[]) =>\n  <T extends Record<K, number>>(list: T[]): [Record<K, number>, [T, ...T[]]][] =>\n    Object.entries(groupBy(list, zipProps(props))).map<[Record<K, number>, [T, ...T[]]]>(\n      ([key, items]) => [unzipProps(props, key), items as unknown as [T, ...T[]]],\n    );\n\nexport const itFirstNotNull = async <T>(it: AsyncIterable<T | null>): Promise<T | null> => {\n  for await (const res of it) {\n    if (res !== null) return res;\n  }\n  return null;\n};\n\nexport const itAll = async <T>(it: AsyncIterable<T | null>): Promise<(T | null)[]> => {\n  const all: (T | null)[] = [];\n  for await (const res of it) {\n    all.push(res);\n  }\n  return all;\n};\n\nexport const toHex = (value: number): string => {\n  const hex = value.toString(16);\n  const pos = hex.length - 4;\n  return pos > 0 ? `0x${hex.slice(0, pos)}_${hex.slice(pos)}` : `${value > 9 ? '0x' : ''}${hex}`;\n};\n\ntype FilterFlags<Base, Condition> = {\n  [Key in keyof Base]: Base[Key] extends Condition ? Key : never;\n};\n\nexport type AllowedNames<Base, Condition> = FilterFlags<Base, Condition>[keyof Base];\n"],"mappings":";;;;;;;;;;AAaA,SAAgB,SAAiB,OAA0D;CACzF,OACE,UAAU,UAAa,UAAU,SAAS,OAAO,UAAU,YAAY,CAAC,OAAO,MAAM,KAAK;AAE9F;AAIA,MAAa,SAAS,MAAc,YAClC,KAAK,QAAQ,KAAK,UAAW,MAAM,QAAS,OAAQ,OAAO;AAE7D,MAAa,QAAQ,MAAc,UAAU,MAC3C,KAAK,QAAQ,KAAK,UAAW,MAAM,QAAS,KAAM,OAAO;AA+B3D,MAAa,UAAU,OAAwB,QAAgB,WAAoC;CACjG,MAAM,WAAW,OAAO,SAAS,KAAK,IAAI,QAAQ,OAAO,KAAK,OAAO,QAAQ;CAC7E,IAAI,SAAS,WAAW,GAAG,MA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