set-property-from-file v2.0.0
set-property-from-file
Set object property using a file path and contents
var setPropertyFromFile = require('set-property-from-file');
// foo.txt (Hello!)
setPropertyFromFile({bar: true}, 'foo.txt', 'utf8', function(err, res) {
if (err) {
throw err;
}
res; //=> {foo: 'Hello!', bar: true};
});
Installation
npm install set-property-from-file
API
var setPropertyFromFile = require('set-property-from-file');
setPropertyFromFile(target, filePath , options, callback)
target: Object
filePath: String
(a relative file path)
options: Object
or String
(file encoding)
callback: Function
It reads a file asynchronously, then sets the property of the target object to the file contents. (It automatically strip UTF-8 byte order mark from contents.)
The names of the created properties are based on the file path. For example,
foo.txt
setsfoo
property.foo/bar.txt
setsfoo.bar
property.foo/bar/baz.qux.txt
setsfoo.bar['baz.qux']
property.../foo/bar.txt
sets['..'].foo.bar
property.foo/../bar/baz.txt
setsbar.baz
property.
var assert = require('assert');
var setPropertyFromFile = require('set-property-from-file');
var target = {
fixtures: {
foo: 'bar'
}
};
setPropertyFromFile(target, 'fixtures/images/00.jpg', function(err, res) {
if (err) {
throw err;
}
// Adds fixtures.images['00'] property to the target object.
assert.deepEqual(res, {
fixtures: {
foo: 'bar' // target's default property
images: {
'00': <Buffer ... > // new property
}
}
});
});
It doesn't create a new property under the existing property if the existing property is not an object.
var assert = require('assert');
var setPropertyFromFile = require('set-property-from-file');
var target = {
fixtures: {
foo: 'Hello!' // string (not an object)
}
};
setPropertyFromFile(target, 'fixtures/foo/bar/baz.txt', function(err, res) {
if (err) {
throw err;
}
// Doesn't overwrite the existing non-object property.
assert.deepEqual(res, {
fixtures: {
foo: 'Hello!' // object['fixtures']['foo'] already exists
}
});
});
options
(In addition to the follwing options, all fs.readFile options are available.)
options.cwd
Type: String
Default: process.cwd()
Specify the working directory the source path is relative to. This won't be included in the property names.
// Reads one/two/three.txt
setPropertyFromFile({}, 'two/three.txt', {cwd: 'one'}, function(err, res) {
res; // {two: {three: <Buffer ...>}}
});
options.base
Type: String
Default: ''
Specify the directory relative to the cwd. This won't be included in the property names.
// Reads one/two/three.txt
setPropertyFromFile({}, 'one/two/three.txt', {base: 'one/two'}, function(err, res) {
res; // {three: <Buffer ...>}
});
options.ext
Type: Boolean
Default: false
By default the property names don't include file extension. true
keeps it in the last property name.
setPropertyFromFile({}, 'index.js', function(err, res) {
res; //=> {index: ... }
});
setPropertyFromFile({}, 'index.js', {ext: true}, function(err, res) {
res; //=> {index: {js: ... }}
});
options.processor
Type: Function
Default: undefined
Specify a function to process file contents before setting the properties.
// foo.txt (abcde)
var encoder = function(content) {
return content.toString().toUpperCase();
};
setPropertyFromFile({}, 'foo.txt', {encoding: encoder}, function(err, res) {
res; //=> 'ABCDE'
});
callback(error, result)
error: Error
if it fails to read the file, otherwise null
result: Object
(Target object modified)
Note that it overwrites the target object.
var target = {foo: true};
setPropertyFromFile(target, 'bar.txt', function(err, res) {
res; //=> {foo: true, bar: ... }
target; //=> {foo: true, bar: ... }
res === target; //=> true
});
License
Copyright (c) 2014 Shinnosuke Watanabe
Licensed under the MIT License.