Comparing version 0.1.0 to 0.1.1
30
index.js
@@ -33,3 +33,3 @@ "use strict"; | ||
var openUnique = function(tryNum, head, padLeft, pad, padRight, tail, mode, cb) { | ||
var openUniqueHandler = function(tryNum, head, padLeft, pad, padRight, tail, mode, cb) { | ||
var file = tryNum ? (head + padLeft + padNum(tryNum, pad) + padRight + tail) : (head + tail); | ||
@@ -39,3 +39,3 @@ | ||
if(err && err.errno === 47) { | ||
openUnique(++tryNum, head, padLeft, pad, padRight, tail, mode, cb); | ||
openUniqueHandler(++tryNum, head, padLeft, pad, padRight, tail, mode, cb); | ||
} else { | ||
@@ -47,6 +47,6 @@ cb(err, fd); | ||
var writeFileUnique = function(filename, data, options, cb) { | ||
var openUnique = function(filename, mode, cb) { | ||
if(cb === undefined) { | ||
cb = options; | ||
options = { encoding: 'utf8', mode: 438 /*=0666*/ }; | ||
cb = mode; | ||
mode = 438; | ||
} | ||
@@ -59,3 +59,12 @@ | ||
openUnique(0, filename[1], filename[2], filename[3].length, filename[4], filename[5], options.mode, function(err, fd) { | ||
openUniqueHandler(0, filename[1], filename[2], filename[3].length, filename[4], filename[5], mode, cb); | ||
}; | ||
var writeFileUnique = function(filename, data, options, cb) { | ||
if(cb === undefined) { | ||
cb = options; | ||
options = { encoding: 'utf8', mode: 438 /*=0666*/ }; | ||
} | ||
openUnique(filename, options.mode, function(err, fd) { | ||
var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, options.encoding || 'utf8'); | ||
@@ -73,9 +82,3 @@ writeAll(fd, buffer, 0, buffer.length, 0, cb); | ||
UniqueWriteStream.prototype.open = function() { | ||
var filename = rx.exec(this.path); | ||
if(!filename) { | ||
this.destroy(); | ||
this.emit('error', new Error("Can't find a counter pattern in filename")); | ||
} | ||
openUnique(0, filename[1], filename[2], filename[3].length, filename[4], filename[5], this.mode, function(err, fd) { | ||
openUnique(this.path, this.mode, function(err, fd) { | ||
if (err) { | ||
@@ -97,2 +100,3 @@ this.destroy(); | ||
module.exports = { | ||
openUnique: openUnique, | ||
writeFileUnique: writeFileUnique, | ||
@@ -99,0 +103,0 @@ createUniqueWriteStream: createUniqueWriteStream |
{ | ||
"name": "fsu", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Unique file name with streams support", | ||
@@ -5,0 +5,0 @@ "license" : "MIT", |
@@ -9,3 +9,14 @@ #fsu (fs unique) | ||
## writeFileUnique(filename, data, [options], callback) | ||
## openUnique(path, [mode], callback) | ||
Same as [fs.open](http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback) but open for writing and creates unique filename. | ||
```js | ||
var fsu = require('fsu'); | ||
fsu.openUnique("text{_###}.txt", function(err, fd) { | ||
//now we can use file descriptor as usual | ||
}); | ||
``` | ||
## writeFileUnique(path, data, [options], callback) | ||
Same as [fs.writeFile](http://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback) but creates unique filename. | ||
@@ -19,3 +30,2 @@ | ||
}); | ||
``` | ||
@@ -29,3 +39,2 @@ | ||
var stream = fsu.createUniqueWriteStream("text{_###}.txt"); | ||
``` | ||
@@ -36,3 +45,3 @@ | ||
If we run first example several times filenames will be | ||
If we run second example several times filenames will be | ||
``` | ||
@@ -39,0 +48,0 @@ text.txt |
5822
92
51