Move get-with-arrays to shared (#15332)

* Move get-with-arrays to shared, add fallback

* Just fallback to undefineds, let the usage handle fallbacks
This commit is contained in:
Rijk van Zanten
2022-08-30 17:21:07 -04:00
committed by GitHub
parent 60c5f80dd6
commit f834605a13
6 changed files with 8 additions and 6 deletions

View File

@@ -1,18 +0,0 @@
import { test, expect } from 'vitest';
import { get } from '@/utils/get-with-arrays';
test('Returns static value from object', () => {
const input = { test: { path: 'example' } };
expect(get(input, 'test.path')).toBe('example');
});
test('Returns default value if path does not exist in object', () => {
const input = { test: { path: 'example' } };
expect(get(input, 'test.wrong', 'default value')).toBe('default value');
});
test('Returns values in array path as flattened array', () => {
const input = { test: [{ path: 'example' }, { path: 'another' }] };
expect(get(input, 'test.path')).toEqual(['example', 'another']);
});

View File

@@ -1,21 +0,0 @@
/**
* Basically the same as `get` from `lodash`, but will convert nested array values to arrays, so for example:
*
* @example
* ```js
* const obj = { value: [{ example: 1 }, { example: 2 }]}
* get(obj, 'value.example');
* // => [1, 2]
* ```
*/
export function get(object: Record<string, any> | any[], path: string, defaultValue?: any): any {
const [key, ...follow] = path.split('.');
const result = Array.isArray(object) ? object.map((entry) => entry[key!]) : object?.[key!];
if (follow.length > 0) {
return get(result, follow.join('.'), defaultValue);
}
return result ?? defaultValue;
}

View File

@@ -1,7 +1,7 @@
import { useAliasFields } from '@/composables/use-alias-fields';
import { getDisplay } from '@/displays';
import { useFieldsStore } from '@/stores/fields';
import { get } from '@/utils/get-with-arrays';
import { get } from '@directus/shared/utils';
import { DisplayConfig, Field, Item } from '@directus/shared/types';
import { saveAs } from 'file-saver';
import { parse } from 'json2csv';