file-system
Advanced tools
Comparing version 1.2.6 to 1.2.7
@@ -8,3 +8,4 @@ module.exports = function(grunt) { | ||
'vendor/**/*.js', | ||
'./*.js' | ||
'./*.js', | ||
'!./test.js' | ||
] | ||
@@ -11,0 +12,0 @@ } |
34
index.js
@@ -370,2 +370,36 @@ /** | ||
}); | ||
}; | ||
function base64(filename, data) { | ||
var extname = path.extname(filename).substr(1); | ||
extname = extname || 'png'; | ||
var baseType = { | ||
jpg: 'jpeg' | ||
}; | ||
var type = baseType[extname] ? baseType[extname] : extname; | ||
return 'data:image/' + type + ';base64,' + new Buffer(data, 'binary').toString('base64'); | ||
} | ||
/** | ||
* @description | ||
* Get image file base64 data | ||
*/ | ||
exports.base64 = function(filename, callback) { | ||
if (!callback) callback = util.noop; | ||
fs.readFile(filename, { encoding: 'binary' }, function(err, data) { | ||
if (err) return callback(err); | ||
callback(null, base64(filename, data)); | ||
}); | ||
}; | ||
/** | ||
* @description | ||
* The api same as base64, but it's synchronous | ||
*/ | ||
exports.base64Sync = function(filename) { | ||
var data = fs.readFileSync(filename); | ||
return base64(filename, data); | ||
}; |
{ | ||
"name": "file-system", | ||
"version": "1.2.6", | ||
"version": "1.2.7", | ||
"description": "Strengthen the ability of file system", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"maker", | ||
"node file" | ||
"node file", | ||
"base64" | ||
], | ||
@@ -22,0 +23,0 @@ "author": "douzi <liaowei08@gmail.com>", |
@@ -66,13 +66,3 @@ # file-system — Simplified file system | ||
``` | ||
#### filter description | ||
* `*.js` only match js files in current dir. | ||
* `**/*.js` match all js files. | ||
* `path/*.js` match js files in path. | ||
* `!*.js` exclude js files in current dir. | ||
* ``.{jpg,png,gif}`` means jpg, png or gif | ||
``` | ||
'**/*' // Match all files | ||
'!**/*.js' // Exclude all js files | ||
'**/*.{jpg,png,gif}' // Match jpg, png, or gif files | ||
``` | ||
[filter params description](https://github.com/douzi8/file-match#filter-description) | ||
@@ -123,2 +113,14 @@ ### file.recurseSync | ||
file.copySync('path', 'path', { filter: ['*.html.js'], process: function(contents, filepath) {} }); | ||
``` | ||
### file.base64 | ||
Read image file, callback with base64 data | ||
```js | ||
file.base64('img.png', function(err, data) { | ||
}); | ||
``` | ||
### file.base64Sync | ||
```js | ||
var base64 = file.base64Sync('img.png'); | ||
``` |
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
26596
12
770
124