Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
file-path-helper
Advanced tools
Helpful methods for handling file path.
This method returns Promise object.
Glob promise.
string
patternGlobOptions
optionsPromise.<Array<string>, Error>
Example
const { globPromise } = require('file-path-helper');
async function getFiles() {
const files = await globPromise('*.*');
return files;
}
Replace directory separator.
string
pathSeparator
separator default: '/'string
Append last slash to directory.
string
dirSeparator
separator default: '/'Set directory part of path.
string
pathstring
dirSeparator
separator default: '/'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.
Get last number from path.
string
pathstring
Example
const { getLastNumber } = require('file-path-helper');
const num = getLastNumber('my-favorite-13.txt');
// num = '13'
Remove last number from file name.
string
filestring
Example
const { removeLastNumber } = require('file-path-helper');
const file = removeLastNumber('my-favorite-13.txt');
// file = 'my-favorite.txt'
This method returns Promise object.
If the same file exists, It's returns filename what increased number.
string
pathPromise<string>
auto increased path.Example
const { autoIncrease } = require('file-path-helper');
const file = await autoIncrease('dogs.txt');
// file = 'dogs (2).txt';
Resolve output filename using templates such as {name}
, {source}
or {ext}
.
string
outputstring
sourcestring
Example
const { resolveOutputFile } = require('file-path-helper');
const output = resolveOutputFile('{name}-fixed.{ext}', 'dogs.txt');
// output = 'dogs-fixed.txt'
Converts bytes to human readable size. e.g. 10 MB
1.25 GB
number
bytesnumber
decimals - default: 2string
Example
const { bytesToSize } = require('file-path-helper');
const fileSize = bytesToSize(2048);
// fileSize = '2 KB'
Parses string that includes file size and operator.
interface Size {
bytes: number;
operator: string; // '>', '>=', '=' ...
}
string
size - e.g 1kb
10.5 MB
>1gb
=< 10 kb
Size
Example
const { parseSize } = require('file-path-helper');
const size = parseSize('> 1mb');
// size.bytes = 1048576
// size.operator = '>'
Truncate string what given length.
string
strnumber
lengthstring
ellipsis - default: '…'
string
Example
const { truncate } = require('file-path-helper');
const str = truncate('1234567890', 6);
// str = '12345…'
Sanitize string for filename safe.
string
strstring
replacer - default: ''
Example
const { sanitize } = require('file-path-helper');
const str = sanitize(' he*llo/_<wo:rld');
// str = 'hello_world'
Sorting array of alphanumerical strings naturally.
string[]
arrstring[]
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',
// ];
Filtering an array with Promise
.
T[]
arr - filtering target array.function(T, number, T[]): Promise<boolean>
cb - callback function for filtering. arguments is value, index, array.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]
Split array into chunks.
T[]
arrnumber
sizeT[][]
Example
const arr = [1, 2, 3, 4, 5, 6, 7];
const res = chunks(arr, 3);
// res = [[1, 2, 3], [4, 5, 6], [7]]
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;
}
string
|number
|Date
valueParsedDate
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
Returns array of date strings.
string
value - date string. e.g. '2020-01-01' or '2020-01-01~2020-01-31'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',
// ]
Returns difference between two dates.
string
|number
|Date
astring
|number
|Date
bnumber
Example
const days = diffDays('2020-02-24', '2020-03-02');
// days = 7
FAQs
Helpful methods for handling file path.
We found that file-path-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.