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

strange-item

v1.0.1

Published

A framework-agnostic way to bind a JS value to one or more DOM elements

Downloads

10

Readme

Strange Item

Build Status Coverage Status

Ever wanted to auto-update a DOM element every time a certain JS value is changed? But you don't want to set up Angular or React just to do that? If so, Strange Item might be for you.

Strange Item is a utility class that:

  • stores a value (preferably a string or number)
  • exposes a getter and a setter for that value
  • auto-updates a set of DOM elements whenever that value is changed

Strange Item is written in ES6 with ES6-style imports just because I don't want to have to go back and refactor when ES6 becomes mainstream. As a result, you need Node 7+ to build it. The good news is that, it generates ES5-compliant builds in AMD, CommonJS, IIFE and UMD formats. The unit tests run on the UMD version when run from the command line.

Installation

npm --save install strange-item

For Typescript

npm --save install @types/strange-item

Basic usage

Let's say you're working on a web-based game and you want a frames-per-second counter.

Include the JS library

Preferably use something like Webpack to bundle it with other libraries or with your code

<script type="text/javascript" src="/path/to/strange-item.min.js"></script>

Create a strange item to track your value

var fps = new StrangeItem();
fps.set(0)

// or 

var fps = new StrangeItem(0);

Create a DOM element to show the current value

<span id=""></span>

Bind the DOM element to the strange item

fps.bind('fps-counter')
lastTimestamp = performance.now();
requestAnimationFrame(function(timestamp) {
  var timeElapsed = timestamp - lastTimestamp;
  fps.set(1000 / timeElapsed);
})

Manual updates

Not sure why you would need this since it defeats the entire purpose of using Strange Item.

fps.autoUpdate = false
fps.update(); // updates all bound DOM elements at once

If you set autoUpdate to false then you don't need to call set either. You can just do mySI.value = newValue.

Strange Item stores (not implemented yet)

A store lets you track multiple related values from a single object and bind/unbind/update them all at once.

var playerStats = new StrangeItem.Store({
    hp: 100,
    mana: 0
}, {
    autoUpdate: false
});
playerStats.bind({
    hp: document.querySelector('#hpCounter'),
    mana: document.querySelector('#manaCounter')
});

function drinkManaPotion() {
    playerStats.set('mana', 100);
}

function getCurrentHP() {
    return playerStats.hp // playerStats.get('hp') works too
}

function receiveDamage(dmg) {
    playerStats.set('hp', getCurrentHP() - dmg);
}

function onFrameRendered() {
    playerStats.update();
}

FAQ

Why did you name this class "strange item"?

It's a reference to ADOM.

What if I need more complex functionality?

Use Angular or React.

TODO

  • Minification
  • stores
  • unbind