Inspired by this post, that's about getting data from your dev.to dashboard using the browser's devtools. I'm going to do the same but with transducers.
The original code transforms NodeList to an array in order to use map and filter, but we don't need to do that.
If you're trying this, make sure you have all the utility functions in scope.
// Common utilitiesvarensure_number=(value)=>Number.isNaN(value)?0:value;varget_text=(element)=>(query)=>element.querySelector(query).innerText;// Useful callbacksvarget_data=function(story){consttext=get_text(story);return{published:newDate(story.querySelector('time').dateTime),reactions:Number(text('[title="Reactions"]')),comments:Number(text('[title="Comments"]')),views:Number(text('[title="Views"]')),};};varpublished_2020=function(story){returnstory.published.getFullYear()===2020;}varadd_stats=function(state,value){return{reactions:state.reactions+value.reactions,comments:state.comments+value.comments,views:state.views+ensure_number(value.views)};};// Transducer stuffvarstats={reactions:0,comments:0,views:0,};vartransducer=compose(map(get_data),filter(published_2020));varstories=document.querySelectorAll('.dashboard-story:not(.story-unpublished)');transduce(add_stats,stats,transducer,stories);
Inspired by this post, that's about getting data from your dev.to dashboard using the browser's devtools. I'm going to do the same but with transducers.
The original code transforms NodeList to an array in order to use
map
andfilter
, but we don't need to do that.If you're trying this, make sure you have all the utility functions in scope.