filenamify
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -1,24 +0,5 @@ | ||
declare namespace filenamify { | ||
interface Options { | ||
/** | ||
String to use as replacement for reserved filename characters. | ||
import filenamify = require('./filenamify'); | ||
import filenamifyPath = require('./filenamify-path'); | ||
Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*` | ||
@default '!' | ||
*/ | ||
readonly replacement?: string; | ||
/** | ||
Truncate the filename to the given length. | ||
Systems generally allow up to 255 characters, but we default to 100 for usability reasons. | ||
@default 100 | ||
*/ | ||
readonly maxLength?: number; | ||
} | ||
} | ||
declare const filenamify: { | ||
declare const filenamifyCombined: { | ||
/** | ||
@@ -40,8 +21,5 @@ Convert a string to a valid filename. | ||
/** | ||
Convert the filename in a path a valid filename and return the augmented path. | ||
*/ | ||
path(path: string, options?: filenamify.Options): string; | ||
path: typeof filenamifyPath; | ||
}; | ||
export = filenamify; | ||
export = filenamifyCombined; |
44
index.js
'use strict'; | ||
const path = require('path'); | ||
const trimRepeated = require('trim-repeated'); | ||
const filenameReservedRegex = require('filename-reserved-regex'); | ||
const stripOuter = require('strip-outer'); | ||
const filenamify = require('./filenamify'); | ||
const filenamifyPath = require('./filenamify-path'); | ||
// Doesn't make sense to have longer filenames | ||
const MAX_FILENAME_LENGTH = 100; | ||
const filenamifyCombined = filenamify; | ||
filenamifyCombined.path = filenamifyPath; | ||
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex | ||
const reRelativePath = /^\.+/; | ||
const filenamify = (string, options = {}) => { | ||
if (typeof string !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
const replacement = options.replacement === undefined ? '!' : options.replacement; | ||
if (filenameReservedRegex().test(replacement) && reControlChars.test(replacement)) { | ||
throw new Error('Replacement string cannot contain reserved filename characters'); | ||
} | ||
string = string.replace(filenameReservedRegex(), replacement); | ||
string = string.replace(reControlChars, replacement); | ||
string = string.replace(reRelativePath, replacement); | ||
if (replacement.length > 0) { | ||
string = trimRepeated(string, replacement); | ||
string = string.length > 1 ? stripOuter(string, replacement) : string; | ||
} | ||
string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string; | ||
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH); | ||
return string; | ||
}; | ||
filenamify.path = (filePath, options) => { | ||
filePath = path.resolve(filePath); | ||
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options)); | ||
}; | ||
module.exports = filenamify; |
{ | ||
"name": "filenamify", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Convert a string to a valid safe filename", | ||
"license": "MIT", | ||
"repository": "sindresorhus/filenamify", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -19,5 +20,13 @@ "engines": { | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"filenamify-path.d.ts", | ||
"filenamify-path.js", | ||
"filenamify.d.ts", | ||
"filenamify.js", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"exports": { | ||
".": "./index.js", | ||
"./browser": "./filenamify.js" | ||
}, | ||
"keywords": [ | ||
@@ -24,0 +33,0 @@ "filename", |
@@ -1,2 +0,2 @@ | ||
# filenamify [![Build Status](https://travis-ci.org/sindresorhus/filenamify.svg?branch=master)](https://travis-ci.org/sindresorhus/filenamify) | ||
# filenamify [![Build Status](https://travis-ci.com/sindresorhus/filenamify.svg?branch=master)](https://travis-ci.com/github/sindresorhus/filenamify) | ||
@@ -7,3 +7,2 @@ > Convert a string to a valid safe filename | ||
## Install | ||
@@ -15,3 +14,2 @@ | ||
## Usage | ||
@@ -29,10 +27,9 @@ | ||
## API | ||
### filenamify(string, [options]) | ||
### filenamify(string, options?) | ||
Convert a string to a valid filename. | ||
### filenamify.path(path, [options]) | ||
### filenamify.path(path, options?) | ||
@@ -43,7 +40,7 @@ Convert the filename in a path a valid filename and return the augmented path. | ||
Type: `Object` | ||
Type: `object` | ||
##### replacement | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `'!'` | ||
@@ -57,10 +54,20 @@ | ||
Type: `boolean`<br> | ||
Type: `number`\ | ||
Default: `100` | ||
Truncate the filename to the given length. | ||
Systems generally allow up to 255 characters, but we default to 100 for usability reasons. | ||
## Browser-only import | ||
You can also import `filenamify/browser`, which only imports `filenamify` and not `filenamify.path`, which relies on `path` being available or polyfilled. Importing `filenamify` this way is therefore useful when it is shipped using `webpack` or similar tools, and if `filenamify.path` is not needed. | ||
```js | ||
const filenamify = require('filenamify/browser'); | ||
filenamify('<foo/bar>'); | ||
//=> 'foo!bar' | ||
``` | ||
## Related | ||
@@ -73,6 +80,1 @@ | ||
- [slugify](https://github.com/sindresorhus/slugify) - Slugify a string | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7529
9
95
75