dir-to-object
Advanced tools
Comparing version 1.0.0-alpha.3 to 1.0.0-alpha.4
@@ -5,5 +5,21 @@ const { join } = require('path'); | ||
describe('dirToObject', () => { | ||
it('to match snapshot', () => { | ||
expect(dirToObject(join(__dirname, '../__mocks__/foo'))).toMatchSnapshot(); | ||
const scenarios = [ | ||
{ | ||
config: { dirPath: join(__dirname, '../__mocks__/foo') }, | ||
description: 'config w/ dirPath only' | ||
}, | ||
{ | ||
config: { | ||
canAdd: data => data.resolve && data.type, | ||
dirPath: join(__dirname, '../__mocks__/foo') | ||
}, | ||
description: 'config w/ canAdd and dirPath' | ||
} | ||
]; | ||
scenarios.forEach(({ config, description }) => { | ||
it(description, () => { | ||
expect(dirToObject(config)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "dir-to-object", | ||
"version": "1.0.0-alpha.3", | ||
"version": "1.0.0-alpha.4", | ||
"description": "Builds an object from the files contained inside a directory.", | ||
@@ -9,3 +9,4 @@ "main": "index.js", | ||
"pretest": "npm run eslint", | ||
"test": "jest" | ||
"test": "jest", | ||
"test:watch": "jest --watch" | ||
}, | ||
@@ -12,0 +13,0 @@ "repository": { |
@@ -17,7 +17,14 @@ # dir-to-object | ||
const foo = dirToObject(join(__dirname, 'foo')); | ||
const config = { | ||
canAdd: () => true, | ||
dirPath: join(__dirname, 'foo') | ||
}; | ||
const foo = dirToObject(config); | ||
console.log(foo); | ||
``` | ||
N.B.: `canAdd` is optional while `dirPath` is mandatory | ||
## Contributing | ||
@@ -24,0 +31,0 @@ |
const fs = require('fs'); | ||
const { join } = require('path'); | ||
const fields = path => | ||
fs.readdirSync(path).reduce((accumulator, current) => { | ||
const fields = ({ canAdd, dirPath }) => | ||
fs.readdirSync(dirPath).reduce((accumulator, fileName) => { | ||
const filePath = join(dirPath, fileName); | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
accumulator[current.replace('.js', '')] = require(`${path}/${current}`); | ||
const content = require(filePath); | ||
if (canAdd && canAdd(content)) { | ||
accumulator[fileName.replace('.js', '')] = content; | ||
} | ||
if (!canAdd) { | ||
accumulator[fileName.replace('.js', '')] = content; | ||
} | ||
return accumulator; | ||
@@ -8,0 +16,0 @@ }, {}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
4999
10
50
40