HOO (Higher-order operator) append #6971
voliva
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Inspired by my answer in a stackoverflow question, I came up with
append
, an operator enhancer (or HOO🦉 if you will) that keeps the original value of the source. Source posted in my gist.I think it could be useful for those cases where you want to map/switchMap/etc. the value to something else, but you need to keep in the original value for later use.
To use it, you just wrap any existing operator with it:
And it also has a perk! you can chain as many
append
as you want, and it won't build up nested arrays:And do not worry, if your original value was an array, it will nest it as if it was a regular value (so it won't return a flat array with your values in it + the one returned by the operator):
(It uses a
symbol
to flag what arrays are appendable and which ones aren't. Another possibility would be to use a WeakSet instead).And, of course, full typescript support. It uses tuple types, so you can have different types and it will work when destructuring those tuples.
Beta Was this translation helpful? Give feedback.
All reactions