11const { getByDot, setByDot, getItems, replaceItems } = require ( 'feathers-hooks-common' ) ;
22const sanitizeHtml = require ( 'sanitize-html' ) ;
33// const embedToAnchor = require('quill-url-embeds/dist/embed-to-anchor');
4- const { isEmpty } = require ( 'lodash' ) ;
4+ const { isEmpty, intersection } = require ( 'lodash' ) ;
55const cheerio = require ( 'cheerio' ) ;
66
77const embedToAnchor = ( content ) => {
@@ -80,7 +80,7 @@ function clean (dirty) {
8080}
8181
8282// iterate through all fields and clean the values
83- function cleanAllFields ( items , fields ) {
83+ function cleanAllFields ( items , fields , hook ) {
8484 if ( ! items ) {
8585 return items ;
8686 }
@@ -90,7 +90,7 @@ function cleanAllFields (items, fields) {
9090 items . forEach ( ( item , key ) => {
9191 items [ key ] = cleanAllFields ( items [ key ] , fields ) ;
9292 } ) ;
93- } else {
93+ } else if ( intersection ( Object . keys ( items ) , fields ) . length ) {
9494 // clean value for all fields on the single given item
9595 fields . forEach ( ( field ) => {
9696 // get item by dot notation
@@ -99,6 +99,11 @@ function cleanAllFields (items, fields) {
9999 setByDot ( items , field , clean ( value ) ) ;
100100 } ) ;
101101 }
102+
103+ if ( hook && items ) {
104+ replaceItems ( hook , items ) ;
105+ }
106+
102107 return items ;
103108}
104109
@@ -108,7 +113,7 @@ module.exports = function (options = { fields: [] }) {
108113 const isFindOrGet = [ 'find' , 'get' ] . includes ( hook . method ) ;
109114 const items = getItems ( hook ) ;
110115 if ( ! isEmpty ( items ) && ! ( isFindOrGet && hook . type === 'before' ) ) {
111- replaceItems ( hook , cleanAllFields ( items , options . fields ) ) ;
116+ cleanAllFields ( items , options . fields , hook ) ;
112117 }
113118 resolve ( hook ) ;
114119 } ) ;
0 commit comments