Returns GetMapReturn<Tinput, Toptions, null, null>
the same type as the input value, but with the nested items within it filtered out.
- Objects, Arrays, Maps, Sets etc are copied and returned as new values, with filtered items.
- Iterators & Generators (sync or async) are returned as a Generator/Iterator with their items filtered out.
isSingle
values are returned as copies.
Filter over the (many) items of a value and returning of a new instance of the value, containing the elements that pass the filter with
isPass
(an elaborate kind of truthy). Value can beObject
,Array
,Map
,Iterators
and many more.Similar idea to lodash
_.filter()
orArray.filter()
but powered byz.loop()
. Hence:it works with many value types, not just
Array
but alsoMap
,Set
,Object
, Iterators, Generators, AsyncIterators, Functions and optionallyisPrimitive
/isSingle
s!BUT the twist is, it's not returning always just an
Array
like a normalArray.filter()
or lodash_.filter()
, but a copy of whatever the input value is, with the nested items that pass thefilterCb
function. So afilter(new Map([]))
will return a new Map, with the filtered keys / values of the original Map instance and so on.Using
props: 'all'
you can also copy over the props of the original value, like with_.mapValues()
, but also for all object types.In effect:
Arrays (or sparse arrays) can be filter-copied verbatim but dense by default: the filtered items will disappear in the new array (like
Array.filter()
/_.filter()
. But ifsparse: true
it will be respecting sparse items position, as well as the filtered array elements, which will be at the same indexes of the original. The resulting array will be sparse.Objects are copied over like with
_.mapValues()
, respecting their props, including symbols ones (optionally), unlike_.mapValues()
which deals only with string keys.Generators & AsyncGenerators are also supported, and the result is a new Generator / AsyncGenerator with the mapped items.
NOTE: When
props: 'all'
, the Generator's props are also copied over to the new Generator, all exceptnext
,Symbol.iterator
&Symbol.asyncIterator
(since these would mess the iteration). Also instrict: true
, you can't usesymbol: true
(viaprops
) with Iterators/Generators (it throws).Other/Single Values
All of these currently are optionally supported by
z.filter
(withfilterSingles: true
):isSingle
isPrimitive
but it makes little sense to 'filter' on them. When boxed value is filtered out, the result will be an instance with undefined / / false etc, which it's not great. If
filterSingles: false
(default) it throws an error @todo: unlessprops: "only"
is used in which case the props are filtered out, into an "empty" value?Powered by the mighty
z.loop()
/z.keys
, so and hence you can control which keys / idx are visited (own / inherited / enumerable / string / symbol etc) viaIloopOptions
.z.isSingle
values are also supported: the projection callback will be called only once (withoptions.strict: false
- default) or throw an error (withoptions.strict: true
). This follows the functional programming principles, that all values are enclosed and can be mapped over. This is also the reason whyoptions.strict: false
is the default.You can pass an
IloopOptions
object, to control which keys / idx are visited (own / inherited / enumerable etc):options.filter
- if it already exists, it throws an error.options.map
oroptions.take
exists, it is applied after the filter passes.