invert
invert
(collection: KeyedCollection) => KeyedCollection
Swap the values and keys of a KeyedCollection
invert({ a: 1, b: 2 }) // is { 1: 'a', 2: 'b' } const aMap = new Map([ ['a', 1], ['b', 2], ]) invert(aMap) // is a new Map [[1, 'a'], [2, 'b']]
invert({ a: 1, b: 2 }) // is { 1: 'a', 2: 'b' } const aMap = new Map([ ['a', 1], ['b', 2], ]) invert(aMap) // is a new Map [[1, 'a'], [2, 'b']]
invert({ a: 1, b: 2 }) // is { 1: 'a', 2: 'b' } const aMap = new Map([ ['a', 1], ['b', 2], ]) invert(aMap) // is a new Map [[1, 'a'], [2, 'b']]
invert({ a: 1, b: 2 }) // is { 1: 'a', 2: 'b' } const aMap = new Map([ ['a', 1], ['b', 2], ]) invert(aMap) // is a new Map [[1, 'a'], [2, 'b']]
One common case for invert is to get a two-way enum mapping. Below, we run a stock reporting website. People don't remember stock symbols, so we can use invert to convert between symbols and company names. With that, let's create a report for our users, as well as let them query the price for a single company.
const companyByStock = { NVDA: 'NVIDIA', AAPL: 'Apple', } const stockByCompany = invert(companyByStock) const stockPrices = { AAPL: 200, NVDA: 150, } const formatPrice = (price, stock) => { const company = companyByStock[stock] return ` ${company} - $${price}` } const formatAllPrices = mapValues(formatPrice) const createReport = compose([ formatAllPrices, joinValues('\n'), prepend('Company Stock Prices\n'), ]) const report = createReport(stockPrices) console.log(report) // is // Company Stock Prices // Apple - $200 // Nvidia - $150 const getPriceForCompany = company => { const stock = stockByCompany[company] return stockPrices[stock] } const price = getPriceForCompany('Apple') console.log(price) // is 200
type Stock = 'NVDA' | 'AAPL' type Company = 'NVIDIA' | 'Apple' const companyByStock = { NVDA: 'NVIDIA', AAPL: 'Apple', } as const const stockByCompany = invert(companyByStock) const stockPrices: Record<Stock, number> = { AAPL: 200, NVDA: 150, } const formatPrice = (price: number, stock: Stock) => { const company = companyByStock[stock] return ` ${company} - $${price}` } const formatAllPrices = mapValues(formatPrice)<Record<Stock, number>> const createReport = compose([ formatAllPrices, joinValues('\n'), prepend('Company Stock Prices\n'), ]) const report = createReport(stockPrices) console.log(report) // is // Company Stock Prices // Apple - $200 // Nvidia - $150 const getPriceForCompany = (company: Company) => { const stock = stockByCompany[company] return stockPrices[stock] } const price = getPriceForCompany('Apple') console.log(price) // is 200
const companyByStock = { NVDA: 'NVIDIA', AAPL: 'Apple', } const stockByCompany = invert(companyByStock) const stockPrices = { AAPL: 200, NVDA: 150, } const formatPrice = (price, stock) => { const company = companyByStock[stock] return ` ${company} - $${price}` } const formatAllPrices = mapValues(formatPrice) const createReport = compose([ formatAllPrices, joinValues('\n'), prepend('Company Stock Prices\n'), ]) const report = createReport(stockPrices) console.log(report) // is // Company Stock Prices // Apple - $200 // Nvidia - $150 const getPriceForCompany = company => { const stock = stockByCompany[company] return stockPrices[stock] } const price = getPriceForCompany('Apple') console.log(price) // is 200
type Stock = 'NVDA' | 'AAPL' type Company = 'NVIDIA' | 'Apple' const companyByStock = { NVDA: 'NVIDIA', AAPL: 'Apple', } as const const stockByCompany = invert(companyByStock) const stockPrices: Record<Stock, number> = { AAPL: 200, NVDA: 150, } const formatPrice = ( price: number, stock: Stock ) => { const company = companyByStock[stock] return ` ${company} - $${price}` } const formatAllPrices = mapValues(formatPrice)< Record<Stock, number> > const createReport = compose([ formatAllPrices, joinValues('\n'), prepend('Company Stock Prices\n'), ]) const report = createReport(stockPrices) console.log(report) // is // Company Stock Prices // Apple - $200 // Nvidia - $150 const getPriceForCompany = (company: Company) => { const stock = stockByCompany[company] return stockPrices[stock] } const price = getPriceForCompany('Apple') console.log(price) // is 200