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

wyld

v1.1.0

Published

Wyld is a lightweight Javascript library that allows you to easily create input-output mappings.

Downloads

5

Readme

Wyld

Wyld is a lightweight Javascript library that allows you to easily create input-output mappings.

This library is named after James Wyld.

Table of Contents

Installation

You can use any of the following Javascript package managers to install Wyld.

npm install wyld

bower install wyld

yarn add wyld

Usage

Use Wyld.Map in conjunction with a configuration object to create your map.

var map = Wyld.Map({
  // map configuration goes here
});

When you want to retrieve an output for a given input using the map, call the get method.

var output = map.get(input);

Types of Mappings

Direct

To create a direct mapping from an exact input to an exact output, use a key-value assignment.

var map = Wyld.Map({
  5: 10
});
map.get(5); // returns 10

Duplicate input keys will result in the last one being used.

var map = Wyld.Map({
  5: 10,
  5: 50
});
map.get(5); // returns 50

Conditional

To map multiple inputs to a single output based on whether or not the input satisfies a condition, use keys in quotes and include the relevant operator.

var map = Wyld.Map({
  '>10': 10,
  '<5': 5
});
map.get(50); // returns 10
map.get(2); // returns 5

The conditions supported are greater than >, less than <, greater than or equal to >=, and less than or equal to <=.

There are two ways to evaluate multiple conditions at once. The first is to use a single & operator to separate conditions.

var map = Wyld.Map({
  '>5&<10': 20,
});
map.get(7); // returns 20

The second way is to put a dollar sign $ in the key to represent the input and surround it with the conditions.

var map = Wyld.Map({
  '5<$<10': 20,
});
map.get(7); // returns 20

The string 5<$<10 reads if five is less than the input and the input is less than ten.

Calculated

To have an output be a calculated operation on the input, just map to a callback function.

var map = Wyld.Map({
  '>5': function(num){
    return num/2;
  }
});
map.get(5); // returns 5
map.get(6); // returns 3
map.get(20); // returns 10

Universal

Using a quote-surrounded * as a key will act as a universal mapping. If it is the only rule in the map, then all inputs will map to its output. If there are other rules in the map, then placing it at the beginning will act as a default in the case that no other mappings match, and placing it at the end will override all other rules.

var map = Wyld.Map({
  '*': 10,
  5: 15
});
map.get(5); // returns 15
map.get(-50); // returns 10
map.get(22); // returns 10

Collisions

It is possible to create a map with incompatible or overlapping rules.

var map = Wyld.Map({
  '>5': 10,
  '<10': 1,

  '>20': 100,
  '>50': 500
});
map.get(7); // returns 1
map.get(70); // returns 500

When rules conflict or overlap, the last one declared that matches the given input overrides all previous rules.

The Future

This tool is in its infancy and welcomes advice, suggestions, and contributions. If you have any ideas for how to make it better, don't hesitate to submit a feature request!