UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 2.01 kB
{"version":3,"file":"swapProps.cjs","names":["purry"],"sources":["../src/swapProps.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\ntype SwappedProps<T, K1 extends keyof T, K2 extends keyof T> = {\n [K in keyof T]: T[K1 extends K ? K2 : K2 extends K ? K1 : K];\n};\n\n/**\n * Swaps the values of two properties in an object based on the provided keys.\n *\n * @param data - The object to be manipulated.\n * @param key1 - The first property key.\n * @param key2 - The second property key.\n * @returns Returns the manipulated object.\n * @signature\n * swapProps(data, key1, key2)\n * @example\n * swapProps({a: 1, b: 2, c: 3}, 'a', 'b') // => {a: 2, b: 1, c: 3}\n * @dataFirst\n * @category Object\n */\nexport function swapProps<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T,\n>(data: T, key1: K1, key2: K2): SwappedProps<T, K1, K2>;\n\n/**\n * Swaps the values of two properties in an object based on the provided keys.\n *\n * @param key1 - The first property key.\n * @param key2 - The second property key.\n * @returns Returns the manipulated object.\n * @signature\n * swapProps(key1, key2)(data)\n * @example\n * swapProps('a', 'b')({a: 1, b: 2, c: 3}) // => {a: 2, b: 1, c: 3}\n * @dataLast\n * @category Object\n */\nexport function swapProps<\n T extends object,\n K1 extends keyof T,\n K2 extends keyof T,\n>(key1: K1, key2: K2): (data: T) => SwappedProps<T, K1, K2>;\n\nexport function swapProps(...args: ReadonlyArray<unknown>): unknown {\n return purry(swapPropsImplementation, args);\n}\n\nfunction swapPropsImplementation<T extends object>(\n obj: T,\n key1: keyof T,\n key2: keyof T,\n): Record<PropertyKey, unknown> {\n const { [key1]: value1, [key2]: value2 } = obj;\n return {\n ...obj,\n [key1]: value2,\n [key2]: value1,\n };\n}\n"],"mappings":"wCA6CA,SAAgB,EAAU,GAAG,EAAuC,CAClE,OAAOA,EAAAA,EAAM,EAAyB,EAAK,CAG7C,SAAS,EACP,EACA,EACA,EAC8B,CAC9B,GAAM,EAAG,GAAO,GAAS,GAAO,GAAW,EAC3C,MAAO,CACL,GAAG,GACF,GAAO,GACP,GAAO,EACT"}