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

lacona-util-phrasehelpers

v0.1.0

Published

Helper functions for building lacona phrases

Downloads

6

Readme

lacona-util

#OUTDATED

Utility functions for building lacona phrases.

##substrings(inputString)

Returns an array of all substrings from the beginning: [inputString[0], inputString[0..1], inputString[0..2]...]

###Why to use it

When a value is used, the method specified by its compute property will be called for every possible input, and it is expected to call data with every possible string that this value is capable of handling. However, most of the time, it does not handle the full input, but just a portion of it.

Take this very basic phrase, for example.

{
	scope: {
		integer0to99: function (inputString, data, done) {
			var intRegex = /^[1-9]?\d$/;
			if (inputString.match(intRegex)) {
				data({
					display: inputString,
					value: parseInt(inputString)
				});
			}
			done();
		}
	},

	schema: {
		name: 'bottlesOfBeer',
		root: {
			type: 'value',
			compute: 'integer0to99'
		}
	}
};

We have a simple phrase which just matches a simple regex, and converts the value to an integer. It should match any non-negative 1 or 2-digit number.

  • 0 :ok:
  • 9 :ok:
  • 42 :ok:
  • 03 :no_entry_sign:

We could use it very easily with a sentence that only contains the bottlesOfBeer phrase.

However, we run into problems when this phrase is used in sequence with other phrases. Let's look at this sentence:

{
	schema: {
		name: 'bottlesGame',
		root: [
			{
				type: 'bottlesOfBeer'
			},
			'bottles of beer on the wall'
		],
		sentence: true
	}
}

Remember that an Array is shorthand for a sequence and a string is shorthand for a literal.

Let's say the user inputs the string '99 bottles of beer on the wall'. Lacona will attempt to parse this string with the bottlesGame sentence. The sequence will send the entire string to its first child: bottlesOfBeer. bottlesOfBeer will call integer0to99, still with the full string.

integer0to99 will observe that /^[1-9]?\d$/ does not match the string '99 bottles of beer on the wall', and it will not send any data.

The solution is to attempt to parse every substring individually, and you can use substrings.

Let's modify our code to use substrings.

integer0to99: function (inputString, data, done) {
	var intRegex = /^[1-9]?\d$/;
	var substrings = laconaUtil.substrings(inputString);
	var substring;
	var l = substrings.length;
	var i;

	for (i = 0; i < l; i++) {
		substring = substrings[i];
		if (.match(intRegex)) {
			data({
				display: substring,
				value: parseInt(substring)
			});
		}
	}
	done();
}

In the example that we described, substrings will return an array that looks like this:

[
	'9',
	'99',
	'99 ',
	'99 b',
	'99 bo',
	...
]

integer0to99 will call data for the only two inputs that match the regex: '9' and '99'. The sequence will consume those strings and attempt to match the literal value - ' bottles of beer on the wall'. Only the second option will match, just as we desired.