├── test ├── fixtures │ ├── aaa.hbs │ ├── bbb.hbs │ ├── ccc.hbs │ ├── items │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── each-items-as │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── each-items │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── items-inline │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── each-items-sortBy │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── each-sortItems │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── each-items-filter-function │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── each-items-filter-string │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── groupBy │ │ ├── aaa.hbs │ │ ├── bbb.hbs │ │ ├── ccc.hbs │ │ └── index.hbs │ ├── index.hbs │ ├── items-inverse-inline │ │ └── index.hbs │ ├── order.hbs │ ├── hash.hbs │ └── items-inverse │ │ └── index.hbs └── test.js ├── .gitattributes ├── .travis.yml ├── .verb.md ├── .editorconfig ├── .gitignore ├── LICENSE ├── package.json ├── .github └── contributing.md ├── .eslintrc.json ├── README.md └── index.js /test/fixtures/aaa.hbs: -------------------------------------------------------------------------------- 1 | this is foo -------------------------------------------------------------------------------- /test/fixtures/bbb.hbs: -------------------------------------------------------------------------------- 1 | this is baz -------------------------------------------------------------------------------- /test/fixtures/ccc.hbs: -------------------------------------------------------------------------------- 1 | this is ccc -------------------------------------------------------------------------------- /test/fixtures/items/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/items/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/items/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-as/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-as/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-as/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/items-inline/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/items-inline/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/items-inline/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-sortBy/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-sortBy/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-sortBy/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items/index.hbs: -------------------------------------------------------------------------------- 1 | {{#eachItems "posts"}} 2 | {{stem}} 3 | {{/eachItems}} -------------------------------------------------------------------------------- /test/fixtures/each-sortItems/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-sortItems/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-sortItems/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/items-inline/index.hbs: -------------------------------------------------------------------------------- 1 | {{#each (items "posts")}} 2 | {{stem}} 3 | {{/each}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-function/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-function/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-function/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-string/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-string/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-string/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | --- 4 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/groupBy/aaa.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: zzz 3 | tags: ['a', 'b'] 4 | --- 5 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/groupBy/bbb.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: aaa 3 | tags: ['b', 'c'] 4 | --- 5 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/groupBy/ccc.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | title: ggg 3 | tags: ['c', 'd'] 4 | --- 5 | this is {{title}} -------------------------------------------------------------------------------- /test/fixtures/index.hbs: -------------------------------------------------------------------------------- 1 | {{#each (sortItems (items "pages")) as |item|}} 2 | {{item.stem}} 3 | {{/each}} -------------------------------------------------------------------------------- /test/fixtures/items/index.hbs: -------------------------------------------------------------------------------- 1 | {{#items "posts"}} 2 | {{#each .}} 3 | {{stem}} 4 | {{/each}} 5 | {{/items}} -------------------------------------------------------------------------------- /test/fixtures/each-items-sortBy/index.hbs: -------------------------------------------------------------------------------- 1 | {{#eachItems "posts" sortBy="data.title"}} 2 | {{stem}} 3 | {{/eachItems}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-string/index.hbs: -------------------------------------------------------------------------------- 1 | {{#eachItems "posts" filter="!index"}} 2 | {{stem}} 3 | {{/eachItems}} 4 | -------------------------------------------------------------------------------- /test/fixtures/items-inverse-inline/index.hbs: -------------------------------------------------------------------------------- 1 | {{#each (items "docs")}} 2 | {{stem}} 3 | {{^}} 4 | No docs yet. 5 | {{/each}} -------------------------------------------------------------------------------- /test/fixtures/each-items-filter-function/index.hbs: -------------------------------------------------------------------------------- 1 | {{#eachItems "posts" filter=(matchView "!index")}} 2 | {{stem}} 3 | {{/eachItems}} -------------------------------------------------------------------------------- /test/fixtures/order.hbs: -------------------------------------------------------------------------------- 1 | {{#pages}} 2 | {{#each (sortItems items "baz,foo") as |item|}} 3 | {{item.stem}} 4 | {{/each}} 5 | {{/pages}} -------------------------------------------------------------------------------- /test/fixtures/each-items-as/index.hbs: -------------------------------------------------------------------------------- 1 | {{#eachItems "posts" sortBy="data.title" filter="!index" as |foo|}} 2 | {{foo.stem}} 3 | {{/eachItems}} -------------------------------------------------------------------------------- /test/fixtures/hash.hbs: -------------------------------------------------------------------------------- 1 | {{#pages}} 2 | {{#each (sortItems items order="baz,bar") as |item|}} 3 | {{item.stem}} 4 | {{/each}} 5 | {{/pages}} -------------------------------------------------------------------------------- /test/fixtures/items-inverse/index.hbs: -------------------------------------------------------------------------------- 1 | {{#items "docs"}} 2 | {{#each .}} 3 | {{stem}} 4 | {{/each}} 5 | {{^}} 6 | No docs yet. 7 | {{/items}} -------------------------------------------------------------------------------- /test/fixtures/each-sortItems/index.hbs: -------------------------------------------------------------------------------- 1 | --- 2 | order: ['bbb', 'ccc', 'aaa'] 3 | --- 4 | {{#each (sortItems (items "pages") order) as |item|}} 5 | {{item.basename}} 6 | {{/each}} -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '6' 9 | - '5' 10 | - '4.7.0' 11 | matrix: 12 | allow_failures: 13 | - node_js: '4.7.0' 14 | -------------------------------------------------------------------------------- /test/fixtures/groupBy/index.hbs: -------------------------------------------------------------------------------- 1 | {{#groupBy "posts" "data.tags" filter="!index"}} 2 |

Tags

3 | {{#each . as |tags|}} 4 | {{log tags}} 5 |

{{!Tags}}

6 | {{#each tags as |tag|}} 7 | {{/each}} 8 | {{/each}} 9 | {{/groupBy}} -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var assembleHelpers = require('{%= name %}'); 5 | var assemble = require('assemble'); 6 | var app = assemble(); 7 | 8 | app.use(assembleHelpers()); 9 | ``` 10 | 11 | ## Helpers 12 | {%= apidocs("index.js") %} 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "assemble-helpers", 3 | "description": "Plugin that adds helpers for assemble projects. Includes both sync and async helpers that take advantage of assemble's powerful features.", 4 | "version": "1.0.1", 5 | "homepage": "https://github.com/assemble/assemble-helpers", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "assemble/assemble-helpers", 8 | "bugs": { 9 | "url": "https://github.com/assemble/assemble-helpers/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=4.7.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "get-value": "^2.0.6", 24 | "handlebars-helper-each": "^0.1.2", 25 | "handlebars-utils": "^0.2.1", 26 | "helper-link-to": "^0.2.0", 27 | "helper-sort-items": "^0.1.0", 28 | "isobject": "^3.0.0", 29 | "match-file": "^0.2.2", 30 | "relative-dest": "^0.1.0" 31 | }, 32 | "devDependencies": { 33 | "assemble": "^0.23.0", 34 | "async-each-series": "^1.1.0", 35 | "gulp-format-md": "^0.1.11", 36 | "mocha": "^3.2.0" 37 | }, 38 | "keywords": [ 39 | "assemble", 40 | "boilerplate", 41 | "build", 42 | "cli", 43 | "cli-app", 44 | "command-line", 45 | "create", 46 | "dev", 47 | "development", 48 | "framework", 49 | "front", 50 | "frontend", 51 | "helpers", 52 | "plugin", 53 | "project", 54 | "projects", 55 | "scaffold", 56 | "scaffolder", 57 | "scaffolding", 58 | "template", 59 | "templates", 60 | "webapp", 61 | "yeoman", 62 | "yo" 63 | ], 64 | "verb": { 65 | "toc": false, 66 | "layout": "default", 67 | "tasks": [ 68 | "readme" 69 | ], 70 | "plugins": [ 71 | "gulp-format-md" 72 | ], 73 | "related": { 74 | "list": [ 75 | "handlebars-helpers", 76 | "template-helpers", 77 | "helper-link-to" 78 | ] 79 | }, 80 | "lint": { 81 | "reflinks": true 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /.github/contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to assemble-helpers 2 | 3 | First and foremost, thank you! We appreciate that you want to contribute to assemble-helpers, your time is valuable, and your contributions mean a lot to us. 4 | 5 | **What does "contributing" mean?** 6 | 7 | Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following: 8 | 9 | - Updating or correcting documentation 10 | - Feature requests 11 | - Bug reports 12 | 13 | If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information. 14 | 15 | **Showing support for assemble-helpers** 16 | 17 | Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use. 18 | 19 | Don't have time to contribute? No worries, here are some other ways to show your support for assemble-helpers: 20 | 21 | - star the [project](https://github.com/jonschlinkert/assemble-helpers) 22 | - tweet your support for assemble-helpers 23 | 24 | ## Issues 25 | 26 | ### Before creating an issue 27 | 28 | Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues. 29 | 30 | Try to follow these guidelines 31 | 32 | - **Investigate the issue**: 33 | - **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue. 34 | - Create the issue in the appropriate repository. 35 | 36 | ### Creating an issue 37 | 38 | Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue: 39 | 40 | - **version**: please note the version of assemble-helpers are you using 41 | - **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using 42 | - **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/) 43 | 44 | ## Above and beyond 45 | 46 | Here are some tips for creating idiomatic issues. Taking just a little bit extra time will make your issue easier to read, easier to resolve, more likely to be found by others who have the same or similar issue in the future. 47 | 48 | - read the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) 49 | - take some time to learn basic markdown. This [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601) is super helpful, as is the GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/). 50 | - Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/). 51 | - use backticks to wrap code. This ensures that code will retain its format, making it much more readable to others 52 | - use syntax highlighting by adding the correct language name after the first "code fence" 53 | 54 | 55 | [node-glob]: https://github.com/isaacs/node-glob 56 | [micromatch]: https://github.com/jonschlinkert/micromatch 57 | [so]: http://stackoverflow.com/questions/tagged/assemble-helpers -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # assemble-helpers [![NPM version](https://img.shields.io/npm/v/assemble-helpers.svg?style=flat)](https://www.npmjs.com/package/assemble-helpers) [![NPM monthly downloads](https://img.shields.io/npm/dm/assemble-helpers.svg?style=flat)](https://npmjs.org/package/assemble-helpers) [![NPM total downloads](https://img.shields.io/npm/dt/assemble-helpers.svg?style=flat)](https://npmjs.org/package/assemble-helpers) [![Linux Build Status](https://img.shields.io/travis/assemble/assemble-helpers.svg?style=flat&label=Travis)](https://travis-ci.org/assemble/assemble-helpers) 2 | 3 | > Plugin that adds helpers for assemble projects. Includes both sync and async helpers that take advantage of assemble's powerful features. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save assemble-helpers 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var assembleHelpers = require('assemble-helpers'); 17 | var assemble = require('assemble'); 18 | var app = assemble(); 19 | 20 | app.use(assembleHelpers()); 21 | ``` 22 | 23 | ## Helpers 24 | 25 | ### [{{content}}](index.js#L30) 26 | 27 | Returns the stringified contents from a `view` object, or just returns the value if `view` is a string. 28 | 29 | **Params** 30 | 31 | * `view` **{String|Object}** 32 | * `returns` **{String}**: Returns `view.contents` or the value if it's a string. 33 | 34 | **Example** 35 | 36 | ```js 37 | app.view('foo', {contents: 'This is contents'}); 38 | 39 | // {{contents foo}} 40 | //=> "This is contents" 41 | ``` 42 | 43 | ### [{{find}}](index.js#L62) 44 | 45 | Get the first view with the given `name` from any collection, or the specified `collection`. If no collection is passed only `renderable` collections will be searched ("renderable" collections are any collections not specified as `partials` and `layouts`). 46 | 47 | **Params** 48 | 49 | * `name` **{String}**: The name/key (`view.stem`, `view.basename`, `view.relative` or `view.path`) of the view to find. It's good practice to use a longer path when practical. 50 | * `collection` **{String}**: (optional) 51 | * `returns` **{Object|undefined}**: Returns the view if found, or `undefined` if not. 52 | 53 | **Example** 54 | 55 | ```handlebars 56 | {{find "foo"}} 57 | {{find "foo.hbs" }} 58 | {{find "a/b/c/foo.hbs"}} 59 | 60 | 61 | {{find "foo" "pages"}} 62 | {{find "foo.hbs" "posts"}} 63 | {{find "a/b/c/foo.hbs" "partials"}} 64 | ``` 65 | 66 | ### [{{getView}}](index.js#L81) 67 | 68 | Get a view from the specified `collection`. 69 | 70 | **Params** 71 | 72 | * `collection` **{String}**: (required) 73 | * `name` **{String}** 74 | * `returns` **{Object}** 75 | 76 | **Example** 77 | 78 | ```handlebars 79 | {{getView "pages" "foo"}} 80 | {{getView "pages" "foo.hbs" }} 81 | {{getView "pages" "a/b/c/foo.hbs"}} 82 | ``` 83 | 84 | ### [{{config}}](index.js#L110) 85 | 86 | Get the value of `prop` from `app.options` or the context. Dot-notation may be used. Context values from `app.cache.data` will override values from `app.options`, and values from locals will override all other values. 87 | 88 | **Params** 89 | 90 | * `prop` **{String}** 91 | * `locals` **{Object}**: (optional) Locals to merge onto the options. 92 | * `returns` **{any}** 93 | 94 | **Example** 95 | 96 | ```js 97 | app.option({a: {b: 'c'}, x: 'z'}); 98 | app.data({a: {b: 'd'}}); 99 | app.data({foo: {a: {b: 'ABC'}}}); 100 | 101 | // {{config "x"}} => 'z' 102 | // {{config "a.b.c"}} => 'eee' 103 | // {{config "a.b"}} => '{c: 'eee'}' 104 | 105 | // with locals 106 | // {{config foo "a.b.c"}} => 'ABC' 107 | // {{config foo "a.b"}} => '{c: 'ABC'}' 108 | ``` 109 | 110 | ### [{{option}}](index.js#L143) 111 | 112 | Return the value of `prop` from `app.options`. 113 | 114 | **Params** 115 | 116 | * `prop` **{String}** 117 | * `returns` **{any}** 118 | 119 | **Example** 120 | 121 | ```js 122 | app.option({a: {b: 'c'}}); 123 | 124 | // {{option "a"}} => '{b: 'c'}' 125 | // {{option "a.b"}} => 'c' 126 | ``` 127 | 128 | ### [{{enabled}}](index.js#L168) 129 | 130 | Return true if the value of `prop` on `app.options` is strictly `true. 131 | 132 | **Params** 133 | 134 | * `prop` **{String}** 135 | * `returns` **{Boolean}** 136 | 137 | **Example** 138 | 139 | ```js 140 | app.option('foo', false); 141 | app.option('bar', true); 142 | app.enable('baz'); //<= convenience method for setting an option to true 143 | 144 | // {{enabled "foo"}} => false 145 | // {{enabled "bar"}} => true 146 | // {{enabled "baz"}} => true 147 | ``` 148 | 149 | ### [{{disabled}}](index.js#L193) 150 | 151 | Return the given value of `prop` from `this.options`. 152 | 153 | **Params** 154 | 155 | * `prop` **{String}** 156 | * `returns` **{any}** 157 | 158 | **Example** 159 | 160 | ```js 161 | app.option('foo', false); 162 | app.option('bar', true); 163 | app.disable('baz'); //<= convenience method for setting an option to false 164 | 165 | // {{disabled "foo"}} => true 166 | // {{disabled "bar"}} => false 167 | // {{disabled "baz"}} => false 168 | ``` 169 | 170 | ### [{{matchView}}](index.js#L215) 171 | 172 | Returns a function for matching a view in subexpressions. The returned function takes a 173 | 174 | **Params** 175 | 176 | * `prop` **{String}** 177 | * `returns` **{any}** 178 | 179 | **Example** 180 | 181 | ```handlebars 182 | {{#eachItems "posts" filter=(matchView "!index")}} 183 | {{stem}} 184 | {{/eachItems}} 185 | ``` 186 | 187 | ### [{{items}}](index.js#L235) 188 | 189 | Returns an array of items from the views in collection `name`. 190 | 191 | **Params** 192 | 193 | * `name` **{String|Object|Array}**: Collection name, or collection/list instance, or array of list items. 194 | * `options` **{Object}** 195 | * `returns` **{Array}** 196 | 197 | **Example** 198 | 199 | ```handlebars 200 | {{#items "posts"}} 201 | {{#each .}} 202 | 203 | {{titleize data.title}} 204 | {{/each}} 205 | {{/items}} 206 | ``` 207 | 208 | ### [{{eachItems}}](index.js#L291) 209 | 210 | Block helper that iterates over the items in collection `name` and exposes each item in the collection as "this" inside the block. 211 | 212 | **Hash options**: 213 | 214 | * `sortBy`: function or property path to sort the collection by. Dot-notation may be used for getting nested properties. 215 | * `filter`: function, glob pattern, or filepath for filtering items in the collection. 216 | 217 | **Params** 218 | 219 | * `name` **{String}**: (required) Collection name 220 | * `locals` **{Object}**: (optional) 221 | * `options` **{Object}**: Handlebars options 222 | * `returns` **{Array}** 223 | 224 | **Example** 225 | 226 | ```js 227 | // built-in "pages" collection 228 | app.pages('templates/pages/*.hbs'); 229 | // {{#eachItems "pages" filter="!index"}} 230 | // {{log stem}} 231 | // {{/eachItems}} 232 | 233 | // custom "posts" collection 234 | app.create('posts'); //<= create the collection first 235 | app.posts('templates/posts/*.hbs'); 236 | // {{#eachItems "posts" sortBy="data.date"}} 237 | // {{log stem}} 238 | // {{/eachItems}} 239 | ``` 240 | 241 | ### [{{sortItems}}](index.js#L353) 242 | 243 | Sort and filter the items in a collection to match the order of an array of strings. Given you have three pages, `aaa.hbs`, `bbb.hbs`, and `ccc.hbs`, the following will sort the items in the order specified in front-matter: 244 | 245 | **Params** 246 | 247 | * `items` **{Array}**: (required) Array of items from a collection 248 | * `locals` **{Object}**: (optional) 249 | * `options` **{Object}**: Handlebars options 250 | * `returns` **{Array}** 251 | 252 | **Example** 253 | 254 | ```handlebars 255 | --- 256 | order: ['bbb', 'ccc', 'aaa'] 257 | --- 258 | 259 | {{#each (sortItems (items "pages") order) as |item|}} 260 | {{item.basename}} 261 | {{/each}} 262 | 263 | 264 | {{#pages}} 265 | {{#each (sortItems items order) as |item|}} 266 | {{item.basename}} 267 | {{/each}} 268 | {{/pages}} 269 | ``` 270 | 271 | ### [{{link-to}}](index.js#L373) 272 | 273 | Returns a relative path from the view being rendered to destination path of the specified view. 274 | 275 | **Params** 276 | 277 | * `path` **{String}** 278 | * `collection` **{String}**: (optional) Collection name 279 | * `lookups` **{Array}**: (optional) Array of property paths to use for resolving views. 280 | * `returns` **{String}**: Relative path to the specified view. 281 | 282 | **Example** 283 | 284 | ```handlebars 285 | 286 | {{link-to "index"}} 287 | 288 | {{link-to "index" "posts"}} 289 | ``` 290 | 291 | ### [{{render}}](index.js#L388) 292 | 293 | Render the given view. 294 | 295 | **Params** 296 | 297 | * `view` **{Object|String}**: View object or the name of the view to render. 298 | * `returns` **{String}** 299 | 300 | **Example** 301 | 302 | ```handlebars 303 | {{render "foo.hbs"}} 304 | ``` 305 | 306 | ### [{{assets}}](index.js#L438) 307 | 308 | Returns the relative path to the `assets` directory defined on `app.options` or the context. Alias for the [asset](#asset) helper, to provide a semantic alternative depending on usage. 309 | 310 | **Params** 311 | 312 | * `filepath` **{String}**: (optional) Filepath to append to the assets path. 313 | * `returns` **{String}** 314 | 315 | **Example** 316 | 317 | ```handlebars 318 | 319 | 320 | ``` 321 | 322 | ### [{{asset}}](index.js#L458) 323 | 324 | Returns the relative path to `filename` from the `assets` directory defined on `app.options` or the context. Alias for the [assets](#assets) helper, to provide a semantic alternative depending on usage. 325 | 326 | **Params** 327 | 328 | * `filepath` **{String}**: (optional) Filepath to append to the asset path. 329 | * `returns` **{String}** 330 | 331 | **Example** 332 | 333 | ```handlebars 334 | 335 | 336 | ``` 337 | 338 | ### [{{root}}](index.js#L476) 339 | 340 | Returns the relative path to `filename` from either the `root` or `dest` directory defined on `app.options` or the context. 341 | 342 | **Params** 343 | 344 | * `filepath` **{String}**: (optional) Filepath to append to the root path. 345 | * `returns` **{String}** 346 | 347 | **Example** 348 | 349 | ```handlebars 350 | 351 | 352 | ``` 353 | 354 | ## About 355 | 356 | ### Related projects 357 | 358 | * [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/assemble/handlebars-helpers) | [homepage](https://github.com/assemble/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.") 359 | * [helper-link-to](https://www.npmjs.com/package/helper-link-to): Templates helper that returns a link path from the current view to the another view. | [homepage](https://github.com/helpers/helper-link-to "Templates helper that returns a link path from the current view to the another view.") 360 | * [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.") 361 | 362 | ### Contributing 363 | 364 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 365 | 366 | Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards. 367 | 368 | ### Building docs 369 | 370 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 371 | 372 | To generate the readme, run the following command: 373 | 374 | ```sh 375 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 376 | ``` 377 | 378 | ### Running tests 379 | 380 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 381 | 382 | ```sh 383 | $ npm install && npm test 384 | ``` 385 | 386 | ### Author 387 | 388 | **Jon Schlinkert** 389 | 390 | * [github/jonschlinkert](https://github.com/jonschlinkert) 391 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 392 | 393 | ### License 394 | 395 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 396 | Released under the [MIT License](LICENSE). 397 | 398 | *** 399 | 400 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 24, 2017._ -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var series = require('async-each-series'); 7 | var assemble = require('assemble'); 8 | var helpers = require('..'); 9 | var app; 10 | 11 | var cwd = path.join.bind(path, __dirname); 12 | var fixtures = path.join.bind(path, cwd('fixtures')); 13 | 14 | function renderAssert(view, expected, locals, cb) { 15 | if (typeof locals === 'function') { 16 | cb = locals; 17 | locals = {}; 18 | } 19 | 20 | app.render(view, locals, function(err, res) { 21 | if (err) { 22 | cb(err); 23 | return; 24 | } 25 | assert.equal(res.content.trim(), expected); 26 | cb(); 27 | }); 28 | } 29 | 30 | function assertEach(views, expected, cb) { 31 | var idx = -1; 32 | series(views, function(view, next) { 33 | renderAssert(view, expected[++idx], next); 34 | }, cb); 35 | } 36 | 37 | describe('assemble-helpers >', function() { 38 | beforeEach(function() { 39 | app = assemble(); 40 | app.use(helpers()); 41 | app.pages(fixtures('*.hbs')); 42 | }); 43 | 44 | describe('helpers', function() { 45 | it('should export a function', function() { 46 | assert.equal(typeof helpers, 'function'); 47 | }); 48 | 49 | it('should register helpers', function() { 50 | assert.equal(typeof app._.helpers.async.render, 'function'); 51 | assert.equal(typeof app._.helpers.sync.asset, 'function'); 52 | assert.equal(typeof app._.helpers.sync.assets, 'function'); 53 | assert.equal(typeof app._.helpers.sync.config, 'function'); 54 | assert.equal(typeof app._.helpers.sync.disabled, 'function'); 55 | assert.equal(typeof app._.helpers.sync.eachItems, 'function'); 56 | assert.equal(typeof app._.helpers.sync.enabled, 'function'); 57 | assert.equal(typeof app._.helpers.sync.items, 'function'); 58 | assert.equal(typeof app._.helpers.sync.linkTo, 'function'); 59 | assert.equal(typeof app._.helpers.sync.matchView, 'function'); 60 | assert.equal(typeof app._.helpers.sync.option, 'function'); 61 | assert.equal(typeof app._.helpers.sync.root, 'function'); 62 | assert.equal(typeof app._.helpers.sync.sortItems, 'function'); 63 | assert.equal(typeof app._.helpers.sync['link-to'], 'function'); 64 | }); 65 | }); 66 | 67 | describe('content:', function() { 68 | it('should return the given string', function(cb) { 69 | app.pages('bar.hbs', {content: '{{content "foo"}}'}); 70 | renderAssert('bar.hbs', 'foo', cb); 71 | }); 72 | 73 | it('should return the contents from a view', function(cb) { 74 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 75 | app.pages('bar.hbs', {content: '{{content (page "foo.hbs")}}'}); 76 | renderAssert('bar.hbs', 'this is foo', cb); 77 | }); 78 | }); 79 | 80 | describe('find:', function() { 81 | it('should find a view from any view collection by view.stem', function(cb) { 82 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 83 | app.pages('bar.hbs', {content: '{{content (find "foo")}}'}); 84 | renderAssert('bar.hbs', 'this is foo', cb); 85 | }); 86 | 87 | it('should find a view from any view collection by view.basename', function(cb) { 88 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 89 | app.pages('bar.hbs', {content: '{{content (find "foo.hbs")}}'}); 90 | renderAssert('bar.hbs', 'this is foo', cb); 91 | }); 92 | 93 | it('should find a view from any view collection by view.path', function(cb) { 94 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 95 | app.pages('bar.hbs', {content: '{{content (find "a/b/c/foo.hbs")}}'}); 96 | renderAssert('bar.hbs', 'this is foo', cb); 97 | }); 98 | }); 99 | 100 | describe('getView:', function() { 101 | it('should get a view from the given view collection by view.stem', function(cb) { 102 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 103 | app.pages('bar.hbs', {content: '{{content (getView "pages" "foo")}}'}); 104 | renderAssert('bar.hbs', 'this is foo', cb); 105 | }); 106 | 107 | it('should get a view from the given view collection by view.basename', function(cb) { 108 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 109 | app.pages('bar.hbs', {content: '{{content (getView "pages" "foo.hbs")}}'}); 110 | renderAssert('bar.hbs', 'this is foo', cb); 111 | }); 112 | 113 | it('should get a view from the given view collection by view.path', function(cb) { 114 | app.pages('a/b/c/foo.hbs', {content: 'this is foo'}); 115 | app.pages('bar.hbs', {content: '{{content (getView "pages" "a/b/c/foo.hbs")}}'}); 116 | renderAssert('bar.hbs', 'this is foo', cb); 117 | }); 118 | }); 119 | 120 | describe('render:', function() { 121 | it('should render templates in contents of the given view object', function(cb) { 122 | app.pages('a/b/c/foo.hbs', {content: 'this is {{name}}'}); 123 | app.pages('bar.hbs', {content: '{{render (find "foo.hbs")}}'}); 124 | renderAssert('bar.hbs', 'this is foo', {name: 'foo'}, cb); 125 | }); 126 | 127 | it('should get a view and render its contents', function(cb) { 128 | app.pages('a/b/c/foo.hbs', {content: 'this is {{name}}'}); 129 | app.pages('bar.hbs', {content: '{{render "foo.hbs"}}'}); 130 | renderAssert('bar.hbs', 'this is foo', {name: 'foo'}, cb); 131 | }); 132 | 133 | it('should error when a view is not resolved', function(cb) { 134 | app.pages('a/b/c/foo.hbs', {content: 'this is {{name}}'}); 135 | app.pages('bar.hbs', {content: '{{render "blah.hbs"}}'}); 136 | renderAssert('bar.hbs', 'this is foo', function(err) { 137 | assert(err); 138 | assert.equal(err.message, 'render helper cannot find view: "blah.hbs"'); 139 | cb(); 140 | }); 141 | }); 142 | }); 143 | 144 | describe('config:', function() { 145 | it('should get a value from app.options', function(cb) { 146 | app.option('name', 'foo'); 147 | app.pages('bar.hbs', {content: '{{config "name"}}'}); 148 | renderAssert('bar.hbs', 'foo', cb); 149 | }); 150 | 151 | it('should get a value from app.data', function(cb) { 152 | app.data('name', 'foo'); 153 | app.pages('bar.hbs', {content: '{{config "name"}}'}); 154 | renderAssert('bar.hbs', 'foo', cb); 155 | }); 156 | 157 | it('should prefer values from app.cache.data over app.options', function(cb) { 158 | app.option('name', 'foo'); 159 | app.data('name', 'bar'); 160 | app.pages('bar.hbs', {content: '{{config "name"}}'}); 161 | renderAssert('bar.hbs', 'bar', cb); 162 | }); 163 | 164 | it('should prefer locals over other values', function(cb) { 165 | app.option('name', 'foo'); 166 | app.data('name', 'bar'); 167 | app.data('locals', {name: 'zzz'}); 168 | app.pages('bar.hbs', {content: '{{config locals "name"}}'}); 169 | renderAssert('bar.hbs', 'zzz', cb); 170 | }); 171 | 172 | it('should return an empty string if the property does not exist', function(cb) { 173 | app.pages('bar.hbs', {content: '{{config "name"}}'}); 174 | renderAssert('bar.hbs', '', cb); 175 | }); 176 | 177 | it('should throw an error when property is not a string', function(cb) { 178 | app.pages('bar.hbs', {content: '{{config}}'}); 179 | renderAssert('bar.hbs', 'foo', function(err) { 180 | assert(err); 181 | assert.equal(err.message, 'helper {{config}} expected "prop" to be a string'); 182 | cb(); 183 | }); 184 | }); 185 | }); 186 | 187 | describe('option:', function() { 188 | it('should get an option from app.options', function(cb) { 189 | app.option('name', 'foo'); 190 | app.pages('bar.hbs', {content: '{{option "name"}}'}); 191 | renderAssert('bar.hbs', 'foo', cb); 192 | }); 193 | 194 | it('should return an empty string if the option does not exist', function(cb) { 195 | app.pages('bar.hbs', {content: '{{option "name"}}'}); 196 | renderAssert('bar.hbs', '', cb); 197 | }); 198 | 199 | it('should throw an error when property is not a string', function(cb) { 200 | app.pages('a/b/c/foo.hbs', {content: 'this is {{option}}'}); 201 | app.pages('bar.hbs', {content: '{{render "foo.hbs"}}'}); 202 | renderAssert('bar.hbs', 'this is foo', function(err) { 203 | assert(err); 204 | assert.equal(err.message, 'helper {{option}} expected "prop" to be a string'); 205 | cb(); 206 | }); 207 | }); 208 | }); 209 | 210 | describe('enabled:', function() { 211 | it('should return true when an option is set to true', function(cb) { 212 | app.option('navbar', true); 213 | app.pages('foo.hbs', {content: '{{enabled "navbar"}}'}); 214 | renderAssert('foo.hbs', 'true', cb); 215 | }); 216 | 217 | it('should return true when an option is enabled', function(cb) { 218 | app.enable('navbar'); 219 | app.pages('foo.hbs', {content: '{{enabled "navbar"}}'}); 220 | renderAssert('foo.hbs', 'true', cb); 221 | }); 222 | 223 | it('should return false when an option is false', function(cb) { 224 | app.option('navbar', false); 225 | app.pages('foo.hbs', {content: '{{enabled "navbar"}}'}); 226 | renderAssert('foo.hbs', 'false', cb); 227 | }); 228 | 229 | it('should return false when an option is disabled', function(cb) { 230 | app.disable('navbar'); 231 | app.pages('foo.hbs', {content: '{{enabled "navbar"}}'}); 232 | renderAssert('foo.hbs', 'false', cb); 233 | }); 234 | 235 | it('should throw an error when property is not a string', function(cb) { 236 | app.pages('foo.hbs', {content: 'this is {{enabled}}'}); 237 | renderAssert('foo.hbs', 'this is foo', function(err) { 238 | assert(err); 239 | assert.equal(err.message, 'helper {{enabled}} expected "prop" to be a string'); 240 | cb(); 241 | }); 242 | }); 243 | }); 244 | 245 | describe('disabled:', function() { 246 | it('should return false when an option is set to true', function(cb) { 247 | app.option('navbar', true); 248 | app.pages('foo.hbs', {content: '{{disabled "navbar"}}'}); 249 | renderAssert('foo.hbs', 'false', cb); 250 | }); 251 | 252 | it('should return false when an option is enabled', function(cb) { 253 | app.enable('navbar'); 254 | app.pages('foo.hbs', {content: '{{disabled "navbar"}}'}); 255 | renderAssert('foo.hbs', 'false', cb); 256 | }); 257 | 258 | it('should return true when an option is false', function(cb) { 259 | app.option('navbar', false); 260 | app.pages('foo.hbs', {content: '{{disabled "navbar"}}'}); 261 | renderAssert('foo.hbs', 'true', cb); 262 | }); 263 | 264 | it('should return true when an option is disabled', function(cb) { 265 | app.disable('navbar'); 266 | app.pages('foo.hbs', {content: '{{disabled "navbar"}}'}); 267 | renderAssert('foo.hbs', 'true', cb); 268 | }); 269 | 270 | it('should throw an error when property is not a string', function(cb) { 271 | app.pages('bar.hbs', {content: '{{disabled}}'}); 272 | renderAssert('bar.hbs', '', function(err) { 273 | assert(err); 274 | assert.equal(err.message, 'helper {{disabled}} expected "prop" to be a string'); 275 | cb(); 276 | }); 277 | }); 278 | }); 279 | 280 | describe('items:', function() { 281 | it('should render a block with items from the given collection', function(cb) { 282 | app.create('posts'); 283 | app.posts(fixtures('items/*.hbs')); 284 | renderAssert('items/index.hbs', 'aaa\nbbb\nccc\nindex', cb); 285 | }); 286 | 287 | it('should render the inverse block when no items exist', function(cb) { 288 | app.create('docs'); 289 | app.create('posts'); 290 | app.posts(fixtures('items-inverse/index.hbs')); 291 | renderAssert('items-inverse/index.hbs', 'No docs yet.', cb); 292 | }); 293 | 294 | it('should render inline with items from the given collection', function(cb) { 295 | app.create('posts'); 296 | app.posts(fixtures('items-inline/*.hbs')); 297 | renderAssert('items-inline/index.hbs', 'aaa\nbbb\nccc\nindex', cb); 298 | }); 299 | 300 | it('should render the inverse each block when no items exist', function(cb) { 301 | app.create('docs'); 302 | app.create('posts'); 303 | app.posts(fixtures('items-inverse-inline/index.hbs')); 304 | renderAssert('items-inverse-inline/index.hbs', 'No docs yet.', cb); 305 | }); 306 | }); 307 | 308 | describe('sortItems:', function() { 309 | it('should sort items in the given `order`', function(cb) { 310 | renderAssert('index.hbs', 'bbb\nccc\naaa', {order: ['bbb', 'ccc', 'aaa']}, cb); 311 | }); 312 | }); 313 | 314 | describe('each - sortItems:', function() { 315 | it('should render items from the given collection', function(cb) { 316 | app.create('posts'); 317 | app.posts(fixtures('each-sortItems/*.hbs')); 318 | renderAssert('each-sortItems/index.hbs', 'bbb.hbs\nccc.hbs\naaa.hbs', cb); 319 | }); 320 | }); 321 | 322 | describe('eachItems:', function() { 323 | it('should render items from the given collection', function(cb) { 324 | app.create('posts'); 325 | app.posts(fixtures('each-items/*.hbs')); 326 | renderAssert('each-items/index.hbs', 'aaa\nbbb\nccc\nindex', cb); 327 | }); 328 | 329 | it('should sort items based on value passed to "sortBy" hash', function(cb) { 330 | app.create('posts'); 331 | app.posts(fixtures('each-items-sortBy/*.hbs')); 332 | renderAssert('each-items-sortBy/index.hbs', 'bbb\nccc\naaa\nindex', cb); 333 | }); 334 | 335 | it('should use blockParams for items', function(cb) { 336 | app.create('posts'); 337 | app.posts(fixtures('each-items-as/*.hbs')); 338 | renderAssert('each-items-as/index.hbs', 'bbb\nccc\naaa', cb); 339 | }); 340 | 341 | it('should filter items based on function passed to "filter" hash', function(cb) { 342 | app.create('posts'); 343 | app.posts(fixtures('each-items-filter-function/*.hbs')); 344 | renderAssert('each-items-filter-function/index.hbs', 'aaa\nbbb\nccc', cb); 345 | }); 346 | 347 | it('should filter items based on string passed to "filter" hash', function(cb) { 348 | app.create('posts'); 349 | app.posts(fixtures('each-items-filter-string/*.hbs')); 350 | renderAssert('each-items-filter-string/index.hbs', 'aaa\nbbb\nccc', cb); 351 | }); 352 | }); 353 | 354 | describe('assets', function() { 355 | it('should render the relative path to options.assets from the current view', function(cb) { 356 | app.option('assets', cwd('assets')); 357 | app.create('posts'); 358 | 359 | app.post(cwd('a/one.hbs'), {content: '{{assets}}/css/foo.css'}); 360 | app.post(cwd('a/b/c/two.hbs'), {content: '{{assets}}/css/foo.css'}); 361 | app.post(cwd('a/b/three.hbs'), {content: '{{assets}}/css/foo.css'}); 362 | app.post(cwd('four.hbs'), {content: '{{assets}}/css/foo.css'}); 363 | app.post(cwd('posts/index.hbs'), {content: '{{assets}}/css/foo.css'}); 364 | 365 | var keys = Object.keys(app.views.posts); 366 | var expected = ['../assets/css/foo.css', '../../../assets/css/foo.css', '../../assets/css/foo.css', 'assets/css/foo.css', '../assets/css/foo.css']; 367 | assertEach(keys, expected, cb); 368 | }); 369 | }); 370 | 371 | describe('asset', function() { 372 | it('should render the relative path from a view to the given asset path', function(cb) { 373 | app.option('assets', cwd('assets')); 374 | app.create('posts'); 375 | 376 | app.post(cwd('a/one.hbs'), {content: '{{asset "css/foo.css"}}'}); 377 | app.post(cwd('a/b/c/two.hbs'), {content: '{{asset "css/foo.css"}}'}); 378 | app.post(cwd('a/b/three.hbs'), {content: '{{asset "css/foo.css"}}'}); 379 | app.post(cwd('four.hbs'), {content: '{{asset "css/foo.css"}}'}); 380 | app.post(cwd('posts/index.hbs'), {content: '{{asset "css/foo.css"}}'}); 381 | 382 | var keys = Object.keys(app.views.posts); 383 | var expected = ['../assets/css/foo.css', '../../../assets/css/foo.css', '../../assets/css/foo.css', 'assets/css/foo.css', '../assets/css/foo.css']; 384 | assertEach(keys, expected, cb); 385 | }); 386 | }); 387 | }); 388 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var relativeDest = require('relative-dest'); 5 | var each = require('handlebars-helper-each'); 6 | var get = require('get-value'); 7 | var utils = require('handlebars-utils'); 8 | var matchfile = require('match-file'); 9 | var isObject = require('isobject'); 10 | 11 | module.exports = function(options) { 12 | return function(app) { 13 | 14 | /** 15 | * Returns the stringified contents from a `view` object, or just returns 16 | * the value if `view` is a string. 17 | * 18 | * ```js 19 | * app.view('foo', {contents: 'This is contents'}); 20 | * 21 | * // {{contents foo}} 22 | * //=> "This is contents" 23 | * ``` 24 | * @name {{content}} 25 | * @param {String|Object} `view` 26 | * @return {String} Returns `view.contents` or the value if it's a string. 27 | * @api public 28 | */ 29 | 30 | app.helper('content', function(view, options) { 31 | if (isObject(view) && view.isView) { 32 | return view.contents.toString(); 33 | } 34 | if (typeof view === 'string') { 35 | return view; 36 | } 37 | }); 38 | 39 | /** 40 | * Get the first view with the given `name` from any collection, or the 41 | * specified `collection`. If no collection is passed only `renderable` 42 | * collections will be searched ("renderable" collections are any collections 43 | * not specified as `partials` and `layouts`). 44 | * 45 | * ```handlebars 46 | * {{find "foo"}} 47 | * {{find "foo.hbs" }} 48 | * {{find "a/b/c/foo.hbs"}} 49 | * 50 | * 51 | * {{find "foo" "pages"}} 52 | * {{find "foo.hbs" "posts"}} 53 | * {{find "a/b/c/foo.hbs" "partials"}} 54 | * ``` 55 | * @name {{find}} 56 | * @param {String} `name` The name/key (`view.stem`, `view.basename`, `view.relative` or `view.path`) of the view to find. It's good practice to use a longer path when practical. 57 | * @param {String} `collection` (optional) 58 | * @return {Object|undefined} Returns the view if found, or `undefined` if not. 59 | * @api public 60 | */ 61 | 62 | app.helper('find', function(/*name, collection*/) { 63 | return this.app.find.apply(this.app, args([].slice.call(arguments))); 64 | }); 65 | 66 | /** 67 | * Get a view from the specified `collection`. 68 | * 69 | * ```handlebars 70 | * {{getView "pages" "foo"}} 71 | * {{getView "pages" "foo.hbs" }} 72 | * {{getView "pages" "a/b/c/foo.hbs"}} 73 | * ``` 74 | * @name {{getView}} 75 | * @param {String} `collection` (required) 76 | * @param {String} `name` 77 | * @return {Object} 78 | * @api public 79 | */ 80 | 81 | app.helper('getView', function(/*collection, name*/) { 82 | return this.app.getView.apply(this.app, args([].slice.call(arguments))); 83 | }); 84 | 85 | /** 86 | * Get the value of `prop` from `app.options` or the context. Dot-notation 87 | * may be used. Context values from `app.cache.data` will override values 88 | * from `app.options`, and values from locals will override all other values. 89 | * 90 | * ```js 91 | * app.option({a: {b: 'c'}, x: 'z'}); 92 | * app.data({a: {b: 'd'}}); 93 | * app.data({foo: {a: {b: 'ABC'}}}); 94 | * 95 | * // {{config "x"}} => 'z' 96 | * // {{config "a.b.c"}} => 'eee' 97 | * // {{config "a.b"}} => '{c: 'eee'}' 98 | * 99 | * // with locals 100 | * // {{config foo "a.b.c"}} => 'ABC' 101 | * // {{config foo "a.b"}} => '{c: 'ABC'}' 102 | * ``` 103 | * @name {{config}} 104 | * @param {String} `prop` 105 | * @param {Object} `locals` (optional) Locals to merge onto the options. 106 | * @return {any} 107 | * @api public 108 | */ 109 | 110 | app.helper('config', function(prop, locals, options) { 111 | if (typeof locals === 'string') { 112 | var temp = locals; 113 | locals = prop; 114 | prop = temp; 115 | } 116 | 117 | if (typeof options === 'undefined') { 118 | options = locals; 119 | locals = {}; 120 | } 121 | 122 | if (typeof prop !== 'string') { 123 | throw new TypeError('helper {{config}} expected "prop" to be a string'); 124 | } 125 | return get(createContext(this, locals, options), prop); 126 | }); 127 | 128 | /** 129 | * Return the value of `prop` from `app.options`. 130 | * 131 | * ```js 132 | * app.option({a: {b: 'c'}}); 133 | * 134 | * // {{option "a"}} => '{b: 'c'}' 135 | * // {{option "a.b"}} => 'c' 136 | * ``` 137 | * @name {{option}} 138 | * @param {String} `prop` 139 | * @return {any} 140 | * @api public 141 | */ 142 | 143 | app.helper('option', function(prop) { 144 | if (typeof prop !== 'string') { 145 | throw new TypeError('helper {{option}} expected "prop" to be a string'); 146 | } 147 | return get(this.options, prop); 148 | }); 149 | 150 | /** 151 | * Return true if the value of `prop` on `app.options` is strictly `true. 152 | * 153 | * ```js 154 | * app.option('foo', false); 155 | * app.option('bar', true); 156 | * app.enable('baz'); //<= convenience method for setting an option to true 157 | * 158 | * // {{enabled "foo"}} => false 159 | * // {{enabled "bar"}} => true 160 | * // {{enabled "baz"}} => true 161 | * ``` 162 | * @name {{enabled}} 163 | * @param {String} `prop` 164 | * @return {Boolean} 165 | * @api public 166 | */ 167 | 168 | app.helper('enabled', function(prop) { 169 | if (typeof prop !== 'string') { 170 | throw new TypeError('helper {{enabled}} expected "prop" to be a string'); 171 | } 172 | return get(this.options, prop) === true; 173 | }); 174 | 175 | /** 176 | * Return the given value of `prop` from `this.options`. 177 | * 178 | * ```js 179 | * app.option('foo', false); 180 | * app.option('bar', true); 181 | * app.disable('baz'); //<= convenience method for setting an option to false 182 | * 183 | * // {{disabled "foo"}} => true 184 | * // {{disabled "bar"}} => false 185 | * // {{disabled "baz"}} => false 186 | * ``` 187 | * @name {{disabled}} 188 | * @param {String} `prop` 189 | * @return {any} 190 | * @api public 191 | */ 192 | 193 | app.helper('disabled', function(prop) { 194 | if (typeof prop !== 'string') { 195 | throw new TypeError('helper {{disabled}} expected "prop" to be a string'); 196 | } 197 | return get(this.options, prop) === false; 198 | }); 199 | 200 | /** 201 | * Returns a function for matching a view in subexpressions. The returned 202 | * function takes a 203 | * 204 | * ```handlebars 205 | * {{#eachItems "posts" filter=(matchView "!index")}} 206 | * {{stem}} 207 | * {{/eachItems}} 208 | * ``` 209 | * @name {{matchView}} 210 | * @param {String} `prop` 211 | * @return {any} 212 | * @api public 213 | */ 214 | 215 | app.helper('matchView', matchView); 216 | 217 | /** 218 | * Returns an array of items from the views in collection `name`. 219 | * 220 | * ```handlebars 221 | * {{#items "posts"}} 222 | * {{#each .}} 223 | * 224 | * {{titleize data.title}} 225 | * {{/each}} 226 | * {{/items}} 227 | * ``` 228 | * @name {{items}} 229 | * @param {String|Object|Array} `name` Collection name, or collection/list instance, or array of list items. 230 | * @param {Object} `options` 231 | * @return {Array} 232 | * @api public 233 | */ 234 | 235 | app.helper('items', function(name, options) { 236 | if (isObject(name) && name.isList) { 237 | return name.items; 238 | } 239 | 240 | if (Array.isArray(name) && isView(name[0])) { 241 | return name; 242 | } 243 | 244 | var collection = isViews(name) ? name : this.app[name]; 245 | 246 | if (typeof collection === 'undefined') { 247 | throw new TypeError(`helper {{items}} cannot get collection ${name}`); 248 | } 249 | 250 | var list = this.app.list({onLoad: false}); 251 | list.addItems(collection.views); 252 | 253 | if (utils.isEmpty(list.items)) { 254 | return utils.inverse([], options, createContext(this, {}, options)); 255 | } 256 | 257 | return utils.fn(list.items, options, list.items); 258 | }); 259 | 260 | /** 261 | * Block helper that iterates over the items in collection `name` and 262 | * exposes each item in the collection as "this" inside the block. 263 | * 264 | * **Hash options**: 265 | * 266 | * - `sortBy`: function or property path to sort the collection by. Dot-notation may be used for getting nested properties. 267 | * - `filter`: function, glob pattern, or filepath for filtering items in the collection. 268 | * 269 | * ```js 270 | * // built-in "pages" collection 271 | * app.pages('templates/pages/*.hbs'); 272 | * // {{#eachItems "pages" filter="!index"}} 273 | * // {{log stem}} 274 | * // {{/eachItems}} 275 | * 276 | * // custom "posts" collection 277 | * app.create('posts'); //<= create the collection first 278 | * app.posts('templates/posts/*.hbs'); 279 | * // {{#eachItems "posts" sortBy="data.date"}} 280 | * // {{log stem}} 281 | * // {{/eachItems}} 282 | * ``` 283 | * @name {{eachItems}} 284 | * @param {String} `name` (required) Collection name 285 | * @param {Object} `locals` (optional) 286 | * @param {Object} `options` Handlebars options 287 | * @return {Array} 288 | * @api public 289 | */ 290 | 291 | app.helper('eachItems', function(name, locals, options) { 292 | if (typeof name !== 'string') { 293 | throw new TypeError('helper {{items}} expected collection "name" to be a string'); 294 | } 295 | 296 | if (utils.isOptions(locals)) { 297 | options = locals; 298 | locals = {}; 299 | } 300 | 301 | var opts = utils.options({}, locals, options); 302 | var args = [].slice.call(arguments, 1); 303 | var list = this.app.list({onLoad: false}); 304 | 305 | list.addItems(this.app[name].views); 306 | 307 | if (opts.filter) { 308 | if (typeof opts.filter === 'string') { 309 | list = list.filter(matchView.call(this, opts.filter, options)); 310 | } else { 311 | list = list.filter(opts.filter); 312 | } 313 | } 314 | 315 | if (opts.sortBy) { 316 | list = list.sortBy(opts.sortBy); 317 | } 318 | 319 | args.unshift(list.items); 320 | return each.apply(this, args); 321 | }); 322 | 323 | /** 324 | * Sort and filter the items in a collection to match the order of 325 | * an array of strings. Given you have three pages, `aaa.hbs`, `bbb.hbs`, 326 | * and `ccc.hbs`, the following will sort the items in the order specified 327 | * in front-matter: 328 | * 329 | * ```handlebars 330 | * --- 331 | * order: ['bbb', 'ccc', 'aaa'] 332 | * --- 333 | * 334 | * {{#each (sortItems (items "pages") order) as |item|}} 335 | * {{item.basename}} 336 | * {{/each}} 337 | * 338 | * 339 | * {{#pages}} 340 | * {{#each (sortItems items order) as |item|}} 341 | * {{item.basename}} 342 | * {{/each}} 343 | * {{/pages}} 344 | * ``` 345 | * @name {{sortItems}} 346 | * @param {Array} `items` (required) Array of items from a collection 347 | * @param {Object} `locals` (optional) 348 | * @param {Object} `options` Handlebars options 349 | * @return {Array} 350 | * @api public 351 | */ 352 | 353 | app.helper('sortItems', require('helper-sort-items')); 354 | 355 | /** 356 | * Returns a relative path from the view being rendered to destination 357 | * path of the specified view. 358 | * 359 | * ```handlebars 360 | * 361 | * {{link-to "index"}} 362 | * 363 | * {{link-to "index" "posts"}} 364 | * ``` 365 | * @name {{link-to}} 366 | * @param {String} `path` 367 | * @param {String} `collection` (optional) Collection name 368 | * @param {Array} `lookups` (optional) Array of property paths to use for resolving views. 369 | * @return {String} Relative path to the specified view. 370 | * @api public 371 | */ 372 | 373 | app.helper('link-to', require('helper-link-to')); 374 | app.helper('linkTo', require('helper-link-to')); 375 | 376 | /** 377 | * Render the given view. 378 | * 379 | * ```handlebars 380 | * {{render "foo.hbs"}} 381 | * ``` 382 | * @name {{render}} 383 | * @param {Object|String} `view` View object or the name of the view to render. 384 | * @return {String} 385 | * @api public 386 | */ 387 | 388 | app.asyncHelper('render', function(val, locals, options, cb) { 389 | if (typeof options === 'function') { 390 | cb = options; 391 | options = locals; 392 | locals = {}; 393 | } 394 | 395 | var view = val; 396 | if (typeof val === 'string') { 397 | view = this.app.find(val); 398 | 399 | if (typeof view === 'undefined') { 400 | cb(new Error(`render helper cannot find view: "${val}"`)); 401 | return; 402 | } 403 | } 404 | 405 | if (typeof view === 'undefined') { 406 | cb(new Error('expected the name of a view or a view object')); 407 | return; 408 | } 409 | 410 | var ctx = createContext(this, locals, options); 411 | 412 | this.app.render(view, ctx, function(err, res) { 413 | if (err) { 414 | err.reason = 'helper render'; 415 | cb(err); 416 | return; 417 | } 418 | cb(null, res.contents.toString()); 419 | }); 420 | }); 421 | 422 | /** 423 | * Returns the relative path to the `assets` directory defined on `app.options` 424 | * or the context. Alias for the [asset](#asset) helper, to provide a semantic 425 | * alternative depending on usage. 426 | * 427 | * ```handlebars 428 | * 429 | * 430 | * ``` 431 | * @name {{assets}} 432 | * @param {String} `filepath` (optional) Filepath to append to the assets path. 433 | * @return {String} 434 | * @alias assets 435 | * @api public 436 | */ 437 | 438 | app.helper('assets', pathHelper('"assets" path', function(options) { 439 | return options.assets; 440 | })); 441 | 442 | /** 443 | * Returns the relative path to `filename` from the `assets` directory 444 | * defined on `app.options` or the context. Alias for the [assets](#assets) 445 | * helper, to provide a semantic alternative depending on usage. 446 | * 447 | * ```handlebars 448 | * 449 | * 450 | * ``` 451 | * @name {{asset}} 452 | * @param {String} `filepath` (optional) Filepath to append to the asset path. 453 | * @return {String} 454 | * @alias assets 455 | * @api public 456 | */ 457 | 458 | app.helper('asset', pathHelper('"assets" path', function(options) { 459 | return options.assets; 460 | })); 461 | 462 | /** 463 | * Returns the relative path to `filename` from either the `root` or 464 | * `dest` directory defined on `app.options` or the context. 465 | * 466 | * ```handlebars 467 | * 468 | * 469 | * ``` 470 | * @name {{root}} 471 | * @param {String} `filepath` (optional) Filepath to append to the root path. 472 | * @return {String} 473 | * @api public 474 | */ 475 | 476 | app.helper('root', pathHelper('either a "dest" or "root" path', function(options) { 477 | return options.root || options.dest; 478 | })); 479 | }; 480 | }; 481 | 482 | function pathHelper(msg, fn) { 483 | return function(filename, locals, options) { 484 | if (isObject(filename)) { 485 | options = locals; 486 | locals = filename; 487 | filename = ''; 488 | } 489 | 490 | var opts = utils.options(this, locals, options); 491 | opts = Object.assign({}, this.app.options, opts); 492 | var dir = fn(opts); 493 | 494 | if (typeof dir !== 'string') { 495 | var name = helperName(locals, options); 496 | throw new TypeError(`helper {{${name}}} expected ${msg} to be defined on the options or context`); 497 | } 498 | 499 | return relativePath(this, path.resolve(dir, filename)); 500 | }; 501 | } 502 | 503 | function relativePath(app, targetPath) { 504 | var view = app.view || app.context.view; 505 | var fp = view.data.path || view.path; 506 | var rel = relativeDest(fp, targetPath); 507 | return rel.slice(0, 2) === './' ? rel.slice(2) : rel; 508 | } 509 | 510 | function matchView(pattern, options) { 511 | if (typeof pattern !== 'string') { 512 | throw new TypeError('expected pattern to be a string'); 513 | } 514 | 515 | var opts = hash(options); 516 | var isNegated = false; 517 | 518 | if (pattern.charAt(0) === '!') { 519 | isNegated = true; 520 | pattern = pattern.slice(1); 521 | } 522 | 523 | return function(item) { 524 | if (isNegated) { 525 | return !matchfile.isMatch(pattern, item, opts); 526 | } 527 | return matchfile.isMatch(pattern, item, opts); 528 | }; 529 | } 530 | 531 | function createContext(thisArg, locals, options) { 532 | return Object.assign({}, thisArg.options, thisArg.context, locals, hash(options)); 533 | } 534 | 535 | function hash(options) { 536 | return (options && options.hash) || {}; 537 | } 538 | 539 | function isViews(views) { 540 | return isObject(views) && (views.isCollection || views.isViews); 541 | } 542 | 543 | function helperName(locals, options) { 544 | return (utils.isOptions(locals) ? locals : options).name; 545 | } 546 | 547 | function isView(view) { 548 | return isObject(view) && (view.isView || view.isItem); 549 | } 550 | 551 | function args(arr) { 552 | return arr.slice(0, arr.length - 1); 553 | } 554 | --------------------------------------------------------------------------------