npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ricardo-ch/ric

v1.0.0

Published

Ricardo mini framework for DOM manipulation and other things

Downloads

2

Readme

ric

Ricardo mini framework for DOM manipulation and other things.

Role: provide very basic functionality related to the DOM, events, the browser and a couple of essential polyfills. Keep it simple and small.

The ric function is an instance builder or an initializer depending of what is passed as first parameter.

  ric(function() {
    // Called on DOMContentLoaded
  })

  // any string will be considered a CSS selector
  var articles = ric('#some .articles')
  // the DOM elements are available in the dom property
  console.log(articles.dom)

  // a single DOM element is also accepted
  ric(document.body).addClass('something')

  // as well as an array of DOM elements
  ric([document.body, document.querySelector('#footer')])

Ric instance methods

Once a ric instance is created, you have a list of methods you can use.

  // return true if any of the dom element contain this class name
  ric.prototype.hasClass(className)
  
  // return the indexed element of the dom array unwrapped
  ric.prototype.get(index || 0) 
  
  // within the DOM elements, find the subset of the children that satisfy the 
  // selector and return the result wrapped in a ric object
  ric.prototype.find(selector)
  // E.g.
  ric('#articles').find('a').hide()

  // self explanatory
  ric.prototype.addClass(className)
  ric.prototype.removeClass(className)
  
  // iterate over the DOM nodes
  ric.prototype.each((dom, index) => {
    console.log(dom, index)
  })

  // iterate over the dom and accumulate the returned values
  ric.prototype.reduce((dom, index) => {
    return dom.getAttribute('data-price')
  })

  // toggle between 2 classes, classB is optional
  ric.prototype.toggleClass(classA, <classB>)

  // set the textContent of the DOM elements, if no value
  // is provided the first DOM element textContent is returned
  ric.prototype.text(<value>)

  // attach event to eventHandler on the DOM nodes
  ric.prototype.on(event, eventHandler)

  // Delegate event from DOM elements onto children that matches the selector.
  // Return a list of triplets containing (dom, event, eventHandler)
  var eventList = ric.prototype.delegate(event, selector, eventHandler)
  // E.g.
  ric('#article-list').delegate('click', '[data-tracking-id]', (e) => {
    track(e.delegationTarget.getAttribute('data-tracking-id')
  })
  
  // find the closest parent that satisfy the selector or the predicate
  // return the DOM element wrapped in a ric object of undefined in case of failure
  ric.prototype.closest(selector || predicate)

  // hide the DOM elements (display:none)
  ric.prototype.hide()

  // show the DOM elements
  ric.prototype.show(display='block')

Ric Standalone Functions

Standalone methods not associated with the DOM elements. Those can be called from the ric function directly.

// similar to ric.prototype.closest but apply to the DOM paramater
ric.closest(DOMElement, predicate)

// return true if any element in the array satisfy the predicate
ric.any(iterable, predicate)

// apply func to every item, return the last func return value
ric.apply(iterable, func)

// synonym of apply
ric.each(iterable, func)

// E.g.
ric.each([1, 2, 3], (item, index) => { console.log(item, index) })
> 1, 0
> 2, 1
> 3, 2

// apply func to every item, and return the accumulation of the func return value
ric.reduce(array, func)

// return a decorated function that debounce the execution to wait time (ms)
var debounced = ric.debounce(decoratee, wait, immediate)

// How to use, first decorate the function you want to debounce for 1 second
var debounced = ric.debounce(() => { console.log('blop') }, 1000)
// let's call it 3 times
debounced(); debounced(); debounced()
// after 1 second you should get only one blop
> blop

// You can also cancel the execution at any point.
debounced.cancel()

// remove the event listeners created by ric.prototype.delegate
ric.off(eventList)

// find one element and wrap it in a ric instance
ric.one(selector).addClass('blop')

// return a decorated a function that listen to requestAnimationFrame but
// prevent that several requests exist at the same time
ric.requestAnimationFrameThrottle(decorated)

// E.g.
var throtteled = ric.requestAnimationFrameThrottle(function(){ console.log('blop') })
// let's call it 3 times
throtteled(); throtteled(); throtteled()
// when the browser is ready you should get only 1 blop
> blop