remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.13 kB
Source Map (JSON)
{"version":3,"file":"addProp.cjs","names":["purry"],"sources":["../src/addProp.ts"],"sourcesContent":["import type { UpsertProp } from \"./internal/types/UpsertProp\";\nimport { purry } from \"./purry\";\n\n/**\n * Add a new property to an object.\n *\n * The function doesn't do any checks on the input object. If the property\n * already exists it will be overwritten, and the type of the new value is not\n * checked against the previous type.\n *\n * Use `set` to override values explicitly with better protections.\n *\n * @param obj - The target object.\n * @param prop - The property name.\n * @param value - The property value.\n * @signature\n * R.addProp(obj, prop, value)\n * @example\n * R.addProp({firstName: 'john'}, 'lastName', 'doe') // => {firstName: 'john', lastName: 'doe'}\n * @dataFirst\n * @category Object\n */\nexport function addProp<T, K extends PropertyKey, V>(\n obj: T,\n prop: K,\n value: V,\n): UpsertProp<T, K, V>;\n\n/**\n * Add a new property to an object.\n *\n * The function doesn't do any checks on the input object. If the property\n * already exists it will be overwritten, and the type of the new value is not\n * checked against the previous type.\n *\n * Use `set` to override values explicitly with better protections.\n *\n * @param prop - The property name.\n * @param value - The property value.\n * @signature\n * R.addProp(prop, value)(obj)\n * @example\n * R.addProp('lastName', 'doe')({firstName: 'john'}) // => {firstName: 'john', lastName: 'doe'}\n * @dataLast\n * @category Object\n */\nexport function addProp<T, K extends PropertyKey, V>(\n prop: K,\n value: V,\n): (obj: T) => UpsertProp<T, K, V>;\n\nexport function addProp(...args: readonly unknown[]): unknown {\n return purry(addPropImplementation, args);\n}\n\nconst addPropImplementation = <T, K extends PropertyKey, V>(\n obj: T,\n prop: K,\n value: V,\n): UpsertProp<T, K, V> =>\n // @ts-expect-error [ts2322] - Hard to type...\n ({ ...obj, [prop]: value });\n"],"mappings":"wCAmDA,SAAgB,EAAQ,GAAG,EAAmC,CAC5D,OAAOA,EAAAA,EAAM,EAAuB,EAAK,CAG3C,MAAM,GACJ,EACA,EACA,KAGC,CAAE,GAAG,GAAM,GAAO,EAAO"}