You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@IlyaSemenov could there also be a clean.skip method? I sometimes want to allow empty arrays for certain values. A skip method should allow me to define the values that I don't ant to be included in the clean function.
Hmm... Please elaborate with the sample imaginary input, desired output and desired API syntax?
Filtering out values in an array is a reasonable use case, but I'm not sure about the particular API.
At the moment you can do that with either a custom cumulative cleaner:
constcleanArrayEvenValues=clean.array({element: clean.integer(),// ensure all elements are integersclean: values=>values.filter(value=>value%2===0)// only keep even values})cleanArrayEvenValues([1,2,3,4,5,6,7]).then(res=>console.log(res))// [2,4,6]
or you can delegate the decision to element cleaner, return undefined for unwanted values, and filter them out in the cumulative cleaner:
constcleanArrayEvenValues=clean.array({element: clean.integer({clean: value=>value%2===0 ? value : undefined,// return undefined for unwanted values}),clean: values=>values.filter(value=>value!==undefined)// skip undefined elements})cleanArrayEvenValues([1,2,3,4,5,6,7]).then(res=>console.log(res))// [2,4,6]
this part clean: values => values.filter(value => value !== undefined) seems to be useful in many circumstances and could be extracted to a helper option like clean.array({ skipUndefined: true }).
Thoughts?
The text was updated successfully, but these errors were encountered:
Re: #9 (comment) by @tomanagle:
Hmm... Please elaborate with the sample imaginary input, desired output and desired API syntax?
Filtering out values in an array is a reasonable use case, but I'm not sure about the particular API.
At the moment you can do that with either a custom cumulative cleaner:
or you can delegate the decision to element cleaner, return
undefined
for unwanted values, and filter them out in the cumulative cleaner:this part
clean: values => values.filter(value => value !== undefined)
seems to be useful in many circumstances and could be extracted to a helper option likeclean.array({ skipUndefined: true })
.Thoughts?
The text was updated successfully, but these errors were encountered: