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

filly

v1.0.1

Published

Small, extensible, Angular-style string templating

Downloads

22

Readme

npm version

filly

Small, extensible, Angular-style string templating

Features

  • Angular-style pipes and params
    • Built-in substr and slice pipes
    • Optionally add your own custom pipes
  • Support multiple brace depths
  • Set default options (presets)
  • Zero runtime dependencies

Usage

Basic Fill:

filly('Hello {foo}, I am {bar|slice:1:-1}!', { foo: 'friend', bar: '"filly"' });
// Output: 'Hello friend, I am filly!'

Custom presets:

const myfilly = filly({
	pipes: {
    lower: (val) => val.toLowerCase(),
    upper: (val) => val.toUpperCase(),
  },
});

myfilly('{foo|upper} - {bar|lower}', { foo: 'big', bar: 'SMALL' });
// Output: 'BIG - small'

See API for more details and examples.

API

filly

filly(template: string, source: object | Function, options?: Options): string;

filly(options: Options): (template: string, source: object | Function, options?: Options) => string;

Name | Type | Description --- | --- | --- template | string | The template to fill source | object | Function | The source object or callback from which to obtain values. If an object is passed, nested properties are supported. See SourceFunc for info on passing a callback function. options | object | Configurable options for the fill function. See Options

Options

Name | Type | Default value | Description --- | --- | --- | --- pipes | object | {} | A string-keyed object containing callbacks to be used for custom pipes. Pipes take the form (value: string, ...params: string[]) => string transform | Function | undefined | An additional transformation to perform on values after any pipes have executed. See TransformFunc for details. notFoundValue | string | Function | '' | The value to insert when the placeholder key is not found. If source is a function, this option is ignored. A callback function may also be supplied which takes the same form as SourceFunc

SourceFunc

(key: string, info: { pipe: string, params: string[], depth: number }) => string

Name | Description --- | --- key | The name (first part) of the placeholder info | Additional information about the placeholder info.pipe | The name of the pipe in the placeholder, if any info.params | A copy of the additional parameters to be passed to the pipe, if any info.depth | The number of braces used to enclose the placeholder. See also Gotcha: Asymmetric Braces

Example:

Template:  '{{nickName|foo:bar:baz}}'
Values passed to SourceFunc:
  ('nickName', { pipe: 'foo', params: ['bar', 'baz'], depth: 2 })

TransformFunc

(value: string, info?: { key: string, pipe: string, params: string[], depth: number }) => string

Name | Description --- | --- value | The current value of the placeholder info | Additional information about the placeholder info.key | The name (first part) of the placeholder info.pipe | The name of the pipe in the placeholder, if any info.params | A copy of the additional parameters to be passed to the pipe, if any info.depth | The number of braces used to enclose the placeholder. See also Gotcha: Asymmetric Braces

Gotchas

Asymmetric Braces

If the number of braces on one side of a placeholder is different from the other (such as due to a typographical error), the depth will be the lower of the two numbers. However, all braces will still be removed from both sides of the placeholder upon replacement.

Example | Depth --- | --- {foo}} | 1 {{{{bar}}} | 3

filly('This placeholder has a depth of {{foo}}}', (key, info) => {
  return String(info.depth);
});
// Output: 'This placeholder has a depth of 2'
// Note that all 3 braces were removed from the right side.

Params are immutable

The info.params arrays passed to source and tranform callbacks are copies of the array used internally by filly, so changes to their values will not affect other callbacks, pipes, or the final replacement value, making the params effectively immutable from the perspective of the user. This was done to ward against unintended side effects.

Contributing

Contributions and feedback are welcome and appreciated! Please visit the GitHub repo to submit an issue or pull request.

Recognition

Thanks to the Angular Team and sindresorhus (author of pupa), whose work contributed greatly to the inspiration of filly.