1.0.3 • Published 7 years ago

gonzazoid.sprintf.js v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

sprinf.js

sprintf: (pattern: string, entries: {key: string: string;}, mode?: "sloppy" | "neat" | "strict") => string;

it's nano-like template engine with two differences - I'm using /% %/ as delimiters for template keys and there is no way to pass nested properties like obj.some.prop.to.fill.some.place. Why /% %/? Because this is convenient way to use templates in js/ts code without breaking syntax. Look here:

import {sprintf} from 'gonzazoid.sprintf.js';

const generatePAC = function(_white_list: string[], proxy_host:string, checkURL: Function): string {

    const white_list = JSON.stringify(_white_list);

    function FindProxyForURL(url: string, host: string) {
        var white_list = JSON.parse('/%whiteListSource%/');
        var checkURL = /%checkURLSource%/;
        return (checkURL as any)(url, host, white_list) ? 'DIRECT' : 'HTTPS /%proxyURL%/';
    };

    const entries:{[index:string]:string} = {
        checkURLSource: checkURL.toString()
       ,whiteListSource: white_list
       ,proxyURL: proxy_host
    };

    return sprintf(FindProxyForURL.toString(), entries);
};

Pay attention to this line:

        var checkURL = /%checkURLSource%/;

as you can see template does'n break js/ts syntax because js/ts interpreter (and linter) perceives this as valid code with regular expression.

Keep in mind - if you try to pass serialized object (via JSON.stringify) quotes escaping (if it's necessary) is on your responsibility.

there are three mode:

  • sloppy (default) - replaces with entrieskey whether entry is present or not (if not - replaces with undefined)
  • neat - checks if entry is present, if not - doesn't touch
  • strict - checks if entry is present, if not - throws error

That's all! Enjoy!

P.S. as well as all my npm modules this module is strongly typed, feel free to use it with typescript.