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

wring-js

v1.0.0

Published

Dead Simple string formatter for javascript chatBots

Downloads

1

Readme

Wring.js

forthebadge
A Dead simple string formating and templating utility for working with YAML string collections. Working with Probot or any other chat bots? Want to store all the strings and need an efficient way to format them with user data? This is a simple solution to make the work easier.

Usage

Install Wring.js in your project

$ npm install wring-js

Import / require the library in your file and create an instance.

const { Wring } = require('wring-js');
const wring = new Wring();

To format a single string with data

let message = wring.format('Hello {{user}}', { user: 'haxzie' });
// results in: Hello haxzie

Working with collections

Wring formats the strings using the delimitter {{ }} you can specify the identifier inside it to format from the string based on the object's keys you have passed. Here's an example yaml file:

# my_strings.yml

welcome: |
  Welcome, {{user}}!
congratulate: |
  Hey {{user}}, congratulations on your {{number}} PR for {{repo}}!
waywo: |
  This is {{user}} and I am currently working on {{wring}}
user: 
    sayHello: |
        Hello {{user}}
    sayGoodMorning: |
        Good Morning {{user}}

Create a new collection of string from the YAML file.
new Collection() takes the absolute path of the YAML file to be loaded and returns a Collection.
eg:

import { Collection } from 'wring-js';
let myCollection = new Collection('/user/home/project/my_strings.yml');

If you only have the relative path of the file, you can achieve this by requiring the path module or simply pass the __dirname as second argument for the function.

import { Collection } from 'wring-js';

// simply pass the __dirname as the second argument
let myCollection = new Collection('path/to/my_strings.yml', __dirname);

// or you can do it yourself by using the path module
const path = require('path');
let myCollection = new Collection(path.join(__dirname, 'path/to/my_strings.yml'));

Using existing data

Or Create a collection with existing JSON object with key value pairs.

let myCollection = new Collection().load(<YOUR_JSON_OBJECT>);

Fetching and Formatting

Pick a string and format it using an object with key value pairs.

let welcomeString  = myCollection.with('welcome').format({ user: 'haxzie' });
// this generates -> Welcome, haxzie!

Pick a string inside a sub collection

let helloString = myCollection.from('user').with('sayHello').format({ user: 'haxzie' });
// this generates -> Hello haxzie

Custom delimiters

Wring Collection supports Unary and Binary delimiters for formating the strings. By default the formatter uses {{ }} as the delimiter. To use your own, simply pass the delmiter as a string to the format() method.

Using Binary delimiters

Separate the starting and ending delimiter with a space where the identifier resides. Eg. If you want to use ${key} as the delimiter in your string as Hello ${user}! pass the delimiter as ${ } to the format() method's second argument;

let message = myCollection.with('welcome').format({ user: 'haxzie' }, '${ }');

Using unary delimiters

To use a unary delimiter, simply pass the starting delimiter as a string to the format() method's second argument. Eg. if you want to use just $ as the delimiter to specify the substitution strings in the message as $user.

let message = myCollection.with('welcome').format({ user: 'haxzie' }, '$');

Testing

Wring uses Jest for testing and ts-jest for typescript preprocessing.

$ npm run test

Contributing

Open and issue and discuss the changes with a maintainer. Submit PRs only for issues that's been discussed. Feel free to submit PRs to fix typos and documentation without opening an issue :heart:

TODO

  • [X] Enable sub collections
  • [X] Manually load data into collection
  • [ ] Strict checking of given yaml file
  • [ ] Support for JSON files

License

MIT License © Musthaq Ahamad 2018-19