npm.io
1.3.0 • Published 2d ago

compose-paths

Licence
MIT
Version
1.3.0
Deps
0
Size
22 kB
Vulns
0
Weekly
0

compose-paths

MIT license npm bundle size Version

Quickly throw together a few local paths and have them assigned to aliases or routes.

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.

TypeScript: when the path chart is a string literal or template (or an as const array of lines), alias names after = are inferred on the result — so paths.TEMPLATES type-checks and unknown aliases are errors. Dynamic string inputs stay loosely typed.

More examples

PathRoute (via aliases and zip)

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))
})
PathRoute (directly)
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.

Did you find this useful? If so, I like coffee :)

License

MIT licensed: See LICENSE

Keywords