swapFirstTwoArgs
swapFirstTwoArgs
(fn: function) => function
Return a function that calls
fn
with the first two arguments swapped.const returnArgs = (...args) => args const returnSwappedArgs = swapFirstTwoArgs(returnArgs) const result = returnSwappedArgs('a', 'b', 'c') console.log(result) // is [ // b // a // c // ]
const returnArgs = (a1: string, a2: number, a3: boolean) => [a1, a2, a3] const returnSwappedArgs = swapFirstTwoArgs(returnArgs) const result = returnSwappedArgs(1, 'a', true) console.log(result) // is [ // a // 1 // true // ]
const returnArgs = (...args) => args const returnSwappedArgs = swapFirstTwoArgs(returnArgs) const result = returnSwappedArgs('a', 'b', 'c') console.log(result) // is [ // b // a // c // ]
const returnArgs = ( a1: string, a2: number, a3: boolean ) => [a1, a2, a3] const returnSwappedArgs = swapFirstTwoArgs(returnArgs) const result = returnSwappedArgs(1, 'a', true) console.log(result) // is [ // a // 1 // true // ]
Most iterating callbacks have the signature
(value, key, collection) => any
. For examples, see array.map, map.forEach, _.mapKeys etc. This makes sense, as we usually care about the value. Sometimes, however, we care more about the key.This is where
swapFirstTwoArgs
is helpful. We can pass a function that expects a key as its first parameter, then use it as a callback. Because this is the primary use case, we can assign the alias 'byKey,' as seen below.Onto the example
Below, we are writing library software that tracks checked-out books. Other systems require lowercase names, so let's map over our keys to be compatible.
import { mapKeys, swapFirstTwoArgs as byKey } from 'common-fp' const checkedOutBooks = { Emma: ['The Very Hungry Caterpillar'], Meg: ['The Hobbit'], } const toLower = str => str.toLowerCase() const lowerKey = byKey(toLower) const lowerAllKeys = mapKeys(lowerKey) const checkouts = lowerAllKeys(checkedOutBooks) console.log(checkouts) // is { // emma: [The Very Hungry Caterpillar] // meg: [The Hobbit] // }
import { mapKeys, swapFirstTwoArgs as byKey } from 'common-fp' type Checkouts = Record<string, string[]> const checkouts: Checkouts = { Emma: ['The Very Hungry Caterpillar'], Meg: ['The Hobbit'], } const toLower = (str: string) => str.toLowerCase() const lowerKey = byKey(toLower) const lowerAllKeys = mapKeys(lowerKey)<Checkouts> const updatedCheckouts = lowerAllKeys(checkouts) console.log(updatedCheckouts) // is { // emma: [The Very Hungry Caterpillar] // meg: [The Hobbit] // }
import { mapKeys, swapFirstTwoArgs as byKey, } from 'common-fp' const checkedOutBooks = { Emma: ['The Very Hungry Caterpillar'], Meg: ['The Hobbit'], } const toLower = str => str.toLowerCase() const lowerKey = byKey(toLower) const lowerAllKeys = mapKeys(lowerKey) const checkouts = lowerAllKeys(checkedOutBooks) console.log(checkouts) // is { // emma: [The Very Hungry Caterpillar] // meg: [The Hobbit] // }
import { mapKeys, swapFirstTwoArgs as byKey, } from 'common-fp' type Checkouts = Record<string, string[]> const checkouts: Checkouts = { Emma: ['The Very Hungry Caterpillar'], Meg: ['The Hobbit'], } const toLower = (str: string) => str.toLowerCase() const lowerKey = byKey(toLower) const lowerAllKeys = mapKeys(lowerKey)<Checkouts> const updatedCheckouts = lowerAllKeys(checkouts) console.log(updatedCheckouts) // is { // emma: [The Very Hungry Caterpillar] // meg: [The Hobbit] // }