|
1 |
| -import { keys } from '../../../utils/object' |
2 |
| -import { arrayIncludes } from '../../../utils/array' |
| 1 | +import { arrayIncludes, isArray } from '../../../utils/array' |
3 | 2 | import { isFunction } from '../../../utils/inspect'
|
| 3 | +import { clone, keys, pick } from '../../../utils/object' |
4 | 4 | import { IGNORED_FIELD_KEYS } from './constants'
|
5 | 5 |
|
6 | 6 | // Return a copy of a row after all reserved fields have been filtered out
|
7 |
| -const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) => |
8 |
| - keys(row).reduce((obj, key) => { |
9 |
| - // Ignore special fields that start with `_` |
10 |
| - // Ignore fields in the `ignoreFields` array |
11 |
| - // Include only fields in the `includeFields` array |
12 |
| - if ( |
13 |
| - !IGNORED_FIELD_KEYS[key] && |
14 |
| - !(ignoreFields && ignoreFields.length > 0 && arrayIncludes(ignoreFields, key)) && |
15 |
| - !(includeFields && includeFields.length > 0 && !arrayIncludes(includeFields, key)) |
16 |
| - ) { |
17 |
| - const f = fieldsObj[key] || {} |
18 |
| - const val = row[key] |
19 |
| - // `f.filterByFormatted` will either be a function or boolean |
20 |
| - // `f.formater` will have already been noramlized into a function ref |
21 |
| - const filterByFormatted = f.filterByFormatted |
22 |
| - const formatter = isFunction(filterByFormatted) |
23 |
| - ? /* istanbul ignore next */ filterByFormatted |
24 |
| - : filterByFormatted |
25 |
| - ? /* istanbul ignore next */ f.formatter |
26 |
| - : null |
27 |
| - obj[key] = isFunction(formatter) ? formatter(val, key, row) : val |
| 7 | +const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) => { |
| 8 | + // We first need to format the row based on the field configurations |
| 9 | + // This ensures that we add formatted values for keys that may not |
| 10 | + // exist in the row itself |
| 11 | + const formattedRow = keys(fieldsObj).reduce((result, key) => { |
| 12 | + const field = fieldsObj[key] |
| 13 | + const { filterByFormatted } = field |
| 14 | + const formatter = isFunction(filterByFormatted) |
| 15 | + ? /* istanbul ignore next */ filterByFormatted |
| 16 | + : filterByFormatted |
| 17 | + ? /* istanbul ignore next */ field.formatter |
| 18 | + : null |
| 19 | + |
| 20 | + if (isFunction(formatter)) { |
| 21 | + result[key] = formatter(row[key], key, row) |
28 | 22 | }
|
29 |
| - return obj |
30 |
| - }, {}) |
| 23 | + |
| 24 | + return result |
| 25 | + }, clone(row)) |
| 26 | + |
| 27 | + // Determine the allowed keys: |
| 28 | + // - Ignore special fields that start with `_` |
| 29 | + // - Ignore fields in the `ignoreFields` array |
| 30 | + // - Include only fields in the `includeFields` array |
| 31 | + const allowedKeys = keys(formattedRow).filter( |
| 32 | + key => |
| 33 | + !IGNORED_FIELD_KEYS[key] && |
| 34 | + !(isArray(ignoreFields) && arrayIncludes(ignoreFields, key)) && |
| 35 | + !(isArray(includeFields) && !arrayIncludes(includeFields, key)) |
| 36 | + ) |
| 37 | + |
| 38 | + return pick(formattedRow, allowedKeys) |
| 39 | +} |
31 | 40 |
|
32 | 41 | export default sanitizeRow
|
0 commit comments