Javascript 正在描述两组数据之间的转换。。。a「;元变换;?
我正试图研究我脑海中酝酿的想法的不同方法,但我真的不知道如何寻找我想要做的事情 我有一系列的功能。它们都转换相同的JSON格式(模式)(使用不同的数据)。进入不同的对象(不同的模式) 例如,我可能有JSON,比如Javascript 正在描述两组数据之间的转换。。。a「;元变换;?,javascript,metaprogramming,transformation,Javascript,Metaprogramming,Transformation,我正试图研究我脑海中酝酿的想法的不同方法,但我真的不知道如何寻找我想要做的事情 我有一系列的功能。它们都转换相同的JSON格式(模式)(使用不同的数据)。进入不同的对象(不同的模式) 例如,我可能有JSON,比如 { heading: "Fogmeister", type: "person", body_text: "iOS developer", sections: [ { heading: 'Describing a transform between t
{
heading: "Fogmeister",
type: "person",
body_text: "iOS developer",
sections: [
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate..."
},
{
// other post
}
]
}
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate...",
sections: [
{
heading: null,
type: "answer",
body_text: "What you're looking for is..."
},
{
// other answer
}
]
}
我想把它转换成一个用户对象,比如
{
name: "Fogmeister",
profile: "iOS developer",
posts: [
{ title: 'Describing a transform between two sets of data... a "meta-transform"?' },
{ title: 'Other title' }
]
}
{
title: 'Describing a transform between two sets of data... a "meta-transform"?',
body: "I'm trying to investigate...",
answers: [
{ body_text: "What you're looking for is..." },
{ body_text: 'Other answer' }
]
}
但我可能有一些不同的JSON,比如
{
heading: "Fogmeister",
type: "person",
body_text: "iOS developer",
sections: [
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate..."
},
{
// other post
}
]
}
{
heading: 'Describing a transform between two sets of data... a "meta-transform"?',
type: "post",
body_text: "I'm trying to investigate...",
sections: [
{
heading: null,
type: "answer",
body_text: "What you're looking for is..."
},
{
// other answer
}
]
}
我想把它转换成一个post对象,比如
{
name: "Fogmeister",
profile: "iOS developer",
posts: [
{ title: 'Describing a transform between two sets of data... a "meta-transform"?' },
{ title: 'Other title' }
]
}
{
title: 'Describing a transform between two sets of data... a "meta-transform"?',
body: "I'm trying to investigate...",
answers: [
{ body_text: "What you're looking for is..." },
{ body_text: 'Other answer' }
]
}
希望通过这个小示例,您可以看到输入模式是相同的,但输出模式可能非常不同
我现在有不同的函数来映射不同的类型。但是我想看看我是否能想出一种方法,我可以描述输入和输出之间的映射,然后把它放到一个对象(或其他东西)中
这样,我就可以使用一个函数来使用这个映射对象来转换数据
但是。。。我不知道这是不是已经有名字了。这是一种元转换
,因为我希望能够描述转换,而不是自己进行转换
我能在谷歌上找到什么东西来提供更多关于这种编程的信息吗
我不是在寻找能做到这一点的代码。更多的只是我可以围绕主题阅读的材料,这样我就可以自己做了
谢谢听起来像是在描述一个模式传输程序,它读取模式中的内容,并从中构建一个抽象语法树。然后,您可以阅读该树来构建任意样式的新模式,允许您从同一AST描述不同的形状
本报告熟练地解释了如何使用JavaScript构建编译器。使用模式(因为它是JSON)会容易得多,因为您所要做的就是解析JSON并迭代对象,而不是读取文件中的每个字符
记住,我们的目标是构建一个可以生成干净独立的AST的东西,其他东西可以使用它。祝你好运这是一种更实用的方法,它使用对象作为模式,使用函数获取映射属性
函数转换(模式、对象){
返回Object.assign({},…)对象
.条目(对象)
.map([k,v])=>模式中的k
?图案类型[k]=“对象”
?{[pattern[k].name]:pattern[k].fn(v)}
:{[pattern[k]]:v}
: {}
)
);
}
变量模式1={
标题:“姓名”,
正文文本:“配置文件”,
节:{name:'posts',fn:array=>array.map(o=>convert({heading:'title'},o))}
},
input1={heading:“Fogmeister”,type:“person”,body_text:“iOS开发者”,sections:[{heading:“描述两组数据之间的转换…一个“元转换”?”,type:“post”,body_text:“我正在尝试调查…”,{}]};
日志(转换(模式1,输入1))代码>
。作为控制台包装{max height:100%!important;top:0;}
我很想看看是否存在这样一种映射语言/模式,尽管为了使该语言足够复杂,能够处理这些映射,它可能并不比在计算机中手动执行相同的操作复杂javascript@OliverRadini哦,我毫不怀疑这会很复杂。但希望复杂性可以存在于单个函数/对象/系统中,然后我可以传递一个“相对”简单的元转换对象,该对象将允许复杂性进行转换。如果是这样的话,我就可以相对轻松地将新转换添加到新类型中。。。希望如此。有趣的问题。我自己在谷歌上搜索了一下,得到了这个答案:。该库上的appspot演示看起来可能与您所关注的内容也有潜在的相关性:非常好,开始的地方。谢谢:D我会回来告诉你最新进展的:哇,太棒了!我喜欢使用这样的模式来映射属性。谢谢