0.1.1 • Published 6 years ago

floby v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

floby

npm npm npm

A class to work and manage files

Installation

npm install --save floby

API

Initialize floby on your project:

var floby = require('floby');

file = new floby(path, options)

Create a new file manager object to manage a file. This method accepts the following arguments:

  • path: a string or a pathObject with the path to the file.
  • options: an object with the following optative options:
    • encoding: a string with the file encoding. Default is utf8.
    • reader: a function that will parse the data after reading the file by the read method.
    • writer: a function that will parse the data before writing the file by the write method.

Example with a path string:

var file = new floby('/path/to/my/file.txt', { encoding: 'utf8' });

Same example with a pathObject:

var file = new floby({ dir: '/path/to/my', base: 'file.txt' }, { encoding: 'utf8' });

file.exists(cb)

Check if the file exists. This method will execute the provided callback cb method with an error object if something went wrong, and an exists boolean that indicates if the file exists or not.

file.exists(function(error, exists)
{
  //Check the error 
  if(error){ /* do something with the error */ } 
  
  //Check if the file exists 
  if(exists === true)
  {
    //Do your magic...
  }
});

file.mkdir(cb)

Creates the parent directory of the file. The callback method will be executed with an error object if something went wrong.

file.mkdir(function(error)
{
  //Check the error 
  if(error){ /* something went wrong */ } 
  
  //Parent folder created!
});

file.read(cb)

Read the file content and emit the cb function with the same arguments as the fs.readFile function.

file.read(function(error, data)
{
  //Check for error
  if(error){ /* print the error */ }

  //Do some stuff with your data
  //...
});

file.unlink(cb)

Remove the file and execute the provided callback method with an error object if something went wrong.

file.unlink(function(error)
{
  //Check the error object 
  if(error){ /* something went wrong */ } 
  
});

file.write(data, cb)

Write the data to the file, and emit the cb function with the same arguments as the fs.writeFile function.

If a parse_write function is provided on the options, the data will be parsed with this function and then saved to the file.

file.write(data, function(error)
{
  //Check for error
  if(error){ /* print the error */ }

  //Continue
  //...
});

var jsonFile = new floby.json(path , options)

Create a new JSON file manager. It uses the JSON.parse and JSON.stringify methods to decode and encode the file content.

var iniFile = new floby.ini(path , options)

Create a new INI file manager. It uses the ini package to encode and decode the file content.

License

MIT LICENSE © Josemi Juanes.

0.1.1

6 years ago

0.1.0

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago