如何自动化函数式编程,并避免重复。

huangapple go评论93阅读模式
英文:

How to automate functional programing, and avoid repeating myself

问题

在我的gulpfile.js中,我有以下两个函数,它们都使用gulp-nun-jucks来渲染两个不同的视图...

  1. function genNunJucks(cb) {
  2. return src(paths.views.src)
  3. .pipe(nunjucksRender({
  4. path: ['src/views/'], // 字符串或数组
  5. ext: '.html',
  6. inheritExtension: false,
  7. envOptions: {
  8. watch: true
  9. },
  10. manageEnv: manageEnvironment,
  11. loaders: null
  12. }))
  13. .pipe(htmlbeautify({
  14. indentSize: 2,
  15. "eol": "\n",
  16. "indent_level": 0,
  17. "preserve_newlines": false
  18. }))
  19. .pipe(dest(paths.views.dest))
  20. cb();
  21. }
  22. function genNunJucks2(cb) {
  23. return src(paths.views.src2)
  24. .pipe(nunjucksRender({
  25. path: ['src/views/'], // 字符串或数组
  26. ext: '.html',
  27. inheritExtension: false,
  28. envOptions: {
  29. watch: true
  30. },
  31. manageEnv: manageEnvironment,
  32. loaders: null
  33. }))
  34. .pipe(htmlbeautify({
  35. indentSize: 2,
  36. "eol": "\n",
  37. "indent_level": 0,
  38. "preserve_newlines": false
  39. }))
  40. .pipe(dest(paths.views.dest2))
  41. cb();
  42. }

在处理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...

  1. function genNunJucks(cb) {
  2. return src(paths.views.src)
  3. .pipe(nunjucksRender({
  4. path: ['src/views/'], // String or Array
  5. ext: '.html',
  6. inheritExtension: false,
  7. envOptions: {
  8. watch: true
  9. },
  10. manageEnv: manageEnvironment,
  11. loaders: null
  12. }))
  13. .pipe(htmlbeautify({
  14. indentSize: 2,
  15. "eol": "\n",
  16. "indent_level": 0,
  17. "preserve_newlines": false
  18. }))
  19. .pipe(dest(paths.views.dest))
  20. cb();
  21. }
  22. function genNunJucks2(cb) {
  23. return src(paths.views.src2)
  24. .pipe(nunjucksRender({
  25. path: ['src/views/'], // String or Array
  26. ext: '.html',
  27. inheritExtension: false,
  28. envOptions: {
  29. watch: true
  30. },
  31. manageEnv: manageEnvironment,
  32. loaders: null
  33. }))
  34. .pipe(htmlbeautify({
  35. indentSize: 2,
  36. "eol": "\n",
  37. "indent_level": 0,
  38. "preserve_newlines": false
  39. }))
  40. .pipe(dest(paths.views.dest2))
  41. cb();
  42. }

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.

  1. function createGenNumChunksFunction(src) {
  2. return function genNunJucks(cb) {
  3. return src(src)
  4. .pipe(nunjucksRender({
  5. path: ['src/views/'], // String or Array
  6. ext: '.html',
  7. inheritExtension: false,
  8. envOptions: {
  9. watch: true
  10. },
  11. manageEnv: manageEnvironment,
  12. loaders: null
  13. }))
  14. .pipe(htmlbeautify({
  15. indentSize: 2,
  16. "eol": "\n",
  17. "indent_level": 0,
  18. "preserve_newlines": false
  19. }))
  20. .pipe(dest(paths.views.dest))
  21. cb();
  22. }
  23. }

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:

  1. function createGenNumChunksFunction(src) {
  2. return function genNunJucks() {
  3. console.log(src)
  4. }
  5. }
  6. function callPassedFunction(cb) {
  7. console.log('function will be called')
  8. cb();
  9. }
  10. callPassedFunction(createGenNumChunksFunction('src1'))
  11. 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.

  1. function genNunJucks(src) {
  2. console.log(src)
  3. }
  4. function createGenNumChunksFunction(src) {
  5. return function() {
  6. return genNunJucks(src)
  7. }
  8. }
  9. function callPassedFunction(cb) {
  10. console.log('function will be called')
  11. cb();
  12. }
  13. callPassedFunction(createGenNumChunksFunction('src1'))
  14. callPassedFunction(createGenNumChunksFunction('src2'))
  15. 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 -->

  1. function createGenNumChunksFunction(src) {
  2. return function genNunJucks(cb) {
  3. return src(src)
  4. .pipe(nunjucksRender({
  5. path: [&#39;src/views/&#39;], // String or Array
  6. ext: &#39;.html&#39;,
  7. inheritExtension: false,
  8. envOptions: {
  9. watch: true
  10. },
  11. manageEnv: manageEnvironment,
  12. loaders: null
  13. }))
  14. .pipe(htmlbeautify({
  15. indentSize: 2,
  16. &quot;eol&quot;: &quot;\n&quot;,
  17. &quot;indent_level&quot;: 0,
  18. &quot;preserve_newlines&quot;: false
  19. }))
  20. .pipe(dest(paths.views.dest))
  21. cb();
  22. }
  23. }

<!-- 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 -->

  1. function createGenNumChunksFunction(src) {
  2. return function genNunJucks() {
  3. console.log(src)
  4. }
  5. }
  6. function callPassedFunction(cb) {
  7. console.log(&#39;function will be called&#39;)
  8. cb();
  9. }
  10. callPassedFunction(createGenNumChunksFunction(&#39;src1&#39;))
  11. callPassedFunction(createGenNumChunksFunction(&#39;src2&#39;))

<!-- 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 -->

  1. function genNunJucks(src) {
  2. console.log(src)
  3. }
  4. function createGenNumChunksFunction(src) {
  5. return function() {
  6. return genNunJucks(src)
  7. }
  8. }
  9. function callPassedFunction(cb) {
  10. console.log(&#39;function will be called&#39;)
  11. cb();
  12. }
  13. callPassedFunction(createGenNumChunksFunction(&#39;src1&#39;))
  14. callPassedFunction(createGenNumChunksFunction(&#39;src2&#39;))
  15. genNunJucks(&#39;src3&#39;)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月5日 05:09:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定