Skip to content

jonschlinkert/path-segments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Get n specific segments of a file path, e.g. first 2, last 3, etc.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published