All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.lib.util.propsFactory.mjs.map Maven / Gradle / Ivy

{"version":3,"file":"propsFactory.mjs","names":["propsFactory","props","source","defaults","Object","keys","reduce","obj","prop","isObjectDefinition","Array","isArray","definition","type","default"],"sources":["../../src/util/propsFactory.ts"],"sourcesContent":["// Types\nimport type { IfAny } from '@vue/shared' // eslint-disable-line vue/prefer-import-from-vue\nimport type { ComponentObjectPropsOptions, Prop, PropType } from 'vue'\n\n/**\n * Creates a factory function for props definitions.\n * This is used to define props in a composable then override\n * default values in an implementing component.\n *\n * @example Simplified signature\n * (props: Props) => (defaults?: Record) => Props\n *\n * @example Usage\n * const makeProps = propsFactory({\n *   foo: String,\n * })\n *\n * defineComponent({\n *   props: {\n *     ...makeProps({\n *       foo: 'a',\n *     }),\n *   },\n *   setup (props) {\n *     // would be \"string | undefined\", now \"string\" because a default has been provided\n *     props.foo\n *   },\n * }\n */\n\nexport function propsFactory<\n  PropsOptions extends ComponentObjectPropsOptions\n> (props: PropsOptions, source: string) {\n  return  = {}>(\n    defaults?: Defaults\n  ): AppendDefault => {\n    return Object.keys(props).reduce((obj, prop) => {\n      const isObjectDefinition = typeof props[prop] === 'object' && props[prop] != null && !Array.isArray(props[prop])\n      const definition = isObjectDefinition ? props[prop] : { type: props[prop] }\n\n      if (defaults && prop in defaults) {\n        obj[prop] = {\n          ...definition,\n          default: defaults[prop],\n        }\n      } else {\n        obj[prop] = definition\n      }\n\n      if (source && !obj[prop].source) {\n        obj[prop].source = source\n      }\n\n      return obj\n    }, {})\n  }\n}\n\ntype AppendDefault> = {\n  [P in keyof T]-?: unknown extends D[P]\n    ? T[P]\n    : T[P] extends Record\n      ? Omit & {\n        type: PropType>\n        default: MergeDefault\n      }\n      : {\n        type: PropType>\n        default: MergeDefault\n      }\n}\n\ntype MergeDefault = unknown extends D ? InferPropType : (NonNullable> | D)\n\n/**\n * Like `Partial` but doesn't care what the value is\n */\ntype PartialKeys = { [P in keyof T]?: unknown }\n\n// Copied from Vue\ntype InferPropType = [T] extends [null]\n  ? any // null & true would fail to infer\n  : [T] extends [{ type: null | true }]\n    // As TS issue https://github.com/Microsoft/TypeScript/issues/14829\n    // somehow `ObjectConstructor` when inferred from { (): T } becomes `any`\n    // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`\n    ? any\n    : [T] extends [ObjectConstructor | { type: ObjectConstructor }]\n      ? Record\n      : [T] extends [BooleanConstructor | { type: BooleanConstructor }]\n        ? boolean\n        : [T] extends [DateConstructor | { type: DateConstructor }]\n          ? Date\n          : [T] extends [(infer U)[] | { type: (infer U)[] }]\n            ? U extends DateConstructor\n              ? Date | InferPropType\n              : InferPropType\n            : [T] extends [Prop]\n              ? unknown extends V\n                ? IfAny\n                : V\n              : T\n"],"mappings":"AAAA;AACyC;;AAGzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,OAAO,SAASA,YAAYA,CAEzBC,KAAmB,EAAEC,MAAc,EAAE;EACtC,OACEC,QAAmB,IACuB;IAC1C,OAAOC,MAAM,CAACC,IAAI,CAACJ,KAAK,CAAC,CAACK,MAAM,CAAM,CAACC,GAAG,EAAEC,IAAI,KAAK;MACnD,MAAMC,kBAAkB,GAAG,OAAOR,KAAK,CAACO,IAAI,CAAC,KAAK,QAAQ,IAAIP,KAAK,CAACO,IAAI,CAAC,IAAI,IAAI,IAAI,CAACE,KAAK,CAACC,OAAO,CAACV,KAAK,CAACO,IAAI,CAAC,CAAC;MAChH,MAAMI,UAAU,GAAGH,kBAAkB,GAAGR,KAAK,CAACO,IAAI,CAAC,GAAG;QAAEK,IAAI,EAAEZ,KAAK,CAACO,IAAI;MAAE,CAAC;MAE3E,IAAIL,QAAQ,IAAIK,IAAI,IAAIL,QAAQ,EAAE;QAChCI,GAAG,CAACC,IAAI,CAAC,GAAG;UACV,GAAGI,UAAU;UACbE,OAAO,EAAEX,QAAQ,CAACK,IAAI;QACxB,CAAC;MACH,CAAC,MAAM;QACLD,GAAG,CAACC,IAAI,CAAC,GAAGI,UAAU;MACxB;MAEA,IAAIV,MAAM,IAAI,CAACK,GAAG,CAACC,IAAI,CAAC,CAACN,MAAM,EAAE;QAC/BK,GAAG,CAACC,IAAI,CAAC,CAACN,MAAM,GAAGA,MAAM;MAC3B;MAEA,OAAOK,GAAG;IACZ,CAAC,EAAE,CAAC,CAAC,CAAC;EACR,CAAC;AACH;;AAkBA;AACA;AACA;;AAGA","ignoreList":[]}