npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

defiled

v2.0.2

Published

Class wrapper for interacting with file paths.

Downloads

81

Readme

Build Status downloads npm Code Climate Test Coverage dependencies

defiled

A class wrapper for interacting with file paths.

Installation

npm install --save defiled

Summary

Pass a file and (optionally) it's directory to defiled to get a instance of File back with some helper methods for parsing various parts of it. If you omit the directory, process.cwd() will be used instead.

Example

var File = require('defiled');

var file = new File('foo/bar/banana.js', '/some/absolute/path');
var file2 = new File('baz/quux/apple.js'); // process.cwd() used as directory

API

For these examples, suppose:

var file = new File('fruits/banana.js', '/foo/bar');

File#filename

Get file name and extension (just like path.basename).

file.filename(); // banana.js

File#name

Get the fie name without the extension.

file.name(); // banana

File#ext

Get the file extension. Aliased as File#extention

file.ext(); // .js

File#path

Get the relative part of the path up to the filename.

file.path(); // fruits

File#relative

Get the relative part of the path up to or including the name and extension. Aliased as File#rel.

file.relative(); // fruits/banana.js
file.relative({ ext: false }); // fruits/banana
file.relative({ name: false }); // fruits

Optionally, accepts an options object for controlling the return value (as shown above). Passing { name: false } makes this function work exactly like #path above, but there are still times when you may want to call this instead. #relative accepts some other options to transform the return value. Pass { array: true } to get the path parts split on "/".

file.relative({ array: true }); // ['fruits', 'banana.js']
file.relative({ array: true, name: false }); // ['fruits']

Additionally, you can pass { transform: 'name' } to transform the path. The available transformers are:

  • camel (or camelCase)
  • dash (or kebab or kebabCase)
  • underscore (or snake or snakeCase)
  • title (or startCase or bookCase)
  • human (or capitalize)
  • pipe
  • class
  • lower
  • upper
  • dot

Also note that { ext: false } is implied when using a transform. The extension will never be included in the transformed path.

file.relative({ transform: 'camel' }); // fruitsBanana
file.relative({ transform: 'dash' }); // fruits-banana
file.relative({ transform: 'snake' }); // fruits_banana
file.relative({ transform: 'title' }); // Fruits Banana
file.relative({ transform: 'human' }); // Fruit banana
file.relative({ transform: 'pipe' }); // fruits|banana
file.relative({ transform: 'class' }); // FruitsBanana
file.relative({ transform: 'lower' }); // fruitsbanana
file.relative({ transform: 'upper' }); // FRUITSBANANA
file.relative({ transform: 'dot' }); // fruits.banana

File#absolute

Get the absolute path up to or including the name and extension. Aliased as File#abs. This works just like File#relative (and accepts the same options) but prefixes the relative file with the directory passed in (or the cwd).

file.absolute(); // /foo/bar/fruits/banana.js
file.absolute({ ext: false }); // /foo/bar/fruits/banana
file.absolute({ name: false }); // /foo/bar/fruits
file.absolute({ array: true }); // ['foor', 'bar', 'fruits', 'banana.js'];
file.absolute({ transform: 'camelCase' }); fooBarFruitsBanana

File#parent

Get the absolute path up to but not including the relative path (i.e. the directory containing the relative path originally passed in). Aliased as File#dir. This works just like File#relative and File#absolute (and accepts the same options), but note that { ext: false } and { name: false } are meaningless, as those are already left off the path.

file.parent(); // /foo/bar
file.parent({ array: true }); // ['foo', 'bar']
file.parent({ transform: 'kebab' }); // 'foo-bar'

File#register

Register a custom transformer for this file instance. This takes a name and a function.

file.register('bigSnake', function(path) {
  return path.replace(/\W/g, '__');
});
file.relative({ transform: 'bigSnake' }); // 'fruits__banana'

File#mixin

Register multiple custom transformers for this file instance at the same time. This takes an object where the keys are the names of the transformers and the properties are the functions.

file.mixin({
  bigSnake: function(path) {
    return path.replace(/\W/g, '__');
  }
});
file.relative({ transform: 'bigSnake' }); // 'fruits__banana'

File.register

Just like File#register, but registers the transformer for all future file instances.

File.register('bigKebab', function(path) {
  return path.replace(/\W/g, '--');
});
new File('fruits/banana.js').relative({ transform: 'bigKebab' }); // fruits--banana

File.mixin

Just like File#mixin, but registers the transformer for all future file instances.

File.mixin({
  bigKebab: function(path) {
    return path.replace(/\W/g, '--');
  }
});
new File('fruits/banana.js').relative({ transform: 'bigKebab' }); // fruits--banana

Transformers

You do also have access to the transformers directly via File.transformers.nameOfTransformer (or on an instance via file.transformers.nameOfTransformer), which you can use to transform non-path strings as well.

File.transformers.camelCase('foo-bar-baz'); // fooBarBaz
File.transformers.pipe('foo-bar-baz'); // foo|bar|baz

Contributing

Please see the contribution guidelines.