globdule

1.0.12 • Public • Published

globdule

Provide a filename following naming convention, receive a data object back.

Installation

$ npm install globdule --save-dev

Why?

  • Configuring data in the filename is convenient, readable, fexible and saves the need for an additional data file.
  • Formalises a convention for creating custom filters that can be reused in future projects.
  • See gifle for an example of globdule being used in a project.

Usage

Feed a string representing a filename though a series of filters:

var globdule = require('globdule');

var data = globdule.feed('export-this-movie-20fps-250x300.mov').to('ext').to('fps').to('dims').to('leftoversToSlug').end();

Result:

{  
  ext: '.mov',
  fps: 20,
  width: 250,
  height: 300,
  slug: 'export-this-movie',
  _input: 'export-this-movie-20fps-250x300.mov',
  _leftovers: '' 
}

This example uses core filters that come with globdule, though often you'll be creating your own custom filters.

Filters

A filter is a function that that receives a |glob| string object and a |data| object. It can get info from the string and append it to the data object, then alter the string for use by future filters by extracting the portion from which it obtained data.

In this example the file extension is extracted from a filename:

var extFilter = function(glob, data){
    
    var arr = glob.split('.');
    if (arr.length === 1){
        return false;
    }
    
    data.ext = '.' + arr[arr.length - 1];
    
    return glob.substr(0, glob.length - data.ext.length);

};

If the filter opts to not process the string it returns false.

The filter can optionally be registered with an identifier for future use:

globdule.defineFilter('ext', extFilter);

Then the filter can be invoked referencing this identifier:

var dataA = globdule.feed('textDoc.txt').to('ext').end();
console.log(dataA);
var dataB = globdule.feed('someData.yml').to('ext').end();
console.log(dataB);

Result:

{ ext: '.txt', _input: 'textDoc.txt', _leftovers: 'textDoc' }
{ ext: '.yml', _input: 'someData.txt', _leftovers: 'someData' }

Alternately the filter function can be passed along without being registered first. This is useful for one-off filters:

var data = globdule.feed('textDoc.txt').to(extFilter).end();

Result:

{ ext: '.txt', _input: 'textDoc.txt', _leftovers: 'textDoc' }

Or declared anonymously as you go:

var data = globdule.feed('textDoc.txt').to(function(glob, data){
    
    var arr = glob.split('.');
    if (arr.length === 1){
        return false;
    }
    
    data.ext = '.' + arr[arr.length - 1];
    
    return glob.substr(0, glob.length - data.ext.length);

}).end();

Result:

{ ext: '.txt', _input: 'textDoc.txt', _leftovers: 'textDoc' }

Params

A filter can be registered with default params that inform the filtering function.

Eg:

var extFilter = function(glob, data, params){
    
    var arr = glob.split('.');
    if (arr.length === 1){
        return false;
    }
    
    data.ext = (params.includeDot ? '.' : '') + arr[arr.length - 1];
    
    return glob.substr(0, glob.length - data.ext.length);

};
globdule.defineFilter('ext', extFilter, {includeDot: true});

When invoking the filter you can include any params to override default values:

var data = globdule.feed('textDoc.txt').to('ext', {includeDot: false}).end();

Methods

filterExists(key)

Returns: boolean result if filter has already been registered.

Release History

  • v1.0.11 - Updated dependencies
  • v1.0.9 - Added 'preserveCase' param to 'leftoversToTextFilter'
  • v1.0.8 - Added 'preserveCase' param to 'leftoversToSlugFilter'
  • v1.0.7 – Added |filterExists| method

Package Sidebar

Install

npm i globdule

Weekly Downloads

1

Version

1.0.12

License

MIT

Unpacked Size

30.8 kB

Total Files

6

Last publish

Collaborators

  • loksland