Browse files

Added a DirectoryEntry constructor for fs.readdir calls.

DirectoryEntry contains information about the item within a directory (either a file or a subdirectory).
  • Loading branch information...
1 parent ad0ef43 commit 13f8ccd7880fedd30b69e46848e14669c7b47e17 @matthewp committed Jan 15, 2013
Showing with 66 additions and 26 deletions.
  1. +29 −2 README.md
  2. +1 −0 component.json
  3. +10 −0 directory_entry.js
  4. +4 −1 filesystem.js
  5. +10 −1 index.js
  6. +4 −21 indexeddb.js
  7. +8 −1 test/test.dir.js
View
31 README.md
@@ -34,10 +34,37 @@ Retrieves the file with the name ``fileName`` and calls the ``callback``. The fi
Removes the file with the name ``fileName`` from storage and calls the ``callback``. The callback is called even if the file doesn't exist.
+### fs.readdir(directoryName, callback)
+
+Gets the contents of ``directoryName`` (should be the full path) and calls the ``callback``. The callback will contain an array of ``DirectoryEntry`` objects (see below).
+
+### DirectoryEntry
+
+A ``DirectoryEntry`` object is passed to the callback of ``fs.readdir`` and represents either a **file** or a **directory**. A DirectoryEntry instance contains these properties/methods:
+
+#### path
+
+The ``path`` property is the full path (including file name) for the given file/directory entry.
+
+#### name
+
+The ``name`` of the given entry, either the file or directory name.
+
+#### dir
+
+The given directory that the file/directory sits in.
+
+#### type
+
+The ``type`` of the entry, either **file** or **directory**.
+
+#### readFile(callback)
+
+A convenience method for calling ``readFile(fileName, callback)``. Throws a TypeError if the entry is not of ``type`` **file**.
+
## IN PROGRESS
This component is still in very early stages, but plans are to include methods such as:
* readString - Get a file and return it as a string.
-* readJson - Get a file and return it as an object.
-* readDir - List the contents of a directory.
+* readJson - Get a file and return it as an object.
View
1 component.json
@@ -11,6 +11,7 @@
"license": "MIT",
"scripts": [
"index.js",
+ "directory_entry.js",
"filesystem.js",
"indexeddb.js"
]
View
10 directory_entry.js
@@ -0,0 +1,10 @@
+var path = require('path');
+
+function DirectoryEntry(fullPath, type) {
+ this.path = fullPath;
+ this.name = path.basename(fullPath);
+ this.dir = path.dirname(fullPath);
+ this.type = type;
+}
+
+module.exports = DirectoryEntry;
View
5 filesystem.js
@@ -94,7 +94,10 @@ exports.readdir = function (directoryName, callback) {
callback(null, entries);
} else {
results = Array.prototype.slice.call(results || []);
- entries = entries.concat(results);
+ entries = entries.concat(results.map(function(result) {
+ return new exports.DirectoryEntry(result.fullPath,
+ result.isDirectory ? 'directory' : 'file');
+ }));
readEntries();
}
});
View
11 index.js
@@ -1,3 +1,12 @@
module.exports = (window.requestFileSystem || window.webkitRequestFileSystem)
? require('./filesystem')
- : require('./indexeddb');
+ : require('./indexeddb');
+
+module.exports.DirectoryEntry = require('./directory_entry');
+
+module.exports.DirectoryEntry.prototype.readFile = function (callback) {
+ if (this.type !== 'file') {
+ throw new TypeError('Not a file.');
+ }
+ return module.exports.readFile(this.path, callback);
+};
View
25 indexeddb.js
@@ -48,31 +48,12 @@ exports.readFile = function (fileName, callback) {
});
};
-var writeObject = function (name, type, data, callback) {
- initOS('readwrite', function (os) {
- var dir = fileName.match(dirRegExp);
-
- var req = os.put({
- "path": fileName,
- "dir": (dir && dir[0]) || '/',
- "data": data
- });
-
- req.onerror = function (e) {
- callback(e);
- };
-
- req.onsuccess = function (e) {
- callback(null);
- };
- });
-};
-
exports.writeFile = function (fileName, data, callback) {
initOS('readwrite', function (os) {
var req = os.put({
"path": fileName,
"dir": path.dirname(fileName),
+ "type": "file",
"data": data
});
@@ -115,7 +96,9 @@ exports.readdir = function (directoryName, callback) {
req.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
- results.push(cursor.value);
+ var value = cursor.value;
+ var entry = new exports.DirectoryEntry(value.path, value.type);
+ results.push(entry);
cursor.continue();
} else {
callback(null, results);
View
9 test/test.dir.js
@@ -27,7 +27,8 @@ describe('Empty directory', function () {
});
describe('Directory with files', function () {
- var fs = require('fs');
+ var fs = require('fs'),
+ DirectoryEntry = fs.DirectoryEntry;
var err, files;
before(function (done) {
@@ -51,6 +52,12 @@ describe('Directory with files', function () {
assert(Array.isArray(files));
});
+ it('Should contain DirectoryEntry objects', function () {
+ assert(files.every(function (item) {
+ return item instanceof DirectoryEntry;
+ }));
+ });
+
it('Should have 2 files in the directory.', function () {
assert(files.length === 2);
});

0 comments on commit 13f8ccd

Please sign in to comment.