1
1
const { getByDot, setByDot, getItems, replaceItems } = require ( 'feathers-hooks-common' ) ;
2
2
const sanitizeHtml = require ( 'sanitize-html' ) ;
3
3
// const embedToAnchor = require('quill-url-embeds/dist/embed-to-anchor');
4
- const { isEmpty } = require ( 'lodash' ) ;
4
+ const { isEmpty, intersection } = require ( 'lodash' ) ;
5
5
const cheerio = require ( 'cheerio' ) ;
6
6
7
7
const embedToAnchor = ( content ) => {
@@ -80,7 +80,7 @@ function clean (dirty) {
80
80
}
81
81
82
82
// iterate through all fields and clean the values
83
- function cleanAllFields ( items , fields ) {
83
+ function cleanAllFields ( items , fields , hook ) {
84
84
if ( ! items ) {
85
85
return items ;
86
86
}
@@ -90,7 +90,7 @@ function cleanAllFields (items, fields) {
90
90
items . forEach ( ( item , key ) => {
91
91
items [ key ] = cleanAllFields ( items [ key ] , fields ) ;
92
92
} ) ;
93
- } else {
93
+ } else if ( intersection ( Object . keys ( items ) , fields ) . length ) {
94
94
// clean value for all fields on the single given item
95
95
fields . forEach ( ( field ) => {
96
96
// get item by dot notation
@@ -99,6 +99,11 @@ function cleanAllFields (items, fields) {
99
99
setByDot ( items , field , clean ( value ) ) ;
100
100
} ) ;
101
101
}
102
+
103
+ if ( hook && items ) {
104
+ replaceItems ( hook , items ) ;
105
+ }
106
+
102
107
return items ;
103
108
}
104
109
@@ -108,7 +113,7 @@ module.exports = function (options = { fields: [] }) {
108
113
const isFindOrGet = [ 'find' , 'get' ] . includes ( hook . method ) ;
109
114
const items = getItems ( hook ) ;
110
115
if ( ! isEmpty ( items ) && ! ( isFindOrGet && hook . type === 'before' ) ) {
111
- replaceItems ( hook , cleanAllFields ( items , options . fields ) ) ;
116
+ cleanAllFields ( items , options . fields , hook ) ;
112
117
}
113
118
resolve ( hook ) ;
114
119
} ) ;
0 commit comments