mirror of
https://github.com/directus/directus.git
synced 2026-01-13 21:38:08 -05:00
* fix cohersion of values only in rest * fix tests * Update late-pans-draw.md * default back to keep old behavior * fmt * combined mapValuesDeep into deepMap and cleaned function up. * ran formatter * Update .changeset/fine-boats-brush.md --------- Co-authored-by: Brainslug <br41nslug@users.noreply.github.com> Co-authored-by: Brainslug <tim@brainslug.nl> Co-authored-by: daedalus <44623501+ComfortablyCoding@users.noreply.github.com>
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { expect, test } from 'vitest';
|
|
import { deepMap } from './deep-map.js';
|
|
|
|
for (const value of [123, 'abc', null, undefined, true, false, new Date()]) {
|
|
test(`primitive value ${value}`, () => {
|
|
const result = deepMap(value, () => 'unreachable');
|
|
|
|
expect(result).toEqual(value);
|
|
});
|
|
}
|
|
|
|
test('keep deep complex objects', () => {
|
|
const now = new Date();
|
|
const result = deepMap({ key: now }, (val) => val);
|
|
expect(result).toEqual({ key: now });
|
|
});
|
|
|
|
test('array mapping to string', () => {
|
|
const result = deepMap([123, 'abc', true, [123, 'abc', true]], (val) => String(val));
|
|
expect(result).toEqual(['123', 'abc', 'true', ['123', 'abc', 'true']]);
|
|
});
|
|
|
|
test('object mapping to string', () => {
|
|
const result = deepMap({ a: 'abc', b: 123, c: true, d: { 1: 'abc', 2: 123, 3: true } }, (val) => String(val));
|
|
expect(result).toEqual({ a: 'abc', b: '123', c: 'true', d: { 1: 'abc', 2: '123', 3: 'true' } });
|
|
});
|
|
|
|
test('turning key into value', () => {
|
|
const result = deepMap({ a: 1, b: [undefined, true, 'whatever'], c: { d: 'deep' } }, (_, key) => key);
|
|
expect(result).toEqual({ a: 'a', b: [0, 1, 2], c: { d: 'd' } });
|
|
});
|
|
|
|
test("don't mutate original object", () => {
|
|
const instance = { a: 123 };
|
|
const result = deepMap(instance, (value) => value + 1);
|
|
expect(result === instance).toBeFalsy();
|
|
expect(result).toEqual({ a: 124 });
|
|
});
|
|
|
|
test("don't mutate original array", () => {
|
|
const instance = [123];
|
|
const result = deepMap(instance, (value) => value + 1);
|
|
expect(result === instance).toBeFalsy();
|
|
expect(result).toEqual([124]);
|
|
});
|