jstransformer icon indicating copy to clipboard operation
jstransformer copied to clipboard

Add To/From JSON method

Open RobLoach opened this issue 8 years ago • 1 comments

For each transformer, render() takes an input string, and outputs a string. There are cases, however, where we want to deal with straight objects, like TOML/INI/YAML/etc.

Proposal

We should add toJSON/fromJSON method to each Transformer, similar to render(), but outputs an object rather rather than a string.

Example

This converts an INI string to YAML:

var ini = require('jstransformer')(require('jstransformer-ini'));
var toml = require('jstransformer')(require('jstransformer-yaml'));

var input = 'my_value=5';
var data = ini.toJSON(input);
var output = yaml.fromJSON(data);
//=> "my_value: 5"

Given this, we would be able to easily convert any format to and from any other format without having to parse through JSON.stringify all the time.

RobLoach avatar Nov 14 '15 19:11 RobLoach

I agree that the API seems useful and interesting. I feel like it might be confusing for the JSON transformer. Perhaps the methods should be toObject and fromObject or perhaps even parse and stringify such that the JSON object would be automagically compatible.

One thought, can we figure out a good way to handle blocks of metadata at the top of files e.g.

title="about metadata"
author= "Forbes Lindesay"
---
h1 Metadata
p This article is in jade, but has some TOML metadata at the beginning

Ideally it would be good to be able to do something like:

  • Get Metadata: file |> read metadata |> toml.parse |> json.stringify
  • Get Content: file |> remove metadata |> jade.render

Question 1: Are there lots of different ways of representing metadata as part of a file with other content? Could we supply a unified API on top of them?

Question 2: Do these two new APIs (meta data reading and removing + to/from object) belong as part of jstransformers? I'm beginning to think maybe they do, because we could envision providing parse methods for things like css, jade etc. alongside also providing a render method, but my gut reaction was initially no.

ForbesLindesay avatar Nov 19 '15 19:11 ForbesLindesay