-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e44cc12
Showing
12 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 2 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
env: | ||
node: true | ||
rules: | ||
no-extra-parens: 2 | ||
eqeqeq: 2 | ||
block-scoped-var: 2 | ||
quotes: | ||
- 2 | ||
- single |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: node_js | ||
node_js: | ||
- '0.10' | ||
- '0.11' | ||
after_script: | ||
- npm run-script coveralls | ||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Shinnosuke Watanabe | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# assign-file | ||
|
||
[![Build Status](https://travis-ci.org/shinnn/assign-file.svg?branch=master)](https://travis-ci.org/shinnn/assign-file) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/hmau5iy4r0wppsta?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/assign-file) | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/assign-file.svg)](https://coveralls.io/r/shinnn/assign-file) | ||
[![Dependency Status](https://david-dm.org/shinnn/assign-file.svg)](https://david-dm.org/shinnn/assign-file) | ||
[![devDependency Status](https://david-dm.org/shinnn/assign-file/dev-status.svg)](https://david-dm.org/shinnn/assign-file#info=devDependencies) | ||
|
||
Assign file contents to the target object | ||
|
||
```javascript | ||
var assignFile = require('assign-file'); | ||
|
||
// foo/bar/baz.txt (Hello!) | ||
|
||
assignFile({foo: {bar: 123}}, 'foo/bar/baz.txt', 'utf8', function(err, res) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
res; | ||
/* => | ||
{ | ||
foo: { | ||
bar: { | ||
baz: 'Hello!' | ||
} | ||
} | ||
}; //=> true | ||
*/ | ||
}); | ||
``` | ||
|
||
## Installation | ||
|
||
[![NPM version](https://badge.fury.io/js/assign-file.svg)](https://www.npmjs.org/package/assign-file) | ||
|
||
[Use npm.](https://www.npmjs.org/doc/cli/npm-install.html) | ||
|
||
``` | ||
npm install assign-file | ||
``` | ||
|
||
## API | ||
|
||
```javascript | ||
var assignFile = require('assign-file'); | ||
``` | ||
|
||
### assignFile(*target*, *filePath* [, *options*], *callback*) | ||
|
||
*target*: `object` | ||
*filePath*: `String` (a relative file path) | ||
*options*: `Object` or `String` (file encoding) | ||
*callback*: `Function` | ||
|
||
It asynchronously reads a file, then assigns the file contents to the target object as a property. | ||
|
||
The names of the assigned properties are based on the file path. For example, | ||
|
||
* `foo.txt` sets `foo` property. | ||
* `foo/bar.txt` sets `foo.bar` property. | ||
* `foo/bar/baz.qux.txt` sets `foo.bar['baz.qux']` property. | ||
* `../foo/bar.txt` sets `['..'].foo.bar` property. | ||
* `foo/../bar/baz.txt` sets `bar.baz` property. | ||
|
||
```javascript | ||
var assert = require('assert'); | ||
var assignFile = require('assign-file'); | ||
|
||
var target = { | ||
fixtures: { | ||
foo: 'bar' | ||
} | ||
}; | ||
|
||
assignFile(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: { // overrides fixtures.foo property | ||
images: { | ||
'00': <Buffer ... > // new property | ||
} | ||
} | ||
}); | ||
}); | ||
``` | ||
|
||
All options and callback function can be used in the same way as [set-property-from-file](https://github.com/shinnn/set-property-from-file#options). The only difference from [set-property-from-file](https://github.com/shinnn/set-property-from-file) is that *assign-file* always overwrites existing ones using [`Object.assign()`](http://www.2ality.com/2014/01/object-assign.html). | ||
|
||
## License | ||
|
||
Copyright (c) 2014 [Shinnosuke Watanabe](https://github.com/shinnn) | ||
|
||
Licensed under [the MIT License](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
init: | ||
- git config --global core.autocrlf input | ||
|
||
version: '{build}' | ||
|
||
environment: | ||
matrix: | ||
- nodejs_version: '0.10' | ||
- nodejs_version: '0.11' | ||
|
||
install: | ||
- ps: Install-Product node $env:nodejs_version | ||
- npm install --production | ||
- npm install nop tape | ||
|
||
build: off | ||
|
||
test_script: | ||
- ps: 'node test\test.js #PowerShell' | ||
- cmd: node test\test.js | ||
|
||
cache: | ||
- node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*! | ||
* assign-file | MIT (c) Shinnosuke Watanabe | ||
* https://github.com/shinnn/assign-file | ||
*/ | ||
'use strict'; | ||
|
||
var objectAssign = require('object-assign'); | ||
var setPropertyFromFile = require('set-property-from-file'); | ||
|
||
module.exports = function assignFile(target, filePath, options, cb) { | ||
if (cb === undefined) { | ||
cb = options; | ||
options = {}; | ||
} | ||
|
||
// Argument validation | ||
objectAssign(target); | ||
|
||
if (typeof cb !== 'function') { | ||
throw new TypeError(cb + ' is not a function. Last argument must be a function.'); | ||
} | ||
|
||
setPropertyFromFile({}, filePath, options, function(err, source) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
|
||
cb(null, objectAssign(target, source)); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "assign-file", | ||
"version": "0.0.0", | ||
"description": "Assign file contents to the target object", | ||
"repository": "shinnn/assign-file", | ||
"author": { | ||
"name": "Shinnosuke Watanabe", | ||
"url": "https://github.com/shinnn" | ||
}, | ||
"scripts": { | ||
"pretest": "jscs *.js test/test.js && eslint *.js test/test.js", | ||
"test": "node test/test.js | tap-spec", | ||
"coverage": "istanbul cover --no-default-excludes -x=node_modules/** test/test.js", | ||
"coveralls": "${npm_package_scripts_coverage} && istanbul-coveralls" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/shinnn/assign-file/blob/master/LICENSE" | ||
} | ||
], | ||
"files": [ | ||
"index.js", | ||
"LICENSE" | ||
], | ||
"keywords": [ | ||
"assign", | ||
"read", | ||
"file", | ||
"content", | ||
"object", | ||
"property", | ||
"set", | ||
"name", | ||
"filename", | ||
"path", | ||
"async", | ||
"asynchronous" | ||
], | ||
"dependencies": { | ||
"object-assign": "^2.0.0", | ||
"set-property-from-file": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^0.10.1", | ||
"istanbul": "^0.3.5", | ||
"istanbul-coveralls": "^1.0.1", | ||
"jscs": "^1.8.1", | ||
"nop": "^1.0.0", | ||
"tap-spec": "^2.1.1", | ||
"tape": "^3.0.3" | ||
}, | ||
"jscsConfig": { | ||
"preset": "google", | ||
"maximumLineLength": 98 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
var noop = require('nop'); | ||
var assignFile = require('../'); | ||
var test = require('tape'); | ||
|
||
test('assignFile()', function(t) { | ||
t.plan(14); | ||
|
||
var obj = {}; | ||
assignFile(obj, '.gitattributes', 'utf8', function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {'.gitattributes': '* text=auto\n'}], | ||
'should set property based on the file name, contents and encoding.' | ||
); | ||
t.strictEqual(obj, res, 'should overwrite the target object.'); | ||
}); | ||
|
||
assignFile({'.gitattributes': ['a']}, '.gitattributes', null, function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {'.gitattributes': new Buffer('* text=auto\n')}], | ||
'should override existing property.' | ||
); | ||
}); | ||
|
||
assignFile({}, 'test/fixtures/a.txt', {}, function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {test: {fixtures: {a: new Buffer('foo\n')}}}], | ||
'should set nested property when the file is under some directories.' | ||
); | ||
}); | ||
|
||
assignFile({test: {fixtures: 1}}, 'test/fixtures/a.txt', function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {test: {fixtures: {a: new Buffer('foo\n')}}}], | ||
'should overwrite the property if needed.' | ||
); | ||
}); | ||
|
||
assignFile({}, 'fixtures/a.txt', { | ||
cwd: 'test', | ||
base: 'fixtures', | ||
encoding: 'base64' | ||
}, function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {a: new Buffer('foo\n').toString('base64')}], | ||
'should reflect `cwd` option and `base` option to the result.' | ||
); | ||
}); | ||
|
||
assignFile({}, '.gitattributes', {base: 'test'}, function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {'..': {'.gitattributes': new Buffer('* text=auto\n')}}], | ||
'should set ".." property when the path starts with "../".' | ||
); | ||
}); | ||
|
||
var option = {encoding: Boolean}; | ||
|
||
assignFile({}, 'index.js', option, function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {index: true}], | ||
'should process file content with a function using `encoding` option.' | ||
); | ||
t.deepEqual(option, {encoding: Boolean}, 'should not modify option object.'); | ||
}); | ||
|
||
assignFile({}, 'test/fixtures/a.txt', {ext: true}, function(err, res) { | ||
t.deepEqual( | ||
[err, res], | ||
[null, {test: {fixtures: {'a.txt': new Buffer('foo\n')}}}], | ||
'should add extension to the property name using `ext` option.' | ||
); | ||
}); | ||
|
||
assignFile({}, 'node_modules', function(err) { | ||
t.equal( | ||
err.code, | ||
'EISDIR', | ||
'should pass an error to the callback when it fails to read a file.' | ||
); | ||
}); | ||
|
||
t.throws( | ||
assignFile.bind(null, null, '.gitattributes', noop), | ||
/TypeError/, | ||
'should throw a type error when the first argument is null or undefined.' | ||
); | ||
|
||
t.throws( | ||
assignFile.bind(null, {}, '/', noop), | ||
/must be a relative path/, | ||
'should throw an error when the path is absolute.' | ||
); | ||
|
||
t.throws( | ||
assignFile.bind(null, {}, '.gitattributes', {}), | ||
/TypeError.*must be a function/, | ||
'should throw a type error when the last argument is not a function.' | ||
); | ||
}); |