dir-to-object
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -5,21 +5,47 @@ const { join } = require('path'); | ||
describe('dirToObject', () => { | ||
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') | ||
const dirPath = join(__dirname, '../__mocks__/foo'); | ||
describe('v1', () => { | ||
const scenarios = [ | ||
{ | ||
config: { dirPath }, | ||
description: 'config w/ dirPath only' | ||
}, | ||
description: 'config w/ canAdd and dirPath' | ||
} | ||
]; | ||
{ | ||
config: { | ||
canAdd: data => data.resolve && data.type, | ||
dirPath | ||
}, | ||
description: 'config w/ canAdd and dirPath' | ||
} | ||
]; | ||
scenarios.forEach(({ config, description }) => { | ||
it(description, () => { | ||
expect(dirToObject(config)).toMatchSnapshot(); | ||
scenarios.forEach(({ config, description }) => { | ||
it(description, () => { | ||
expect(dirToObject(config)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
describe('v2', () => { | ||
const scenarios = [ | ||
{ | ||
description: 'w/ path only', | ||
path: dirPath | ||
}, | ||
{ | ||
description: 'w/ path and options', | ||
options: { | ||
canAdd: data => data.resolve && data.type | ||
}, | ||
path: dirPath | ||
} | ||
]; | ||
scenarios.forEach(({ description, options, path }) => { | ||
it(description, () => { | ||
expect(dirToObject(path, options)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "dir-to-object", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Builds an object from the files contained inside a directory.", | ||
"main": "index.js", | ||
"scripts": { | ||
"coverage": "codecov", | ||
"eslint": "eslint src/**", | ||
"pretest": "npm run eslint", | ||
"test": "jest", | ||
"test:coverage": "jest --coverage", | ||
"test:watch": "jest --watch" | ||
@@ -32,2 +33,3 @@ }, | ||
"devDependencies": { | ||
"codecov": "3.0.4", | ||
"eslint": "5.4.0", | ||
@@ -34,0 +36,0 @@ "eslint-config-airbnb": "17.1.0", |
@@ -17,19 +17,21 @@ # dir-to-object | ||
const config = { | ||
canAdd: () => true, | ||
dirPath: join(__dirname, 'foo') | ||
}; | ||
const path = join(__dirname, './__mocks__/foo'); | ||
const foo = dirToObject(config); | ||
const options = { canAdd: () => true }; | ||
console.log(foo); | ||
const bar = dirToObject(path, options); | ||
console.log(bar); | ||
``` | ||
Where **config** is an object with the following properties: | ||
Where **path** is a string and it is required: | ||
| name | type | example | required or optional | | ||
| ------- | ----------------------------- | ----------------------------------- | -------------------- | | ||
| canAdd | _function_: (data) => boolean | `data => data.resolve && data.type` | _optional_ | | ||
| dirPath | _string_ | `join(__dirname, 'foo')` | _required_ | | ||
> e.g.: `join(__dirname, './foo')` | ||
And where **config** is an object with the following property: | ||
| name | type | example | required or optional | | ||
| ------ | ----------------------------- | ----------------------------------- | -------------------- | | ||
| canAdd | _function_: (data) => boolean | `data => data.resolve && data.type` | _optional_ | | ||
## Contributing | ||
@@ -36,0 +38,0 @@ |
const fs = require('fs'); | ||
const { join } = require('path'); | ||
const dirToObject = ({ canAdd, dirPath }) => | ||
fs.readdirSync(dirPath).reduce((accumulator, fileName) => { | ||
const filePath = join(dirPath, fileName); | ||
function dirToObjectV2(path, { canAdd }) { | ||
return fs.readdirSync(path).reduce((accumulator, fileName) => { | ||
const filePath = join(path, fileName); | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
@@ -17,3 +17,18 @@ const content = require(filePath); | ||
}, {}); | ||
} | ||
function dirToObjectV1({ canAdd, dirPath }) { | ||
return dirToObjectV2(dirPath, { canAdd }); | ||
} | ||
function dirToObject(path, options = {}) { | ||
if (typeof path === 'object') { | ||
console.warn( | ||
'WARNING: since v2 `dirToObject` should NOT be invoked w/ (config) but w/ (path, options) instead.' | ||
); | ||
return dirToObjectV1(path); | ||
} | ||
return dirToObjectV2(path, options); | ||
} | ||
module.exports = dirToObject; |
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
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
7198
12
86
47
8