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

@devtanc/fillit

v1.0.0

Published

A library for filling templates based on key-value pairs

Downloads

7

Readme

Fillit

The fillit library allows you to create template files, read them into a class, and replace keys throughout the file by specifying key-value pairs to replace.

npm (scoped) CircleCI GitHub issues code style: prettier XO code style

Installation

npm i -S @devtanc/fillit
// or
yarn add @devtanc/fillit

Usage

folder contents

index.js
email.template.txt

email.template.txt

Hey there @recipientName,

This is @senderFirstName. Just shooting you a note to say @message.

@salutation,

@senderFirstName @senderLastName

index.js

const Template = require('@devtanc/fillit')
const path = require('path')

const emailTemplate = new Template({ path: path.resolve(__dirname, 'email.template.txt') })

emailTemplate.addPair('recipientName', 'Karl')
emailTemplate.addPair('senderFirstName', 'François')
emailTemplate.addPair('senderLastName', 'Lionet')
emailTemplate.addPair('message', 'I hope you\'re doing @howWell well')
emailTemplate.addPair('salutation', 'Hope to hear from you soon')
emailTemplate.addPair('howWell', 'fantastically')
emailTemplate.addPair(/shooting/, 'sending')


const result = emailTemplate.fill()

result

Hey there Karl,

This is François. Just sending you a note to say I hope you're doing fantastically well.

Hope to hear from you soon,

François Lionet

API

getPairs()
// Returns current Map of <key,value> pairs

getResult()
// Returns the current result of the template
// fill() will typically be called before this
// if not, then this will return the same result as getTemplate()

getTemplate()
// Returns the raw template source

getKeyPattern()
// Returns the currently set key pattern string

addPair(key, value)
addPair([key, value])
// Takes either input. Adds the given <key, value> pair to the internal Map

addPairs(Map)
// Takes in a Map and adds all entries to the internal Map

setKeyPattern(string)
// Takes in a key pattern. This string MUST contain the string 'key' at least once
// Key patterns represent a key that is surrounded by one or more other characters
// on fill(), the 'key' is first replaced with the key from the current Map item
// then the resulting string is used to find the locations in the template to place the value
// given:
// keyPattern = '!@#key&*()'
// keyvalue = ['thisismykey', 'theassociatedvalue']
//
// First: keyPattern becomes '!@#thisismykey&*()'
// Then anywhere in the template where that key is found is replaced with 'theassociatedvalue'
// The default key pattern is '@key'

fill()
fill(Map)
// Executes the filling of the template and returns the result of the fill
// If a Map is provided, then the <key,value> pairs in the Map are ADDED to the internal Map
// before the execution of the replacements

reset()
// Simply resets the current template result back to the initial template value

Important things to note

The result is modified and stored after each full <key, value> replacement is complete. This makes is so that nested replacements are possible, as shown in the email example where @howWell was found in a previous replacement, but not in the original template.

It is valid to set a RegExp as a key, which is also in the example. The RegExp is used in place of the key pattern for that round of <key,value> replacement. The g flag is set internally.