Skip to content

Allow for transforms #38

Description

@raijinsetsu

Great library!

I am using it to generate log output according to the installation's configuration. We deploy to various environments and how they parse/understand the log entries differs.

Here is what we are doing:

const template = parse({
    severity: '{{ucLogLevel}}'
});

const result = template({
                logLevel: level,
                lcLogLevel: () => level.toLowerCase(),
                ucLogLevel: () => level.toUpperCase(),
            })

The issue is that the actual template is provided by configuration and we need to provide each of the variables in a bunch of possible styles: strings in uppercase, lowercase, and, possibly, title case; numbers might need rounding; dates might need manipulation (ie. as a unix timestamp vs. ISO 8601 timestamp vs. Windows timestamp, etc.).

I would NOT suggest allowing code to be embedded inside the template as that would pose a security risk unless you added a lot of complexity (ie. using a library to sanitize the code so that only a subset of features could be used).

However, the parse function COULD accept a list of transforms.

I would propose the following, given by example:

const template = parse({
    severity: '{{logLevel:default->toUpperCase}}'
},{
    string: {
        toUpperCase: (val) => val.toUpperCase(),
        toLowerCase: (val) => val.toLowerCase()
    },
    number: {
       round: (val) => Math.round(val)
    }
});
// template.keys => typical stuff
// template.transforms => the second parameter passed to parse

const result = template({logLevel: level})

If a PR were submitted for this, would it be accepted?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions