|
1 | 1 | import {
|
2 | 2 | defaults, filter, is, eq, not, pattern, val, isInjectable
|
3 | 3 | } from "../src/index";
|
4 |
| -import { pick } from '../src/common/common'; |
| 4 | +import { map, mapObj, pick } from '../src/common/common'; |
5 | 5 |
|
6 | 6 | describe('common', function() {
|
7 | 7 | describe('filter', function() {
|
@@ -126,4 +126,40 @@ describe('common', function() {
|
126 | 126 | expect(pick(obj, ['baz'])).toEqual({ });
|
127 | 127 | });
|
128 | 128 | });
|
| 129 | + |
| 130 | + describe('map', () => { |
| 131 | + it('should map arrays', () => { |
| 132 | + const src = [1, 2, 3, 4]; |
| 133 | + const dest = map(src, x => x * 2); |
| 134 | + |
| 135 | + expect(src).toEqual([1, 2, 3, 4]); |
| 136 | + expect(dest).toEqual([2, 4, 6, 8]); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should map arrays in place when target === src', () => { |
| 140 | + const src = [1, 2, 3, 4]; |
| 141 | + const dest = map(src, x => x * 2, src); |
| 142 | + |
| 143 | + expect(src).toEqual([2, 4, 6, 8]); |
| 144 | + expect(dest).toEqual([2, 4, 6, 8]); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('mapObj', () => { |
| 149 | + it('should map objects', () => { |
| 150 | + const src = { foo: 1, bar: 2, baz: 3 }; |
| 151 | + const dest = mapObj(src, x => x * 2); |
| 152 | + |
| 153 | + expect(src).toEqual({ foo: 1, bar: 2, baz: 3 }); |
| 154 | + expect(dest).toEqual({ foo: 2, bar: 4, baz: 6 }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should map objects in place when target === src', () => { |
| 158 | + const src = { foo: 1, bar: 2, baz: 3 }; |
| 159 | + const dest = mapObj(src, x => x * 2, src); |
| 160 | + |
| 161 | + expect(src).toEqual({ foo: 2, bar: 4, baz: 6 }); |
| 162 | + expect(dest).toEqual({ foo: 2, bar: 4, baz: 6 }); |
| 163 | + }); |
| 164 | + }); |
129 | 165 | });
|
0 commit comments