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

file-path-helper

v1.4.5

Published

Helpful methods for handling file path.

Downloads

18

Readme

File Path Helper

Helpful methods for handling file path.

Table of contents

File Methods

globPromise

This method returns Promise object.

Glob promise.

  • @param string pattern
  • @param GlobOptions options
  • @returns Promise.<Array<string>, Error>

Example

const { globPromise } = require('file-path-helper');

async function getFiles() {
  const files = await globPromise('*.*');
  return files;
}

replaceSeparator

Replace directory separator.

  • @param string path
  • @param Separator separator default: '/'
  • @returns string

trimDir

Append last slash to directory.

  • @param string dir
  • @param Separator separator default: '/'

setDir

Set directory part of path.

  • @param string path
  • @param string dir
  • @param Separator separator default: '/'
  • @returns string

Example

const { setDir } = require('file-path-helper');

const newPath = setDir('dir/old/file.txt', 'new');
// newPath = 'new/file.txt'

Set directory part of path.

getLastNumber

Get last number from path.

  • @param string path
  • @returns string

Example

const { getLastNumber } = require('file-path-helper');

const num = getLastNumber('my-favorite-13.txt');
// num = '13'

removeLastNumber

Remove last number from file name.

  • @param string file
  • @returns string

Example

const { removeLastNumber } = require('file-path-helper');

const file = removeLastNumber('my-favorite-13.txt');
// file = 'my-favorite.txt'

autoIncrease

This method returns Promise object.

If the same file exists, It's returns filename what increased number.

  • @param string path
  • @returns Promise<string> auto increased path.

Example

const { autoIncrease } = require('file-path-helper');

const file = await autoIncrease('dogs.txt');
// file = 'dogs (2).txt';

resolveOutputFile

Resolve output filename using templates such as {name}, {source} or {ext}.

  • @param string output
  • @param string source
  • @returns string

Example

const { resolveOutputFile } = require('file-path-helper');

const output = resolveOutputFile('{name}-fixed.{ext}', 'dogs.txt');
// output = 'dogs-fixed.txt'

bytesToSize

Converts bytes to human readable size. e.g. 10 MB 1.25 GB

  • @param number bytes
  • @param number decimals - default: 2
  • @returns string

Example

const { bytesToSize } = require('file-path-helper');

const fileSize = bytesToSize(2048);
// fileSize = '2 KB'

parseSize

Parses string that includes file size and operator.

interface Size {
  bytes: number;
  operator: string; // '>', '>=', '=' ...
}
  • @param string size - e.g 1kb 10.5 MB >1gb =< 10 kb
  • @returns Size

Example

const { parseSize } = require('file-path-helper');

const size = parseSize('> 1mb');
// size.bytes = 1048576
// size.operator = '>'

String Methods

truncate

Truncate string what given length.

  • @param string str
  • @param number length
  • @param string ellipsis - default: '…'
  • @returns string

Example

const { truncate } = require('file-path-helper');

const str = truncate('1234567890', 6);
// str = '12345…'

sanitize

Sanitize string for filename safe.

  • @param string str
  • @returns string replacer - default: ''

Example

const { sanitize } = require('file-path-helper');

const str = sanitize(' he*llo/_<wo:rld');
// str = 'hello_world'

Array Methods

naturalSort

Sorting array of alphanumerical strings naturally.

  • @param string[] arr
  • @returns string[]

Example

const arr = [
  'test 1.txt',
  'test 11.txt',
  'test 3.txt',
];
const sorted = naturalSort(arr);
// sorted = [
//   'test 1.txt',
//   'test 3.txt',
//   'test 11.txt',
// ];

filter

Filtering an array with Promise.

  • @param T[] arr - filtering target array.
  • @param function(T, number, T[]): Promise<boolean> cb - callback function for filtering. arguments is value, index, array.
  • @returns Promise<T[]>

Example

const arr = [1, 2, 3, 4, 5];
const doSomething = () => Promise.resolve();

const res = await filter(arr, async v => {
  await doSomething();
  return (v % 2) == 1;
});
// res = [1, 3, 5]

chunks

Split array into chunks.

  • @param T[] arr
  • @param number size
  • @returns T[][]

Example

const arr = [1, 2, 3, 4, 5, 6, 7];
const res = chunks(arr, 3);
// res = [[1, 2, 3], [4, 5, 6], [7]]

Date Methods

parseDate

Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.

interface ParsedDate {
  date: Date;
  year: number;
  month: number;
  day: number;
  toDateString: () => string;
}
  • @param string|number|Date value
  • @returns ParsedDate

Example

const parsed = parseDate('feb 17, 1995 03:24:00');
// parsed = {
//   date: new Date('feb 17, 1995 03:24:00'), // Date object
//   year: 1995,
//   month: 2,
//   day: 17,
// }
// parsed.toDateString() // '1995-02-17' ISO date format

getDates

Returns array of date strings.

  • @param string value - date string. e.g. '2020-01-01' or '2020-01-01~2020-01-31'
  • @returns string[]

Example

const dates = getDates('2020-02-01~2020-02-05');
// dates = [
//   '2020-02-01',
//   '2020-02-02',
//   '2020-02-03',
//   '2020-02-04',
//   '2020-02-05',
// ]

diffDays

Returns difference between two dates.

  • @param string|number|Date a
  • @param string|number|Date b
  • @returns number

Example

const days = diffDays('2020-02-24', '2020-03-02');
// days = 7

License

MIT License