-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(b-table): make sure to apply all formatters of field configuration (
closes #5672) (#5674) * fix(b-table): make sure to apply all formatters of field configuration * Update .bundlewatch.config.json * Update .bundlewatch.config.json * Update table-filtering.spec.js
- Loading branch information
1 parent
11f8fab
commit c7c14ea
Showing
3 changed files
with
68 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
import { keys } from '../../../utils/object' | ||
import { arrayIncludes } from '../../../utils/array' | ||
import { arrayIncludes, isArray } from '../../../utils/array' | ||
import { isFunction } from '../../../utils/inspect' | ||
import { clone, keys, pick } from '../../../utils/object' | ||
import { IGNORED_FIELD_KEYS } from './constants' | ||
|
||
// Return a copy of a row after all reserved fields have been filtered out | ||
const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) => | ||
keys(row).reduce((obj, key) => { | ||
// Ignore special fields that start with `_` | ||
// Ignore fields in the `ignoreFields` array | ||
// Include only fields in the `includeFields` array | ||
if ( | ||
!IGNORED_FIELD_KEYS[key] && | ||
!(ignoreFields && ignoreFields.length > 0 && arrayIncludes(ignoreFields, key)) && | ||
!(includeFields && includeFields.length > 0 && !arrayIncludes(includeFields, key)) | ||
) { | ||
const f = fieldsObj[key] || {} | ||
const val = row[key] | ||
// `f.filterByFormatted` will either be a function or boolean | ||
// `f.formater` will have already been noramlized into a function ref | ||
const filterByFormatted = f.filterByFormatted | ||
const formatter = isFunction(filterByFormatted) | ||
? /* istanbul ignore next */ filterByFormatted | ||
: filterByFormatted | ||
? /* istanbul ignore next */ f.formatter | ||
: null | ||
obj[key] = isFunction(formatter) ? formatter(val, key, row) : val | ||
const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) => { | ||
// We first need to format the row based on the field configurations | ||
// This ensures that we add formatted values for keys that may not | ||
// exist in the row itself | ||
const formattedRow = keys(fieldsObj).reduce((result, key) => { | ||
const field = fieldsObj[key] | ||
const { filterByFormatted } = field | ||
const formatter = isFunction(filterByFormatted) | ||
? /* istanbul ignore next */ filterByFormatted | ||
: filterByFormatted | ||
? /* istanbul ignore next */ field.formatter | ||
: null | ||
|
||
if (isFunction(formatter)) { | ||
result[key] = formatter(row[key], key, row) | ||
} | ||
return obj | ||
}, {}) | ||
|
||
return result | ||
}, clone(row)) | ||
|
||
// Determine the allowed keys: | ||
// - Ignore special fields that start with `_` | ||
// - Ignore fields in the `ignoreFields` array | ||
// - Include only fields in the `includeFields` array | ||
const allowedKeys = keys(formattedRow).filter( | ||
key => | ||
!IGNORED_FIELD_KEYS[key] && | ||
!(isArray(ignoreFields) && arrayIncludes(ignoreFields, key)) && | ||
!(isArray(includeFields) && !arrayIncludes(includeFields, key)) | ||
) | ||
|
||
return pick(formattedRow, allowedKeys) | ||
} | ||
|
||
export default sanitizeRow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters