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

selectify

v1.1.0

Published

generic array for manipulating styles and attributes

Downloads

17

Readme

selectify

Generic array for manipulating styles and attributes. Heavily inspired by the wonderful selections from d3, but works with more generic objects, not just DOM elements! Includes CSS-oriented methods like style and classed, but can easily be extended to add custom methods. And the input is just an array, so initial selection can be handled elsewhere, and return this object for further manipulation. As an example, see it used for manipulating 3D scenes in gl-scene.

js-standard-style

install

Add to your project with

npm install selectify --save

example

Create a selection from an array

var selectify = require('selectify')
var selection = selectify([{id: 'apple', className: 'fruit'}, {id: 'orange', className: 'fruit'}])

you can now set styles

selection.style({color: 'rgb(255, 0, 0)'})

or classes

selection.classed('cirtrus', function (d) {return d.id === 'orange'})

and evaluate functions

selection.each(function (d) {
	console.log('id: ' + d.id)
	console.log('class: ' + d.className)
	console.log('style: ' + JSON.stringify(d.style))
})

which in this example will return

id: apple
style: {"color":"rgb(255, 0, 0)"}
class: fruit
id: orange
style: {"color":"rgb(255, 0, 0)"}
class: fruit citrus

see below for all included methods, or how to extend with your own!

methods

selection.each(function)

Evaluates a function on each element. The first input to the function will be the element, the second optional input will be the index.

selection.style(name[,value])

Set a style on each element by updating the style property. Can provide either a single object argument selection.style({name: value}) or two arguments selection.style(name, value). Can also provide a function for value, which will be evaluated on each item to determine the property value. If the function returns undefined, the property will be removed.

selection.attr(name[,value])

Set an attribute on each element by updating the attributes property. Otherwise works identically to style.

selection.classed(name[,value])

Set one or more classes on each element by updating the className property. Can provide a single class in the form name or a list of multiple classes specified by spaces in the form name1 name2 name3. If value is truthy it will determine whether to add or remove the class. If not provided, it will return a list of whether or not each element of the selection has the specified class.

selection.toggleClass(name)

Add one or more classes (if not already present), or remove them (if already present).

selection.select(selector)

Return a sub selection matching a string specifier of the form #id or .class. A string without a tag will be treated as an id. Will return the first element that matches. If no matching items, will return null.

selection.selectAll(selector)

Return a sub selection matching a string specifier of the form #id or .class. A string without a tag will be treated as an id. Will return all elements that match. If no matching items, will return null.

extending

You may want to extend selectify with your own custom methods. It's easy to do with inherits. Here's an example. First, the usual boilerplate

var selectify = require('selectify')
var inherits = require('inherits')

inherits(myselectify, selectify)

function myselectify (items) {
  if (!(this instanceof myselectify)) return new myselectify(items)
  myselectify.super_.call(this, items)
}

Then add a method log

myselectify.prototype.log = function () {
  return this.each(function (d) {
    console.log(d.id)
  })
}

If we now create a selection we can use our new method

var selection = myselectify([{id: 'apple'}, {id: 'orange'}])
selection.log()

to get

apple
orange