Skip to content

Commit 11a7392

Browse files
authored
Update sanitize-row.js
1 parent a07055f commit 11a7392

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
import identity from '../../../utils/identity'
12
import { keys } from '../../../utils/object'
2-
import { arrayIncludes } from '../../../utils/array'
3+
import { arrayIncludes, concat } from '../../../utils/array'
34
import { isArray, isFunction } from '../../../utils/inspect'
45
import { IGNORED_FIELD_KEYS } from './constants'
56

7+
// Convert object to array of ignored keys
8+
const IGNORED = keys(IGNORED_FIELD_KEYS)
9+
610
// 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) => {
11+
const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) => {
12+
const ignore = concat(IGNORED, ignoreFields).filter(identity)
13+
const include = isArray(includeFields) && includeFields.length > 0 ? includeFields : null
14+
return keys(row).reduce((obj, key) => {
915
// Ignore special fields that start with `_`
1016
// Ignore fields in the `ignoreFields` array
11-
// Include only fields in the `includeFields` array
12-
if (
13-
!IGNORED_FIELD_KEYS[key] &&
14-
!(isArray(ignoreFields) && arrayIncludes(ignoreFields, key)) &&
15-
!(isArray(includeFields) && includeFields.length > 0 && !arrayIncludes(includeFields, key))
16-
) {
17+
// Include only fields in the `includeFields` array (if present)
18+
if (!arrayIncludes(ignore, key) && !(include && !arrayIncludes(include, key))) {
1719
const f = fieldsObj[key] || {}
1820
const val = row[key]
1921
// `f.filterByFormatted` will either be a function or boolean
@@ -28,5 +30,6 @@ const sanitizeRow = (row, ignoreFields, includeFields, fieldsObj = {}) =>
2830
}
2931
return obj
3032
}, {})
33+
}
3134

3235
export default sanitizeRow

0 commit comments

Comments
 (0)