0.1.1 • Published 10 years ago

path-segments v0.1.1

Weekly downloads
19
License
-
Repository
github
Last release
10 years ago

path-segments NPM version

Get specific segments of a file path. First, last, last two, first two, etc.

Install

Install with npm:

npm i path-segments --save-dev

Usage

var segements = require('path-segments');

Tests

In the command line, run npm test or mocha.

first

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'

last

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.

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license


This file was generated by verb-cli on June 26, 2014.