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

key-list

v0.1.4

Published

Get the list of keys used in a string with interpolation

Downloads

69,499

Readme

Build Status downloads npm Code Climate Test Coverage dependencies

NPM info

key-list

Returns a list of the interpolation keys in a string. That is, if you have a string that you're using interpolation on, like

Hello, {{ member.name }}

and you call key-list with this string, you'll get back

[ 'member.name' ]

Installation

npm install key-list --save

Usage

The default interpolation is the mustache pattern ({{ someKey }}), so if you're using mustache, getting a list of keys is as easy as

var keyList = require('key-list');
var str = 'Hello, {{ member.name }}';
var keys = keyList.getKeys(str); // [ 'member.name' ]

But even if you have a different pattern, I've made it easy to use this library. If you're using a well-known interpolation pattern, there's a good chance I've already got it in this library, and you just need to call it with the correct name. And incidentally, if you're using a well-known interpolation pattern that isnt' in this library, please open an issue so I can add it. (Also, if any of the names have more canonical names, let me know. I just guessed on some.)

Here are the currently recognized pattern names:

keyList.getKeys('{{ member.name }}', 'mustache');
keyList.getKeys('{ member.name }', 'thin-mustache');
keyList.getKeys('{% member.name %}', 'glasses');
keyList.getKeys('<%= member.name %>', 'ejs');
keyList.getKeys('#{ member.name }', 'coffee');
keyList.getKeys('${ member.name }', 'es6');
keyList.getKeys(':member.name', 'express');

// I know, you probably wouldn't use these in javascript, but hey . . . they're there if you do
keyList.getKeys('@member.name', 'razor'); 
keyList.getKeys('[% member.name %]', 'perl');

Additionally, if you're using some bizarre non-standard interpolation pattern, you can pass it yourself as either a string or a regex:

// Note that double slashes are necessary for converting strings
// to regular expressions and preserving the escape
keyList.getKeys('@|foo.bar|', '@\\|\\s*([a-zA-Z0-9_\\.\\$)+]\\s*\\|');

// or

keyList.getKeys('@|foo.bar|', /@\|\s*([a-zA-Z0-9_\.\$)+]\s*\|/g);

Alternatively, if that looks a bit complicated, you can provide only the open and close portions (including any optional spacing) and key-list will provide the capture portion for you...

// Note that open and close MUST be strings, not regular expressions
keyList.open = '\\>%\\s*';
keyList.close = '\\s*%\\<';
keyList.getKeys('>% foo.bar %<');

key-list stores it's patterns on exports, so you could also just do:

keyList.open = '\\>%' + keyList.space;
keyList.close = keyList.space + '*%\\<';
keyList.getKeys('>% foo.bar %<');

NOT usage

This is not an interpolating library, lexer, or parser. You can't pass a context object and get a filled in string back. You can't include arbitrary javascript. And you will never be able to. There are plenty of existing libraries that do that. The sole purpose of this library is to show you what keys an interpolation string uses.

So . . . what ARE the use cases?

I wrote this because I could think of at least three places in other libraries I've written where it would come in handy. In two of those, it was to prompt a user for missing values (for CLI tools). In the third (incomplete), it was to determine the order that a series of interpolations should occur in (e.g. if foo.a uses bar.b via interpolation, but bar.b itself has interpolation, foo.a needs to be evaluated after bar.b). A third possible use case would be to prevent errors in _.template when keys don't exist.

_.safe

On a side note, if you find yourself writing a recursive loop to try to deal with nested keys (like "member.name"), consider using safe-obj, which provides the _.safe method specifically for accessing nested keys safely.