| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | 'use strict'; |
| 8 | |
| 9 | const invertObject = require('../invertObject'); |
| 10 | |
| 11 | const objectValues = target => Object.keys(target).map(key => target[key]); |
| 12 | |
| 13 | describe('invertObject', () => { |
| 14 | it('should return an empty object for an empty input', () => { |
| 15 | expect(invertObject({})).toEqual({}); |
| 16 | }); |
| 17 | |
| 18 | it('should invert key-values', () => { |
| 19 | expect( |
| 20 | invertObject({ |
| 21 | a: '3', |
| 22 | b: '4', |
| 23 | }) |
| 24 | ).toEqual({ |
| 25 | 3: 'a', |
| 26 | 4: 'b', |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | it('should take the last value when there are duplications in vals', () => { |
| 31 | expect( |
| 32 | invertObject({ |
| 33 | a: '3', |
| 34 | b: '4', |
| 35 | c: '3', |
| 36 | }) |
| 37 | ).toEqual({ |
| 38 | 4: 'b', |
| 39 | 3: 'c', |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | it('should preserve the original order', () => { |
| 44 | expect( |
| 45 | Object.keys( |
| 46 | invertObject({ |
| 47 | a: '3', |
| 48 | b: '4', |
| 49 | c: '3', |
| 50 | }) |
| 51 | ) |
| 52 | ).toEqual(['3', '4']); |
| 53 | |
| 54 | expect( |
| 55 | objectValues( |
| 56 | invertObject({ |
| 57 | a: '3', |
| 58 | b: '4', |
| 59 | c: '3', |
| 60 | }) |
| 61 | ) |
| 62 | ).toEqual(['c', 'b']); |
| 63 | }); |
| 64 | }); |