[Pkg-javascript-commits] [node-require-dir] 01/07: New upstream version 1.0.0
Praveen Arimbrathodiyil
praveen at moszumanska.debian.org
Thu Mar 1 14:36:08 UTC 2018
This is an automated email from the git hooks/post-receive script.
praveen pushed a commit to branch master
in repository node-require-dir.
commit 0f5c511024283406cb72daebb1a434c0fee1b497
Author: Pirate Praveen <praveen at debian.org>
Date: Thu Mar 1 19:32:15 2018 +0530
New upstream version 1.0.0
---
.gitignore | 3 ++-
.travis.yml | 2 ++
CHANGELOG.md | 6 ++++++
README.md | 36 +++++++++++++++++++++++++++++------
index.js | 56 +++++++++++++++++++++++++++----------------------------
package.json | 44 +++++++++++++++++++++----------------------
test/camelcase.js | 20 --------------------
test/filter.js | 9 +++++++++
test/mapKey.js | 29 ++++++++++++++++++++++++++++
test/mapValue.js | 30 +++++++++++++++++++++++++++++
10 files changed, 158 insertions(+), 77 deletions(-)
diff --git a/.gitignore b/.gitignore
index 688ef23..4c7df30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
npm-debug.log
-node_modules
\ No newline at end of file
+node_modules
+package-lock.json
diff --git a/.travis.yml b/.travis.yml
index b9879cc..53fc891 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,7 @@
language: node_js
node_js:
+ - "8"
+ - "7"
- "6"
- "5"
- "4"
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..254c1f4
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,6 @@
+## 1.0.0
+
+- mapValue option added
+- mapKey option added
+- filter option added
+- camelcase option removed, use mapKey to achieve the same functionality
diff --git a/README.md b/README.md
index b102855..c59fc00 100644
--- a/README.md
+++ b/README.md
@@ -28,7 +28,7 @@ dir
```
And if CoffeeScript has been registered via `require('coffee-script/register')`,
-`c.coffee` will also be returned.
+`c.coffee` will also be returned. Any extension registered with node will work the same way without any additional configuration.
## Installation
@@ -60,10 +60,6 @@ var dir = requireDir('./path/to/dir', {recurse: true});
(`node_modules` within subdirectories will be ignored.)
Default is false.
-`camelcase`: Automatically add camelcase aliases for files with dash- and
-underscore-separated names. E.g. `foo-bar.js` will be exposed under both the
-original `'foo-bar'` name as well as a `'fooBar'` alias. Default is false.
-
`duplicates`: By default, if multiple files share the same basename, only the
highest priority one is `require()`'d and returned. (Priority is determined by
the order of `require.extensions` keys, with directories taking precedence
@@ -83,7 +79,35 @@ be the same by default, but specifying `duplicates: true` would yield:
}
```
-There might be more options in the future. ;)
+`filter`: Apply a filter on the filename before require-ing. For example, ignoring files prefixed with `dev` in a production environment:
+
+```js
+requireDir('./dir', {
+ filter: function (fullPath) {
+ return process.env.NODE_ENV !== 'production' && !fullPath.match(/$dev/);
+ }
+})
+```
+
+`mapKey`: Apply a transform to the module base name after require-ing. For example, uppercasing any module names:
+
+```js
+requireDir('./dir', {
+ mapKey: function (value, baseName) {
+ return baseName.toUpperCase();
+ }
+})
+```
+
+`mapValue`: Apply a transform to the value after require-ing. For example, uppercasing any text exported:
+
+```js
+requireDir('./dir', {
+ mapValue: function (value, fileName) {
+ return typeof value === 'string' ? value.toUpperCase() : value;
+ }
+})
+```
## Tips
diff --git a/index.js b/index.js
index a4122be..0c5ac7f 100644
--- a/index.js
+++ b/index.js
@@ -1,15 +1,15 @@
// requireDir.js
// See README.md for details.
-var FS = require('fs');
-var Path = require('path');
+var fs = require('fs');
+var path = require('path');
// make a note of the calling file's path, so that we can resolve relative
// paths. this only works if a fresh version of this module is run on every
// require(), so important: we clear the require() cache each time!
var parent = module.parent;
var parentFile = parent.filename;
-var parentDir = Path.dirname(parentFile);
+var parentDir = path.dirname(parentFile);
delete require.cache[__filename];
module.exports = function requireDir(dir, opts) {
@@ -18,11 +18,11 @@ module.exports = function requireDir(dir, opts) {
opts = opts || {};
// resolve the path to an absolute one:
- dir = Path.resolve(parentDir, dir);
+ dir = path.resolve(parentDir, dir);
// read the directory's files:
// note that this'll throw an error if the path isn't a directory.
- var files = FS.readdirSync(dir);
+ var files = fs.readdirSync(dir);
// to prioritize between multiple files with the same basename, we'll
// first derive all the basenames and create a map from them to files:
@@ -30,9 +30,8 @@ module.exports = function requireDir(dir, opts) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
- var ext = Path.extname(file);
- var base = Path.basename(file, ext);
-
+ var ext = path.extname(file);
+ var base = path.basename(file, ext);
(filesForBase[base] = filesForBase[base] || []).push(file);
}
@@ -60,20 +59,24 @@ module.exports = function requireDir(dir, opts) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
- var path = Path.resolve(dir, file);
+ var abs = path.resolve(dir, file);
// ignore the calling file:
- if (path === parentFile) {
+ if (abs === parentFile) {
+ continue;
+ }
+ // apply file filter:
+ if (opts.filter && !opts.filter(abs)) {
continue;
}
- if (FS.statSync(path).isDirectory()) {
+ if (fs.statSync(abs).isDirectory()) {
if (opts.recurse) {
if (base === 'node_modules') {
continue;
}
- map[base] = requireDir(path, opts);
+ map[base] = requireDir(abs, opts);
// if duplicates are wanted, key off the full name too:
if (opts.duplicates) {
@@ -81,7 +84,7 @@ module.exports = function requireDir(dir, opts) {
}
}
} else {
- filesMinusDirs[file] = path;
+ filesMinusDirs[file] = abs;
}
}
@@ -93,19 +96,20 @@ module.exports = function requireDir(dir, opts) {
// otherwise, go through and try each require.extension key!
for (ext in require.extensions) {
+ // Node v8+ uses "clean" objects w/o hasOwnProperty for require
// again protect against enumerable object prototype extensions:
- if (!require.extensions.hasOwnProperty(ext)) {
+ if (!Object.prototype.hasOwnProperty.call(require.extensions, ext)) {
continue;
}
// if a file exists with this extension, we'll require() it:
var file = base + ext;
- var path = filesMinusDirs[file];
+ var abs = filesMinusDirs[file];
- if (path) {
+ if (abs) {
// ignore TypeScript declaration files. They should never be
// `require`d
- if (/\.d\.ts$/.test(path)) {
+ if (/\.d\.ts$/.test(abs)) {
continue;
}
@@ -114,34 +118,30 @@ module.exports = function requireDir(dir, opts) {
// has higher priority than any that follow it). if duplicates
// aren't wanted, we're done with this basename.
if (opts.duplicates) {
- map[file] = require(path);
+ map[file] = require(abs);
if (!map[base]) {
map[base] = map[file];
}
} else {
- map[base] = require(path);
+ map[base] = require(abs);
break;
}
}
}
}
- if (opts.camelcase) {
+ if (opts.mapKey || opts.mapValue) {
for (var base in map) {
// protect against enumerable object prototype extensions:
if (!map.hasOwnProperty(base)) {
continue;
}
- map[toCamelCase(base)] = map[base];
+ var newKey = opts.mapKey ? opts.mapKey(map[base], base) : base;
+ var newVal = opts.mapValue ? opts.mapValue(map[base], newKey) : map[base];
+ delete map[base];
+ map[newKey] = newVal;
}
}
-
return map;
};
-
-function toCamelCase(str) {
- return str.replace(/[_-][a-z]/ig, function (s) {
- return s.substring(1).toUpperCase();
- });
-}
diff --git a/package.json b/package.json
index a907c93..cf18471 100644
--- a/package.json
+++ b/package.json
@@ -1,24 +1,24 @@
-{ "name": "require-dir"
-, "description": "Helper to require() directories."
-, "version": "0.3.1"
-, "author": "Aseem Kishore <aseem.kishore at gmail.com>"
-, "license": "MIT"
-, "dependencies": {}
-, "devDependencies":
- { "coffee-script": "~1.3.3"
- , "mkdirp": "^0.5.0"
- , "ts-node": "^1.3.0"
- , "typescript": "^1.8.0"
- }
-, "engines":
- { "node": "*"
- }
-, "scripts":
- { "test": "node test"
- }
-, "homepage": "https://github.com/aseemk/requireDir"
-, "repository":
- { "type": "git"
- , "url": "git://github.com/aseemk/requireDir.git"
+{
+ "name": "require-dir",
+ "description": "Helper to require() directories.",
+ "version": "1.0.0",
+ "author": "Aseem Kishore <aseem.kishore at gmail.com>",
+ "license": "MIT",
+ "dependencies": {},
+ "devDependencies": {
+ "coffee-script": "^1.3.3",
+ "ts-node": "^1.3.0",
+ "typescript": "^1.8.0"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "scripts": {
+ "test": "node test"
+ },
+ "homepage": "https://github.com/aseemk/requireDir",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/aseemk/requireDir.git"
}
}
diff --git a/test/camelcase.js b/test/camelcase.js
deleted file mode 100644
index 83be585..0000000
--- a/test/camelcase.js
+++ /dev/null
@@ -1,20 +0,0 @@
-var assert = require('assert');
-var requireDir = require('..');
-
-assert.deepEqual(requireDir('./camelcase', {
- recurse: true,
- camelcase: true
-}), {
- aMain: 'a main',
- 'a_main': 'a main',
- subDir: {
- aSub: 'a sub',
- 'a-sub': 'a sub'
- },
- 'sub-dir': {
- aSub: 'a sub',
- 'a-sub': 'a sub'
- }
-});
-
-console.log('Camelcase tests passed.');
diff --git a/test/filter.js b/test/filter.js
new file mode 100644
index 0000000..a7bdae9
--- /dev/null
+++ b/test/filter.js
@@ -0,0 +1,9 @@
+var assert = require('assert');
+var requireDir = require('..');
+
+// filter the results to a particular file:
+assert.deepEqual(requireDir('./simple', { filter: function (filename) { return filename.match(/a\.js$/); } }), {
+ a: 'a'
+});
+
+console.log('Filter tests passed.');
diff --git a/test/mapKey.js b/test/mapKey.js
new file mode 100644
index 0000000..ebeae5a
--- /dev/null
+++ b/test/mapKey.js
@@ -0,0 +1,29 @@
+var assert = require('assert');
+var requireDir = require('..');
+
+var mapper = function(v, f) {
+ return f.toUpperCase();
+};
+
+// first test without recursing:
+assert.deepEqual(requireDir('./recurse', { mapKey: mapper }), {
+ A: 'a',
+});
+
+// then test with recursing:
+assert.deepEqual(requireDir('./recurse', { recurse: true, mapKey: mapper }), {
+ A: 'a',
+ B: {
+ '1': {
+ FOO: 'foo',
+ BAR: 'bar',
+ },
+ '2': {} // note how the directory is always returned
+ },
+ C: {
+ '3': 3
+ },
+ // note that node_modules was explicitly ignored
+});
+
+console.log('mapKey tests passed.');
diff --git a/test/mapValue.js b/test/mapValue.js
new file mode 100644
index 0000000..c015810
--- /dev/null
+++ b/test/mapValue.js
@@ -0,0 +1,30 @@
+var assert = require('assert');
+var requireDir = require('..');
+
+var mapper = function(v, f) {
+ if (typeof v === 'string') return v.toUpperCase();
+ return v;
+};
+
+// first test without recursing:
+assert.deepEqual(requireDir('./recurse', { mapValue: mapper }), {
+ a: 'A',
+});
+
+// then test with recursing:
+assert.deepEqual(requireDir('./recurse', { recurse: true, mapValue: mapper }), {
+ a: 'A',
+ b: {
+ '1': {
+ foo: 'FOO',
+ bar: 'BAR',
+ },
+ '2': {} // note how the directory is always returned
+ },
+ c: {
+ '3': 3
+ },
+ // note that node_modules was explicitly ignored
+});
+
+console.log('mapValue tests passed.');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-require-dir.git
More information about the Pkg-javascript-commits
mailing list