Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

map-files

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-files

Return an object for a glob of files. Pass a `rename` function for the keys, or a `parse` function for the content, allowing it to be used for readable or require-able files.

  • 0.7.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.8K
increased by12.89%
Maintainers
1
Weekly downloads
 
Created
Source

map-files NPM version Build Status

Return an object for a glob of files. Pass a rename function for the keys, or a parse function for the content, allowing it to be used for readable or require-able files.

As of v0.5.0, map-files returns absolute file paths by default. You can achieve the same results by using a custom name function as in the examples.

Install with npm

npm i map-files --save

Usage

var files = require('map-files');
console.log(files('templates/*.txt'));

Returns an object that looks something like:

{ a: { content: 'AAA', path: 'templates/a.txt' },
  b: { content: 'BBB', path: 'templates/b.txt' },
  c: { content: 'CCC', path: 'templates/c.txt' }}

options.cache

Type: Boolean

Default: false

If true, results will be cached in memory so that subsequent lookups for the same cwd and patterns don't repeatedly hit the file system.

options.cwd

Type: String

Default: process.cwd()

Specify the current working directory

files('*.txt', {cwd: 'templates'});

options.name

Type: Function

Default: path.basename(fp, path.extname(fp))

Rename the key of each file object:

var templates = files('templates/*.txt', {
  name: function (filepath) {
    return path.basename(filepath);
  }
});

Returns something like:

{ 'a.txt': { content: 'AAA', path: 'templates/a.txt' },
  'b.txt': { content: 'BBB', path: 'templates/b.txt' },
  'c.txt': { content: 'CCC', path: 'templates/c.txt' }}

options.read

Pass a custom read function to change the object returned for each file.

Type: Function

Default: fs.readFileSync()

The default function reads files and returns a string, but you can do anything you want with the function, like require files:

var helpers = files('helpers/*.js', {
  read: function (fp) {
    return require(path.resolve(fp));
  }
});
//=> { a: [Function: foo], b: [Function: bar], c: [Function: baz] }

Other files libs

  • file-reader: Read a glob of files, dynamically choosing the reader or requiring the files based on the file extension.
  • filter-files: Recursively read directories and return a list of files, filtered to have only the files for which the (optional) filter function returns true. Sync and async.
  • micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch (10-45x faster on avg). Just use minimatch.isMatch() instead of multimatch() or minimatch().
  • export-files: node.js utility for exporting a directory of files as modules.

Running tests

Install dev dependencies.

npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb-cli on March 22, 2015.

Keywords

FAQs

Package last updated on 05 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc