【问题标题】:use underscore to exclude file and directories使用下划线排除文件和目录
【发布时间】:2013-07-25 08:16:33
【问题描述】:

我有一个使用 i18next 进行网站本地化的 node.js / express 应用程序。

目前我正在从硬编码版本中提取此列表:https://github.com/TZM/tzm-blade/blob/master/app/config/apps.coffee#L105,但想通过阅读 locales 目录然后从中组成列表来制作此列表。

我想基本上将devREADME.mdconfig.json 排除在返回之外,只保留其他目录,这样我就可以返回正确的本地国家/地区,例如使用 node-cldr 库来提取根据https://github.com/papandreou/node-cldr#cldrextractlanguagedisplaynameslocaleidroot的语言显示名称:

☺  node  ruby-2.0.0-p195 master 2253522""
> var cldr = require('cldr');
undefined
> cldr.extractLanguageDisplayNames('ru').ru;
'русский'
>

等等……

到目前为止,查看 reject http://underscorejs.org/#reject 集合我不知道如何删除这些文件和目录

  fs.readdir "./locales", (err,locales) ->
    results = []
    __.reject locales, (value, index, list) ->
      console.log value, index, list
      results.push value
    console.log results

任何关于如何使用下划线中的reject 函数最好地做到这一点的建议。

【问题讨论】:

    标签: javascript node.js express coffeescript underscore.js


    【解决方案1】:

    我对coffeescript不太熟悉,但这是您想要的(可能需要修复,因为我不知道coffeescript):

    EXCLUDE = [ 'dev', 'README.md', 'config.json' ]
    results = __.reject locales, (value, index, list) ->
      return EXCLUDE.indexOf(value) == -1
    

    _.reject 返回迭代器为其返回 true 的任何项目的新列表。在这种情况下,如果项目不在 EXCLUDE 列表中 (indexOf === -1),则函数返回 true,并且该项目是它返回的列表的一部分。

    【讨论】:

    • 谢谢,太好了,我只需要将 return EXCLUDE.indexOf(value) == -1 更改为 return EXCLUDE.indexOf(value) != -1 这样它就会列出所有其他目录。
    猜你喜欢
    • 2020-05-19
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多