英文:
How to automate functional programing, and avoid repeating myself
问题
在我的gulpfile.js中,我有以下两个函数,它们都使用gulp-nun-jucks来渲染两个不同的视图...
function genNunJucks(cb) {
return src(paths.views.src)
.pipe(nunjucksRender({
path: ['src/views/'], // 字符串或数组
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
"eol": "\n",
"indent_level": 0,
"preserve_newlines": false
}))
.pipe(dest(paths.views.dest))
cb();
}
function genNunJucks2(cb) {
return src(paths.views.src2)
.pipe(nunjucksRender({
path: ['src/views/'], // 字符串或数组
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
"eol": "\n",
"indent_level": 0,
"preserve_newlines": false
}))
.pipe(dest(paths.views.dest2))
cb();
}
在处理gulp进程的返回区域时,如何将这两个函数合并为一个执行相同任务的函数呢?你会注意到我的 path.views.src(x)
是我在重复这个过程的原因。
也许有一种循环处理的方法,可以查看完整的路径数组 paths.views.src: ['','',''] 吗?
非常感谢任何提示。
英文:
Within my gulpfile.js I have the following two functions that are making use of rendering gulp-nun-jucks for 2 different views...
function genNunJucks(cb) {
return src(paths.views.src)
.pipe(nunjucksRender({
path: ['src/views/'], // String or Array
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
"eol": "\n",
"indent_level": 0,
"preserve_newlines": false
}))
.pipe(dest(paths.views.dest))
cb();
}
function genNunJucks2(cb) {
return src(paths.views.src2)
.pipe(nunjucksRender({
path: ['src/views/'], // String or Array
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
"eol": "\n",
"indent_level": 0,
"preserve_newlines": false
}))
.pipe(dest(paths.views.dest2))
cb();
}
When it comes to working within the return area of gulp processes, how can I combine these two functions to be one function that does the same job? You will notice that my path.views.src(x)
is the reason I am doing duplicating the process.
Perhaps there is there some kind of lopping process that I can apply that looks through a full array of paths.views.src:['','','']?
Many thanks on any tips
答案1
得分: 1
You can make use of closures and a form of currying for this, and create a function that gets src
as a parameter and returns the genNunJucks
that you want to use.
function createGenNumChunksFunction(src) {
return function genNunJucks(cb) {
return src(src)
.pipe(nunjucksRender({
path: ['src/views/'], // String or Array
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
"eol": "\n",
"indent_level": 0,
"preserve_newlines": false
}))
.pipe(dest(paths.views.dest))
cb();
}
}
And instead of passing genNunJucks
at the place where you use it right now, you can pass createGenNumChunksFunction(paths.views.src)
and createGenNumChunksFunction(paths.views.src2)
.
Here is a simplified version that can be executed in the snippet and illustrates how it works:
function createGenNumChunksFunction(src) {
return function genNunJucks() {
console.log(src)
}
}
function callPassedFunction(cb) {
console.log('function will be called')
cb();
}
callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))
An additional note - which is probably not relevant for your case - if you want to use the genNunJucks
directly without the need to "create" it first, you could keep it outside of the createGenNumChunksFunction
.
function genNunJucks(src) {
console.log(src)
}
function createGenNumChunksFunction(src) {
return function() {
return genNunJucks(src)
}
}
function callPassedFunction(cb) {
console.log('function will be called')
cb();
}
callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))
genNunJucks('src3')
英文:
You can make use of closures and a form of currying for this, and create a function that gets src
as parameter and returns the genNunJucks
that you want to use:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function createGenNumChunksFunction(src) {
return function genNunJucks(cb) {
return src(src)
.pipe(nunjucksRender({
path: ['src/views/'], // String or Array
ext: '.html',
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
"eol": "\n",
"indent_level": 0,
"preserve_newlines": false
}))
.pipe(dest(paths.views.dest))
cb();
}
}
<!-- end snippet -->
And instead of passing genNunJucks
at the place where you use it right now you can pass createGenNumChunksFunction(paths.views.src)
and createGenNumChunksFunction(paths.views.src2)
Here is a simplified version that can be executed in the snipped and illustrates how it works:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function createGenNumChunksFunction(src) {
return function genNunJucks() {
console.log(src)
}
}
function callPassedFunction(cb) {
console.log('function will be called')
cb();
}
callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))
<!-- end snippet -->
An additional note - which is probably not relevant for your case - if you want to use the genNunJucks
directly without the need to "create" it first, you could keep it outside of the createGenNumChunksFunction
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function genNunJucks(src) {
console.log(src)
}
function createGenNumChunksFunction(src) {
return function() {
return genNunJucks(src)
}
}
function callPassedFunction(cb) {
console.log('function will be called')
cb();
}
callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))
genNunJucks('src3')
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论