Skip to content

Commit

Permalink
add processor option
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Dec 11, 2014
1 parent 5db4587 commit ff5d95d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ setPropertyFromFile({}, 'index.js', {ext: true}, function(err, res) {
});
```

##### options.encoding
##### options.processor

Type: `String` or `Function`
Type: `Function`
Default: `undefined`

If you set this option to a function, it will be used to process file contents before setting the property.
Specify a function to process file contents before setting the properties.

```javascript
// foo.txt (abcde)
Expand Down
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var fs = require('graceful-fs');
var isRelativePath = require('is-relative');
var objectPath = require('object-path');
var stripBom = require('strip-bom');
var xtend = require('xtend');

module.exports = function setPropertyFromFile(target, filePath, options, cb) {
if (!target || typeof target !== 'object') {
Expand All @@ -34,11 +33,11 @@ module.exports = function setPropertyFromFile(target, filePath, options, cb) {
throw new Error('Second argument must be a relative path.');
}

var encoder;

if (typeof options.encoding === 'function') {
encoder = options.encoding;
options = xtend(options, {encoding: null});
if (options.processor && typeof options.processor !== 'function') {
throw new TypeError(
options.processor +
' is not a function. processor option must be a function.'
);
}

if (typeof cb !== 'function') {
Expand Down Expand Up @@ -69,8 +68,8 @@ module.exports = function setPropertyFromFile(target, filePath, options, cb) {

result = stripBom(result);

if (encoder) {
result = encoder(result);
if (options.processor) {
result = options.processor(result);
}

objectPath.set(target, props, result);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"graceful-fs": "^3.0.5",
"is-relative": "^0.1.3",
"object-path": "^0.8.1",
"strip-bom": "^1.0.0",
"xtend": "^4.0.0"
"strip-bom": "^1.0.0"
},
"devDependencies": {
"eslint": "^0.10.1",
Expand Down
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var setPropertyFromFile = require('../');
var test = require('tape');

test('setPropertyFromFile()', function(t) {
t.plan(15);
t.plan(16);

var obj = {};
setPropertyFromFile(obj, '.gitattributes', 'utf8', function(err, res) {
Expand Down Expand Up @@ -73,15 +73,15 @@ test('setPropertyFromFile()', function(t) {
);
});

var option = {encoding: Boolean};
var option = {processor: Boolean};

setPropertyFromFile({}, 'index.js', option, function(err, res) {
t.deepEqual(
[err, res],
[null, {index: true}],
'should process file content with a function using `encoding` option.'
'should process file content with a function using `processor` option.'
);
t.deepEqual(option, {encoding: Boolean}, 'should not modify option object.');
t.deepEqual(option, {processor: Boolean}, 'should not modify option object.');
});

setPropertyFromFile({}, 'test/fixtures/a.txt', {ext: true}, function(err, res) {
Expand All @@ -92,7 +92,7 @@ test('setPropertyFromFile()', function(t) {
);
});

setPropertyFromFile({}, 'node_modules', function(err) {
setPropertyFromFile({}, 'node_modules', {processor: null}, function(err) {
t.equal(
err.code,
'EISDIR',
Expand All @@ -113,7 +113,13 @@ test('setPropertyFromFile()', function(t) {
);

t.throws(
setPropertyFromFile.bind(null, {}, '.gitattributes', {}),
setPropertyFromFile.bind(null, {}, '.gitattributes', {processor: true}, noop),
/TypeError.*must be a function/,
'should throw a type error when the `processor` option is not a function.'
);

t.throws(
setPropertyFromFile.bind(null, {}, '.gitattributes', {}, [noop]),
/TypeError.*must be a function/,
'should throw a type error when the last argument is not a function.'
);
Expand Down

0 comments on commit ff5d95d

Please sign in to comment.