Created
February 21, 2019 23:34
-
-
Save tzkmx/d82789cabb1c635fc10f5233ace72171 to your computer and use it in GitHub Desktop.
dropRepeats operator for mithril stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates a dependent stream that only updates when the source stream value differs from the previous | |
* @see https://github.com/spacejack/mithril-stream-extra | |
* @param function a custom function to validate equals, default is '===' check | |
* @returns The resulting dependent stream | |
* @author Jesus E. Franco Martinez <tezcatl@fedoraproject.org> 2019 | |
*/ | |
stream.dropRepeats = function dropRepeats (customEquality) { | |
var oldVal // undefined for start | |
if (typeof customEquality !== 'function') { | |
customEquality = function (oldVal, newVal) { | |
return oldVal === newVal | |
} | |
} | |
return stream.map(function (newVal) { | |
if (customEquality(oldVal, newVal)) { | |
return Stream.SKIP | |
} | |
oldVal = newVal | |
return newVal | |
}) | |
} | |
// this should be pushed as PR to mithril streams eventually |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment