https://github.com/shuckster/compose-paths
Define paths + routes without the repetition.
https://github.com/shuckster/compose-paths
compose define folders hierarchical paths tree-structure
Last synced: about 2 months ago
JSON representation
Define paths + routes without the repetition.
- Host: GitHub
- URL: https://github.com/shuckster/compose-paths
- Owner: shuckster
- License: mit
- Created: 2021-05-02T00:56:42.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-07T22:53:01.000Z (almost 2 years ago)
- Last Synced: 2025-09-25T08:24:04.740Z (6 months ago)
- Topics: compose, define, folders, hierarchical, paths, tree-structure
- Language: JavaScript
- Homepage:
- Size: 307 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
compose-paths 🛣
Quickly throw together a few local paths and have them assigned to aliases or routes.
```js
const { composePaths } = require('compose-paths')
const paths = composePaths(`
${__dirname}
/src
/html
/templates = TEMPLATES
/pages = PAGES
/public = PUBLIC
/images = IMAGES
`)
paths.TEMPLATES
// "/dir/name/src/html/templates"
paths.PUBLIC
// "/dir/name/public"
paths.aliases
// ["TEMPLATES", "PAGES", "PUBLIC", "IMAGES"]
```
`compose-paths` looks at the indentation-level of its input as the cue to concatenate lines together. Either `tabs` or `spaces` should be fine, so long as you're consistent.
## More examples
### `Path` → `Route` (via aliases and `zip`)
```js
const { composePaths, zip } = require('compose-paths')
const routes = composePaths(`
/ = HOME
/about = ABOUT
/contact = CONTACT
`)
const paths = composePaths(`
${__dirname}
/src
/html/pages
/index.html = HOME
/about.html = ABOUT
/contact.html = CONTACT
`)
const staticRoutes = zip(routes, paths)
staticRoutes.forEach(([route, path]) => {
app.get(route, sendFile(path))
})
```
### `Path` → `Route` (directly)
```js
const { composePaths } = require('compose-paths')
const pathFromRoute = composePaths(`
${__dirname}
/src/html/pages
/index.html = /
/about.html = /about
/contact.html = /contact
`)
pathFromRoute['/']
// "/dir/name/src/html/pages/index.html"
pathFromRoute['/contact']
// "/dir/name/src/html/pages/contact.html"
pathFromRoute.aliases.forEach(route => {
console.log(pathFromRoute[route])
})
```
That's it!
# Credits
`compose-paths` was written by [Conan Theobald](https://github.com/shuckster/).
Did you find this useful? If so, I like [coffee ☕️](https://www.buymeacoffee.com/shuckster) :)
## License
MIT licensed: See [LICENSE](LICENSE)