First upload version 0.0.1
This commit is contained in:
16
node_modules/filenamify/filenamify-path.d.ts
generated
vendored
Normal file
16
node_modules/filenamify/filenamify-path.d.ts
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
import {type Options} from './filenamify.js';
|
||||
|
||||
/**
|
||||
Convert the filename in a path a valid filename and return the augmented path.
|
||||
|
||||
@example
|
||||
```
|
||||
import {filenamifyPath} from 'filenamify';
|
||||
|
||||
filenamifyPath('foo:bar');
|
||||
//=> 'foo!bar'
|
||||
```
|
||||
*/
|
||||
export default function filenamifyPath(path: string, options?: Options): string;
|
||||
|
||||
export type {Options} from './filenamify.js';
|
||||
7
node_modules/filenamify/filenamify-path.js
generated
vendored
Normal file
7
node_modules/filenamify/filenamify-path.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import path from 'node:path';
|
||||
import filenamify from './filenamify.js';
|
||||
|
||||
export default function filenamifyPath(filePath, options) {
|
||||
filePath = path.resolve(filePath);
|
||||
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options));
|
||||
}
|
||||
37
node_modules/filenamify/filenamify.d.ts
generated
vendored
Normal file
37
node_modules/filenamify/filenamify.d.ts
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
export type Options = {
|
||||
/**
|
||||
String to use as replacement for reserved filename characters.
|
||||
|
||||
Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*`
|
||||
|
||||
@default '!'
|
||||
*/
|
||||
readonly replacement?: string;
|
||||
|
||||
/**
|
||||
Truncate the filename to the given length.
|
||||
|
||||
Only the base of the filename is truncated, preserving the extension. If the extension itself is longer than `maxLength`, you will get a string that is longer than `maxLength`, so you need to check for that if you allow arbitrary extensions.
|
||||
|
||||
Systems generally allow up to 255 characters, but we default to 100 for usability reasons.
|
||||
|
||||
@default 100
|
||||
*/
|
||||
readonly maxLength?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
Convert a string to a valid filename.
|
||||
|
||||
@example
|
||||
```
|
||||
import filenamify from 'filenamify';
|
||||
|
||||
filenamify('<foo/bar>');
|
||||
//=> '!foo!bar!'
|
||||
|
||||
filenamify('foo:"bar"', {replacement: '🐴'});
|
||||
//=> 'foo🐴bar🐴'
|
||||
```
|
||||
*/
|
||||
export default function filenamify(string: string, options?: Options): string;
|
||||
61
node_modules/filenamify/filenamify.js
generated
vendored
Normal file
61
node_modules/filenamify/filenamify.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
import filenameReservedRegex, {windowsReservedNameRegex} from 'filename-reserved-regex';
|
||||
|
||||
// Doesn't make sense to have longer filenames
|
||||
const MAX_FILENAME_LENGTH = 100;
|
||||
|
||||
const reRelativePath = /^\.+(\\|\/)|^\.+$/;
|
||||
const reTrailingPeriods = /\.+$/;
|
||||
|
||||
export default function filenamify(string, options = {}) {
|
||||
const reControlChars = /[\u0000-\u001F\u0080-\u009F]/g; // eslint-disable-line no-control-regex
|
||||
const reRepeatedReservedCharacters = /([<>:"/\\|?*\u0000-\u001F]){2,}/g; // eslint-disable-line no-control-regex
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
if (replacement.length > 0) {
|
||||
string = string.replace(reRepeatedReservedCharacters, '$1');
|
||||
}
|
||||
|
||||
string = string.normalize('NFD');
|
||||
string = string.replace(reRelativePath, replacement);
|
||||
string = string.replace(filenameReservedRegex(), replacement);
|
||||
string = string.replace(reControlChars, replacement);
|
||||
string = string.replace(reTrailingPeriods, '');
|
||||
|
||||
if (replacement.length > 0) {
|
||||
const startedWithDot = string[0] === '.';
|
||||
|
||||
// We removed the whole filename
|
||||
if (!startedWithDot && string[0] === '.') {
|
||||
string = replacement + string;
|
||||
}
|
||||
|
||||
// We removed the whole extension
|
||||
if (string[string.length - 1] === '.') {
|
||||
string += replacement;
|
||||
}
|
||||
}
|
||||
|
||||
string = windowsReservedNameRegex().test(string) ? string + replacement : string;
|
||||
const allowedLength = typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH;
|
||||
if (string.length > allowedLength) {
|
||||
const extensionIndex = string.lastIndexOf('.');
|
||||
if (extensionIndex === -1) {
|
||||
string = string.slice(0, allowedLength);
|
||||
} else {
|
||||
const filename = string.slice(0, extensionIndex);
|
||||
const extension = string.slice(extensionIndex);
|
||||
string = filename.slice(0, Math.max(1, allowedLength - extension.length)) + extension;
|
||||
}
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
3
node_modules/filenamify/index.d.ts
generated
vendored
Normal file
3
node_modules/filenamify/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export {default} from './filenamify.js';
|
||||
export * from './filenamify.js';
|
||||
export {default as filenamifyPath} from './filenamify-path.js';
|
||||
2
node_modules/filenamify/index.js
generated
vendored
Normal file
2
node_modules/filenamify/index.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {default} from './filenamify.js';
|
||||
export {default as filenamifyPath} from './filenamify-path.js';
|
||||
9
node_modules/filenamify/license
generated
vendored
Normal file
9
node_modules/filenamify/license
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
53
node_modules/filenamify/package.json
generated
vendored
Normal file
53
node_modules/filenamify/package.json
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"name": "filenamify",
|
||||
"version": "6.0.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": "https://sindresorhus.com"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./index.js",
|
||||
"./browser": "./filenamify.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"filenamify-path.d.ts",
|
||||
"filenamify-path.js",
|
||||
"filenamify.d.ts",
|
||||
"filenamify.js",
|
||||
"index.d.ts",
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"filename",
|
||||
"safe",
|
||||
"sanitize",
|
||||
"file",
|
||||
"name",
|
||||
"string",
|
||||
"path",
|
||||
"filepath",
|
||||
"convert",
|
||||
"valid",
|
||||
"dirname"
|
||||
],
|
||||
"dependencies": {
|
||||
"filename-reserved-regex": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^5.2.0",
|
||||
"tsd": "^0.28.1",
|
||||
"xo": "^0.54.1"
|
||||
}
|
||||
}
|
||||
83
node_modules/filenamify/readme.md
generated
vendored
Normal file
83
node_modules/filenamify/readme.md
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
# filenamify
|
||||
|
||||
> Convert a string to a valid safe filename
|
||||
|
||||
On Unix-like systems, `/` is reserved. On Windows, [`<>:"/\|?*`](http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29#naming_conventions) along with trailing periods are reserved.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install filenamify
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import filenamify from 'filenamify';
|
||||
|
||||
filenamify('<foo/bar>');
|
||||
//=> '!foo!bar!'
|
||||
|
||||
filenamify('foo:"bar"', {replacement: '🐴'});
|
||||
//=> 'foo🐴bar🐴'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### filenamify(string, options?)
|
||||
|
||||
Convert a string to a valid filename.
|
||||
|
||||
### filenamifyPath(path, options?)
|
||||
|
||||
Convert the filename in a path a valid filename and return the augmented path.
|
||||
|
||||
```js
|
||||
import {filenamifyPath} from 'filenamify';
|
||||
|
||||
filenamifyPath('foo:bar');
|
||||
//=> 'foo!bar'
|
||||
```
|
||||
|
||||
#### options
|
||||
|
||||
Type: `object`
|
||||
|
||||
##### replacement
|
||||
|
||||
Type: `string`\
|
||||
Default: `'!'`
|
||||
|
||||
String to use as replacement for reserved filename characters.
|
||||
|
||||
Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*`
|
||||
|
||||
##### maxLength
|
||||
|
||||
Type: `number`\
|
||||
Default: `100`
|
||||
|
||||
Truncate the filename to the given length.
|
||||
|
||||
Only the base of the filename is truncated, preserving the extension. If the extension itself is longer than `maxLength`, you will get a string that is longer than `maxLength`, so you need to check for that if you allow arbitrary extensions.
|
||||
|
||||
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 `filenamifyPath`, 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 `filenamifyPath` is not needed.
|
||||
|
||||
```js
|
||||
import filenamify from 'filenamify/browser';
|
||||
|
||||
filenamify('<foo/bar>');
|
||||
//=> '!foo!bar!'
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- [filenamify-cli](https://github.com/sindresorhus/filenamify-cli) - CLI for this module
|
||||
- [filenamify-url](https://github.com/sindresorhus/filenamify-url) - Convert a URL to a valid filename
|
||||
- [valid-filename](https://github.com/sindresorhus/valid-filename) - Check if a string is a valid filename
|
||||
- [unused-filename](https://github.com/sindresorhus/unused-filename) - Get a unused filename by appending a number if it exists
|
||||
- [slugify](https://github.com/sindresorhus/slugify) - Slugify a string
|
||||
Reference in New Issue
Block a user