{"version":3,"file":"max-length-array.mjs","sources":["../../src/array/max-length-array.mts"],"sourcesContent":["import { Arr, asUint32, memoizeFunction, Result } from 'ts-data-forge';\nimport { type MaxLengthArray, type SupportedLength } from 'ts-type-forge';\nimport { type Type } from '../type.mjs';\nimport {\n  createPrimitiveValidationError,\n  type ValidationError,\n} from '../utils/index.mjs';\nimport { buildType, validateElements } from './build-type.mjs';\n\nexport type { MaxLengthArray } from 'ts-type-forge';\n\n/**\n * Creates a `Type` for a readonly array with at most `maxLength` elements,\n * typed as the branded {@link MaxLengthArray} instead of the union of tuple\n * types produced by `maxLengthTuple`.\n *\n * Because the length constraint lives only in the brand, no union of tuples\n * of the element type is ever constructed, which keeps type-checking cheap\n * even when `A` is a large type.\n */\nexport function maxLengthArray<A, N extends SupportedLength>(\n  maxLength: N,\n  elementType: Type<A>,\n  options?: Partial<\n    Readonly<{\n      typeName: string;\n      defaultValue: MaxLengthArray<N, A>;\n    }>\n  >,\n): Type<MaxLengthArray<N, A>>;\n\n// For bounds outside `SupportedLength` (`0..2048`) the length cannot be encoded in the brand,\n// so the result length is left unconstrained (`readonly A[]`).\nexport function maxLengthArray<A>(\n  maxLength: number,\n  elementType: Type<A>,\n  options?: Partial<\n    Readonly<{\n      typeName: string;\n      defaultValue: readonly A[];\n    }>\n  >,\n): Type<readonly A[]>;\n\nexport function maxLengthArray<A>(\n  maxLength: number,\n  elementType: Type<A>,\n  options?: Partial<\n    Readonly<{\n      typeName: string;\n      defaultValue: readonly A[];\n    }>\n  >,\n): Type<readonly A[]> {\n  type T = readonly A[];\n\n  const typeName =\n    options?.typeName ??\n    `MaxLengthArray<${maxLength}, ${elementType.typeName}>`;\n\n  const getDefaultValue = memoizeFunction(\n    (): T =>\n      options?.defaultValue ??\n      // The empty array is the shortest value satisfying `length <= maxLength`.\n      Arr.create(0, elementType.defaultValue),\n  );\n\n  const validate: Type<T>['validate'] = (a) => {\n    if (!Arr.isArray(a)) {\n      return Result.err([\n        createPrimitiveValidationError({\n          actualValue: a,\n          expectedType: 'array',\n          typeName,\n          details: undefined,\n        }),\n      ]);\n    }\n\n    if (a.length > maxLength) {\n      return Result.err([\n        {\n          path: [],\n          actualValue: a,\n          expectedType: typeName,\n          typeName,\n          details: {\n            kind: 'array-max-length',\n            maxLength,\n            actualLength: a.length,\n          },\n        } satisfies ValidationError,\n      ]);\n    }\n\n    return validateElements(a, elementType);\n  };\n\n  const fill: Type<T>['fill'] = (a) =>\n    Arr.isArray(a)\n      ? // Keep the input but trim down to at most `maxLength` elements.\n        Arr.map(\n          Arr.take(a, asUint32(maxLength)),\n          (el) => elementType.fill(el) satisfies A,\n        )\n      : getDefaultValue();\n\n  return buildType({ typeName, getDefaultValue, validate, fill, elementType });\n}\n"],"names":[],"mappings":";;;;AA4CO,SAAS,cAAA,CACd,SAAA,EACA,WAAA,EACA,OAAA,EAMoB;AAGpB,EAAA,MAAM,WACJ,OAAA,EAAS,QAAA,IACT,kBAAkB,SAAS,CAAA,EAAA,EAAK,YAAY,QAAQ,CAAA,CAAA,CAAA;AAEtD,EAAA,MAAM,eAAA,GAAkB,eAAA;AAAA,IACtB,MACE,OAAA,EAAS,YAAA;AAAA,IAET,GAAA,CAAI,MAAA,CAAO,CAAA,EAAG,WAAA,CAAY,YAAY;AAAA,GAC1C;AAEA,EAAA,MAAM,QAAA,GAAgC,CAAC,CAAA,KAAM;AAC3C,IAAA,IAAI,CAAC,GAAA,CAAI,OAAA,CAAQ,CAAC,CAAA,EAAG;AACnB,MAAA,OAAO,OAAO,GAAA,CAAI;AAAA,QAChB,8BAAA,CAA+B;AAAA,UAC7B,WAAA,EAAa,CAAA;AAAA,UACb,YAAA,EAAc,OAAA;AAAA,UACd,QAAA;AAAA,UACA,OAAA,EAAS;AAAA,SACV;AAAA,OACF,CAAA;AAAA,IACH;AAEA,IAAA,IAAI,CAAA,CAAE,SAAS,SAAA,EAAW;AACxB,MAAA,OAAO,OAAO,GAAA,CAAI;AAAA,QAChB;AAAA,UACE,MAAM,EAAC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