Get specific segments of a file path. First, last, last two, first two, etc.
Install with npm:
npm i path-segments --save-dev
var segements = require('path-segments');
In the command line, run npm test
or mocha
.
Get the first n
segments of a path:
segments('a/b/c', {first: 1});
//=> 'a'
segments('./a/b/c', {first: 1});
//=> 'a'
segments('/a/b/c.js', {first: 1});
//=> 'a'
segments('/a/b/c/', {first: 1});
//=> 'a'
segments('a\\b\\c', {first: 1});
//=> 'a'
segments('.\\a\\b\\c.js', {first: 1});
//=> 'a'
segments('.\\a\\b\\c\\', {first: 2});
//=> 'a/b'
segments('.\\a\\b\\c.js', {first: 2});
//=> 'a/b'
segments('/a/b/c.js', {first: 3});
//=> 'a/b/c.js'
segments('/a/b/c/', {first: 3});
//=> 'a/b/c'
segments('a\\b\\c', {first: 3});
//=> 'a/b/c'
segments('a\\b\\c\\', {first: 3});
//=> 'a/b/c'
segments('.\\a\\b\\c.js', {first: 3});
//=> 'a/b/c.js'
Get the last n
segments of a path:
segments('a/b/c');
//=> 'c'
segments('./a/b/c');
//=> 'c'
segments('./a/b/c.js');
//=> 'c.js'
segments('/a/b/c.js');
//=> 'c.js'
segments('/a/b/c/');
//=> 'c'
segments('.\\a\\b\\c\\');
//=> 'c'
segments('.\\a\\b\\c.js');
//=> 'c.js'
segments('/a/b/c.js', {last: 2});
//=> 'b/c.js'
segments('/a/b/c/', {last: 2});
//=> 'b/c'
segments('a\\b\\c', {last: 2});
//=> 'b/c'
segments('.\\a\\b\\c.js', {last: 2});
//=> 'b/c.js'
segments('./a/b/c/', {last: 3});
//=> 'a/b/c'
segments('a/b/c.js', {last: 3});
//=> 'a/b/c.js'
segments('.\\a\\b\\c\\', {last: 3});
//=> 'a/b/c'
segments('.\\a\\b\\c.js', {last: 3});
//=> 'a/b/c.js'
See the tests for more examples.
Jon Schlinkert
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on June 26, 2014.