set-property-from-file
Set object property using a file path and contents
var setPropertyFromFile = ; // foo.txt (Hello!) ;
Installation
npm install set-property-from-file
API
var setPropertyFromFile = ;
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 = ;var setPropertyFromFile = ; var target = fixtures: foo: 'bar' ; ;
It doesn't create a new property under the existing property if the existing property is not an object.
var assert = ;var setPropertyFromFile = ; var target = fixtures: foo: 'Hello!' // string (not an object) ; ;
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;
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;
options.ext
Type: Boolean
Default: false
By default the property names don't include file extension. true
keeps it in the last property name.
; ;
options.processor
Type: Function
Default: undefined
Specify a function to process file contents before setting the properties.
// foo.txt (abcde) var { return content;}; ;
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; ;
License
Copyright (c) 2014 Shinnosuke Watanabe
Licensed under the MIT License.