Skip to content
/ strif Public
forked from loggin-js/strif

πŸ“‡ Format strings easily

Notifications You must be signed in to change notification settings

floki-gh/strif

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“‡ strif

NPM version Downloads
Dependencies Known Vulnerabilities NPM quality

Format strings easily

Features

  • βœ”οΈŽ Simple
  • βœ”οΈŽ Expandable/Configurable
  • βœ”οΈŽ Type Checking
  • βœ”οΈŽ No Dependencies

Overview

const formatter = strif.create({
  transformers: {
    date: s => new Date(s),
    lds:  d => d.toLocaleString()
  },
  plugins: [
    '.tests/plugins/strif-color.js'
  ]
});

const template =
  formatter
    .template('{time} {user} {message}')
    .prop('time', { transformers: [`date`, `lds`, `blue`] })
    .prop('user', { accessor: 'user.name', transformers: [`gray`] })
    .prop('message', { type: 'string' });

console.log(template.compile(data));
// will output: <%b1970-1-1 04:07:03> <%grBob> This is a super long message

Table Of Content

Installation

Install from npm:

$ npm install strif

Importing

With require:

const strif = require('strif');

With ES6 import:

import strif from 'strif';

In the browser:

<script src="node_modules/strif/dist/strif.dist.js"></script>

! NOTICE: Plugins currently don't work in browser, woking on it. PRs Welcome

Usage

Using in Node

Using strif is actually pretty easy, you can use the default formatter under strif

let template = strif.template('{time} {user} {message}');
template.compile(data);

or create a custom one by using strif.create(opts), you can pass a set of transformers and plugins and other options

const formatter = strif.create({
  transformers: {
    date: s => new Date(s),
    lds:  d => d.toLocaleString()
  }
});

let template = formatter
  .template('{time} {user} {message}')
  .prop('time', { transformers: [`date`] });

template.compile({
  time: 11223322,
  message: 'This is a super long message ',
  user: { name: 'Bob' }
});

Using in Browser

Using strif in the browser is as simple as in node, just import the script strif/dist/strif.dist.js

<html lang="en">
  <head>
    <script src="node_modules/strif/dist/strif.dist.js"></script>
  </head>
  <body>
    <script>
      strif.create(); // strif is available
    </script>
  </body>
</html>

! NOTICE: Plugins currently don't work in browser, woking on it. PRs Welcome

Api

strif

Exported members from strif.

interface strif {
  create(opts: strif.StrifOptions): void;
  Formatter: strif.Formatter;
}

strif.Formatter

interface strif.Formatter {
  constructor(opts: strif.FormatterOptions);
  template(template: string, options: strif.TemplateOptions): strif.Template;
  fromFile(path: string, options: strif.TemplateOptions): strif.Template;
}

strif.Template

interface strif.Template {
  constructor(template: string, transformers: { [key: string]: (v) => v }, options: strif.TemplateOptions);
  prop(name: string, options: strif.PropOptions): this;
  print(): void;
  compile(data: object, options: { ignoreTransformers: string[] }): string;
}

strif.Prop

interface strif.Prop {
  constructor(name, opts: strif.PropOptions);
  getFromObject(obj: object): any;
}

strif.PropOptions

interface strif.PropOptions {
  accessor: string;
  type: string;
  transformers: string[];
}

strif.TemplateOptions

interface strif.TemplateOptions {
  props: strif.StrifProp[];
}

strif.FormatterOptions

interface strif.FormatterOptions {
  transformers: { [key: string]: (v) => v };
  plugins: string[]; 
}

Transformers

Transformers are functions that are be used to process some segment of the template,
they will receive a value and they must also return a value, here are some example:

{
  transformers: {
    date: s => new Date(s),
    lds:  d => d.toLocaleString()
  }
}

Plugins

I added a little bit of plugin support, what a plugin actually is, is an object (for now) wich contains transformers (also for now), and will be attached to any template generated by that transformer. Here are some example:

const chalk = require('chalk');
module.exports = {
  transformers: {
    blue:  s => chalk.blue(s),
    gray:  s => chalk.gray(s),
    green: s => chalk.green(s),
  }
};

Check this demo for another example.

Found a bug or have a feature request

If you found a bug or have a feature request please dont hesitate on leaving a issue

Contributing

If you would like to collaborate please check CONTRIBUTING.md for more details.

Considerations

This project was in some way inspired by @davidchambers/string-format, at least in the sense of the transformers concept.

About

πŸ“‡ Format strings easily

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 89.6%
  • HTML 10.4%