1.0.0 โ€ข Published 3 years ago

@danielcobo/path v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

path

Extend native nodeJS path module

๐Ÿงญ Table of contents

โœจ Benefits

  • Extends native path module
  • Normalizes naming

๐ŸŽ’ Requierments

To use this package you will need:

๐Ÿš€ Quickstart

Install

NodeJS

Install using the terminal:

npm install @danielcobo/path

Require the module:

const path = require('@danielcobo/path');

Note: In case you're wondering, @danielcobo/ is just a namespace scope - an NPM feature. Scopes make it easier to name modules and improve security.

Browser

Declare it as a global variable named path by including this script before any script you want to use it in:

<script src="https://cdn.jsdelivr.net/npm/@danielcobo/path@1/dist/iife/path.min.js"></script>

Or import it as an ECMAScript module:

import * as path from 'https://cdn.jsdelivr.net/npm/@danielcobo/path@1/dist/esm/path.min.js';

Also, feel free to download the file if you prefer not to use jsdelivr. In that case just replace the url with the relative file path.

Example use

path.byDepth('have', 'have/a/nice/day', 'have/a/sundae'); //['have','have/a/sundae','have/a/nice/day']
path.dotfile("dir/.git"); //'.git'
path.ext('./ext.js'); //'.js'
path.file('./file.js'); //'file.js'
path.filename('./file.js'); //'file.js'
path.filter(['./hello.txt', './file.js'], '.txt'); //['./hello.txt']
path.isFile('./someFile.js'); //true
path.lvls(['hello', 'foo/bar', 'hello/world', 'hello/world/tidy/']);
/*
[
['hello'],
['foo/bar', 'hello/world'],
['hello/world/tidy/']
]
*/
path.parent('vanilla/chocolate/mint'); //'vanilla/chocolate'
path.rmExt('whatisthis.js'); //'.js'
path.separator = path.sep; //'/' or '\' on Win
path.tree('vanilla/chocolate/mint'); //'vanilla/chocolate/mint'

For details see documentation below.

๐Ÿ“˜ Documentation

.tree()

Returns tree part of path

NameTypeDefaultDescription
strstringPath

Returns

TypeDescription
stringTree

Source: src/tree.js:5

.rmExt()

Remove file extension

NameTypeDefaultDescription
pathstring

Returns

TypeDescription
string

Source: src/rmExt.js:1

.parent()

Get parent folder

NameTypeDefaultDescription
strstringPath

Returns

TypeDescription
stringParent folder

Source: src/parent.js:5

.lvls()

Sorts paths by depth

NameTypeDefaultDescription
pathsArray<string>

Returns

TypeDescription
Array<Array>

Source: src/lvls.js:3

.isFile()

True/false if path is a file

NameTypeDefaultDescription
pathstring

Returns

TypeDescription
boolean

Source: src/isFile.js:4

.filter()

Filter by file extension/s or function

NameTypeDefaultDescription
pathsstringArray<string>
filtersstringArray<string>function
whitelistbooleantrueTrue/false for whitelist/blacklist

Returns

TypeDescription
Array<string>Paths

Source: src/filter.js:3

.file()

Get filename from path (incl. extension)

NameTypeDefaultDescription
strstringPath

Returns

TypeDescription
string

Source: src/file.js:3

.dotfile()

Get dotfile in path

NameTypeDefaultDescription
strstringPath

Returns

TypeDescription
stringIn path

Source: src/dotfile.js:3

.byDepth()

Get paths by depth (number of path separators). Shallow to deepest. Will also sort by name, but lexicographically (numbers should be left padded by zero)

NameTypeDefaultDescription
pathsArray<string>

Returns

TypeDescription
Array<string>By depth

Source: src/byDepth.js:3

๐Ÿ†˜ Troubleshooting

If you run into trouble or have questions just submit an issue.

๐Ÿค Contributing

Anyone can contribute

Contributions come in many shapes and sizes. All are welcome. You can contribute by:

  • asking questions
  • suggesting features
  • sharing this repo with friends
  • improving documentation (even fixing typos counts ๐Ÿ˜‰)
  • providing tutorials (if you do, please let me know, I would love to read them)
  • improving tests
  • contributing code (new features, performance boosts, code readability improvements..)

Rules for contributions

General guidelines:

  • there are no dumb questions
  • be polite and respectful to others
  • do good

When coding remember:

  • working > maintainability > performance
  • best code is no code
  • be descriptive when naming
  • keep it DRY
  • do test

Contribution licence: All contributions are considered to be under same license as this repository.

๐Ÿงช Testing

Testing suite: ๐Ÿƒ Jest | Test command: npm test

Mutation testing suite: ๐Ÿ‘ฝ Stryker Mutator | Mutation test command: npm run mutation

If you intend to develop further or contribute code, then please ensure to write and use testing. Strive for 100% code coverage and high mutation scores. Mutation score 100 is great, but it's not always neccessary (if there are valid reasons).

โš–๏ธ License

MIT License