{"version":3,"sources":["../src/easyConstructor.ts"],"sourcesContent":["import { ConstructorType } from \"./types/ConstructorType.js\";\nimport type { Class, SetOptional } from \"type-fest\";\nimport { EasyConstructorOptions } from \"./types/EasyConstructorOptions.js\";\n\n/**\n * Creates a factory function that initializes a class with its properties.\n *\n * @example\n * ```ts\n * class ExampleClass {\n *   property1: string;\n *   property2: number;\n *\n *   static create = easyConstructor(ExampleClass);\n * }\n * // Creates an instance of ExampleClass\n * static exampleInstance = ExampleClass.create({ property1: 'value', property2: 42 });\n * ```\n *\n * @param classType The class to instantiate and initialize.\n * @param options The configuration of the class, including which fields to omit\n * and which fields to make optional.\n * @returns A factory function that creates an instance of the class.\n */\nexport function easyConstructor<\n  T,\n  Arguments extends unknown[],\n  TOmit extends keyof ConstructorType<T> = never,\n  TOptional extends keyof ConstructorType<T> = never,\n>(\n  classType: Class<T, Arguments>,\n  options?: EasyConstructorOptions<TOmit, TOptional>,\n) {\n  return function (\n    input: SetOptional<Omit<ConstructorType<T>, TOmit>, TOptional>,\n    ...constructorArguments: Arguments\n  ): T {\n    const newInstance = new classType(...constructorArguments) as Record<\n      string,\n      unknown\n    >;\n    for (const [key, value] of Object.entries(input)) {\n      if (options?.omit?.includes(key as TOmit)) {\n        continue;\n      }\n      if (\n        value === undefined &&\n        options?.optional?.includes(key as TOptional)\n      ) {\n        continue;\n      }\n      newInstance[key] = value;\n    }\n    return newInstance as T;\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBO,SAAS,gBAMd,WACA,SACA;AACA,SAAO,SACL,UACG,sBACA;AACH,UAAM,cAAc,IAAI,UAAU,GAAG,oBAAoB;AAIzD,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,KAAK,GAAG;AAChD,UAAI,SAAS,MAAM,SAAS,GAAY,GAAG;AACzC;AAAA,MACF;AACA,UACE,UAAU,UACV,SAAS,UAAU,SAAS,GAAgB,GAC5C;AACA;AAAA,MACF;AACA,kBAAY,GAAG,IAAI;AAAA,IACrB;AACA,WAAO;AAAA,EACT;AACF;","names":[]}