Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for transforms #38

Open
raijinsetsu opened this issue Feb 10, 2021 · 3 comments
Open

Allow for transforms #38

raijinsetsu opened this issue Feb 10, 2021 · 3 comments

Comments

@raijinsetsu
Copy link

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?

@curran
Copy link
Member

curran commented Feb 10, 2021

Yes! PRs warmly welcome, as long as they are backwards compatible and have tests & documentation in them.

Looks like a neat feature to add.

@raijinsetsu
Copy link
Author

Would you have issues if I pull in the typings, from @types/json-templates? It would mean that this package's maintainers would have to take-over the maintenance of those definitions. I know several other projects have done this to make it easier to keep the definitions in sink with the library.

@curran
Copy link
Member

curran commented Feb 11, 2021

I would rather the types live externally, please. I have zero interest in maintaining types for this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants