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

@nathanaelscott/madlibs

v1.1.0

Published

A component for replacing words in the dom from a JSON file

Downloads

7

Readme

Mad Libs

Mad Libs is a JavaScript library for replacing words in a dom from a JSON

Installation

Use npm to install

npm install @nathanaelscott/madlibs

JavaScript usage

import MadLibs from '@nathanaelscott/madlibs';


let options = {
    'flowers':{
        'type':'nested',
        'array':[
            {state:'VIC',flower:'Epacris impressa',size:'150mm'},
            {state:'NSW',flower:'Waratah',size:'113mm'},
            {state:'TAS',flower:'Tasmanian blue gum',size:'95mm'}
        ]
        
    },
    'animals':{
        'type':'variable',
        'biggest':'elephant',
        'smallest':'shrew'
    },
    'me':{
        'type':'single',
        'word':'Nathanael Scott'
    }
}

MadLibs(options);

DOM usage:

Accepts the camel cased string in the title attribute or with a leading # in the href attribute.


<a title="WORDflowersRANK1VALUEflower"></a> <!-- Returns: Epacris imperessa -->
<a title="WORDflowersSTATEtasVALUEsize"></a> <!-- Returns: 95mm -->

<a href="#WORDanimalsVALUEsmallest"></a> <!-- Returns: shrew -->
<a href="#WORDme"></a> <!-- Returns: Nathanael Scott -->

Returns:

<span class="madlib-word">Epacris imperessa</span>
<span class="madlib-word">95mm</span>
<span class="madlib-word">shrew</span>
<span class="madlib-word">Nathanael Scott</span>

Options

Mad Libs accepts a nested object. The object key is word the ID.

const options = {
    'flowers':{...}
}

Adding camal cased option to the name attribute starting with WORD followed by the ID replaces the element with a span containg the output

<a name="WORDflowers"></a>

type

nested

Returns values from an array of objects using it's rank (position in array) or id

'flowers':{
    'type':'nested',
    'array':[
        {state:'VIC',flower:'Epacris impressa',size:'150mm'},
        {state:'NSW',flower:'Waratah',size:'113mm'},
        {state:'TAS',flower:'Tasmanian blue gum',size:'95mm'}
    ]
}
<a name="WORDflowersRANK1VALUEflower"></a> 
<!-- Returns the value flower from first entry in the array -->

<a name="WORDflowersSTATEtasVALUEsize"></a>
<!-- Returns the value size from the entry with the state TAS -->

<a name="WORDflowersRANK2VALUEstate"></a>
<!-- Returns the long Australian state name from the 2nd entry in the array -->

Options accepts

array - An ordered array (ordered to your own critera) with either a state (uppercase appreviation) or type variable as identification

Anchor options state, type rank

value

variable Get values from the object via their names

Anchor options value

single

Return a single word from the word value