node-file-class
Object-oriented API for interacting with files in node.js
npm install file-class
Include in your project, the export is a single constructor function
var File = require("file-class");
API Documentation
File(location, [options]) constructor
The constructor will create an object representing a file on disk.
Arguments
- location - The location/path to file on disk (absolute is likely preferred)
- options - A hash of additional properties for the object (just extended to this)
- encoding - The encoding for the file (default:
"utf8"
) - parse - A function to parse a file body (default:
null
) - stringify - A function to encode data into a file body (default:
null
)
- encoding - The encoding for the file (default:
var file = "foo.txt"; // or var file = "foo.conf" encoding: "utf8" { /* parse and return output */ } { /* transform and return output */ }; // or even var file = "my-file"; fileencoding = "binary"; // can set properties after initialization
File#read(callback)
Read the entire contents of the file (via fs.readFile()
) and return in a callback.
Arguments
- callback - Arguments provided:
- err - Error object (if relevent)
- contents - The entire contents of the file
file;
File#mkdir(callback)
Create the entire directory tree for this file (via mkdirp
)
Arguments
- callback - Arguments provided:
- err - Error object (if relevent)
file;
File#write(contents, callback)
Write contents
to the file (via fs.writeFile()
)
Arguments
- contents - The data to be written to the file
- callback - Arguments provided:
- err - Error object (if relevent)
file;
File#empty(callback)
Clear the contents of the file (ie. file.write(
"", ...)
)
Arguments
- callback - Arguments provided:
- err - Error object (if relevent)
file;
File#unlink(callback) Alias: del
Delete the file from the filesystem (via fs.unlink(...)
).
Arguments
- callback - Arguments provided:
- err - Error object (if relevent)
file;
File#exists(callback)
Check for this file's existence (via fs.exists(...)
).
Arguments
- callback - Arguments provided:
- exists -
true
/false
- exists -
file;
File#stat(callback)
Get a fs.Stats
object for the file (via fs.stat(...)
).
Arguments
- callback - Arguments provided:
- err - Error object (if relevent)
- stats - fs.Stats object
file;
File.JSONFile(location, [options]) constructor
The constructor will create an object representing a JSON file on disk. This object exposes helper methods for dealing with JSON.
Arguments
- location - same as
File
- options - same as
File
, with some additions:- replacer - A replacer function: see JSON.stringify (default:
null
) - spaces - Number of spaces to use in output: see JSON.stringify (default:
null
)
- replacer - A replacer function: see JSON.stringify (default:
var json = "package.json"; json;
File.JSONFile#merge(data, callback)
Reads the file, uses extend
to merge the data in before writing.
Arguments
- data - The object of data to merge
- callback - Arguments provided:
- err - Error object (if relevent)
- contents - The contents as they were written
json;
File.ListFile(location, [options]) constructor
The constructor will create an object representing a file on disk whose contents are comprised of one item per-line. This object exposes helper methods for dealing with that collection.
Arguments
- location - same as
File
- options
- ignore -
String
,RegExp
,Function
- ignore -
var list = "banned.txt"; list;
File.ListFile#ignore
This property can be added via the options
object in the constructor, or can
be set manually as an object property.
If a String
, any line beginning with that same string will be ignored.
If a RegExp
, any line that matches the regular expression will be ignored.
If a Function
, any line that returns true
when executing the function will be ignored.
NOTE Empty lines (this includes lines that consist only of whitespace) are always ignored.
var list = "banned.txt" ignore: "#" ; // or perhapslistignore = /^#/; // or evenlist { return line0 === "#";}; list;
File.ListFile#indexOf(item, callback)
Determine the index of the item specified in the collection.
Arguments
- item - The item to check
- callback - Arguments provided:
- err - Error object (if relevent)
- index - The 0-indexed line number for that item (-1 if not found)
- list - The complete list (via
this.read(...)
)
list;
File.ListFile#contains(item, callback)
Determine whether or not an item is in the collection at all.
Arguments
- item - The item to check
- callback - Arguments provided:
- err - Error object (if relevent)
- contains -
true
/false
- list - The complete list (via
this.read(...)
)
list;
File.ListFile#add(item, callback)
Add a new item to the collection.
Arguments
- item - The item to add
- callback - Arguments provided:
- err - Error object (if relevent)
list;
File.ListFile#remove(item, callback)
Remove an item from the collection. (either by value, or index)
Notice: this will only remove the first occurence, even if the value occurs multiple times in the file.
Arguments
- item - The item to remove (
Number
: will remove that line via index.String
: will callthis.indexOf()
to determine which line to remove) - callback - Arguments provided:
- err - Error object (if relevent)
list;