Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 12.20 and move to ESM #25

Merged
merged 9 commits into from Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Expand Up @@ -10,10 +10,9 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
6 changes: 2 additions & 4 deletions filenamify-path.d.ts
@@ -1,8 +1,6 @@
import filenamify = require('./filenamify');
import {Options} from './filenamify.js';

/**
Convert the filename in a path a valid filename and return the augmented path.
*/
declare const filenamifyPath: (path: string, options?: filenamify.Options) => string;

export = filenamifyPath;
export default function filenamifyPath(path: string, options?: Options): string;
11 changes: 4 additions & 7 deletions filenamify-path.js
@@ -1,10 +1,7 @@
'use strict';
const path = require('path');
const filenamify = require('./filenamify');
import path from 'node:path';
import filenamify from './filenamify.js';

const filenamifyPath = (filePath, options) => {
export default function filenamifyPath(filePath, options) {
filePath = path.resolve(filePath);
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options));
};

module.exports = filenamifyPath;
}
34 changes: 15 additions & 19 deletions filenamify.d.ts
@@ -1,31 +1,29 @@
declare namespace filenamify {
interface Options {
/**
String to use as replacement for reserved filename characters.
export interface Options {
/**
String to use as replacement for reserved filename characters.

Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*`
Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*`

@default '!'
*/
readonly replacement?: string;
@default '!'
*/
readonly replacement?: string;

/**
Truncate the filename to the given length.
/**
Truncate the filename to the given length.

Systems generally allow up to 255 characters, but we default to 100 for usability reasons.
Systems generally allow up to 255 characters, but we default to 100 for usability reasons.

@default 100
*/
readonly maxLength?: number;
}
@default 100
*/
readonly maxLength?: number;
}

/**
Convert a string to a valid filename.

@example
```
import filenamify = require('filenamify');
import filenamify from 'filenamify';

filenamify('<foo/bar>');
//=> 'foo!bar'
Expand All @@ -34,6 +32,4 @@ filenamify('foo:"bar"', {replacement: '🐴'});
//=> 'foo🐴bar'
```
*/
declare const filenamify: (string: string, options?: filenamify.Options) => string;

export = filenamify;
export default function filenamify(string: string, options?: Options): string;
15 changes: 6 additions & 9 deletions filenamify.js
@@ -1,16 +1,15 @@
'use strict';
const trimRepeated = require('trim-repeated');
const filenameReservedRegex = require('filename-reserved-regex');
const stripOuter = require('strip-outer');
import trimRepeated from 'trim-repeated';
import filenameReservedRegex from 'filename-reserved-regex';
import stripOuter from 'strip-outer';

// Doesn't make sense to have longer filenames
const MAX_FILENAME_LENGTH = 100;

const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex
const reControlChars = /[\u0000-\u001F\u0080-\u009F]/g; // eslint-disable-line no-control-regex
const reRelativePath = /^\.+/;
const reTrailingPeriods = /\.+$/;

const filenamify = (string, options = {}) => {
export default function filenamify(string, options = {}) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
Expand All @@ -35,6 +34,4 @@ const filenamify = (string, options = {}) => {
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH);

return string;
};

module.exports = filenamify;
}
26 changes: 5 additions & 21 deletions index.d.ts
@@ -1,24 +1,8 @@
import filenamify = require('./filenamify');
import filenamifyPath = require('./filenamify-path');
import filenamify from './filenamify.js';
import filenamifyPath from './filenamify-path.js';

declare const filenamifyCombined: {
/**
Convert a string to a valid filename.
export default filenamify;

@example
```
import filenamify = require('filenamify');

filenamify('<foo/bar>');
//=> 'foo!bar'

filenamify('foo:"bar"', {replacement: '🐴'});
//=> 'foo🐴bar'
```
*/
(string: string, options?: filenamify.Options): string;

path: typeof filenamifyPath;
export {
filenamifyPath
};

export = filenamifyCombined;
12 changes: 6 additions & 6 deletions index.js
@@ -1,8 +1,8 @@
'use strict';
const filenamify = require('./filenamify');
const filenamifyPath = require('./filenamify-path');
import filenamify from './filenamify.js';
import filenamifyPath from './filenamify-path.js';

const filenamifyCombined = filenamify;
filenamifyCombined.path = filenamifyPath;
export default filenamify;

module.exports = filenamify;
export {
filenamifyPath
};
Richienb marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions index.test-d.ts
@@ -1,6 +1,6 @@
import {expectType} from 'tsd';
import filenamify = require('.');
import filenamify, {filenamifyPath} from './index.js';

expectType<string>(filenamify('<foo/bar>'));
expectType<string>(filenamify('foo:"bar"', {replacement: '🐴'}));
expectType<string>(filenamify.path('/some/!path'));
expectType<string>(filenamifyPath('/some/!path'));
17 changes: 9 additions & 8 deletions package.json
Expand Up @@ -10,8 +10,13 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
".": "./index.js",
"./browser": "./filenamify.js"
},
"engines": {
"node": ">=8"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -24,10 +29,6 @@
"index.d.ts",
"index.js"
],
"exports": {
".": "./index.js",
"./browser": "./filenamify.js"
},
"keywords": [
"filename",
"safe",
Expand All @@ -47,8 +48,8 @@
"trim-repeated": "^1.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.40.3"
}
}
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -13,7 +13,7 @@ $ npm install filenamify
## Usage

```js
const filenamify = require('filenamify');
import filenamify from 'filenamify';

filenamify('<foo/bar>');
//=> 'foo!bar'
Expand All @@ -28,7 +28,7 @@ filenamify('foo:"bar"', {replacement: '🐴'});

Convert a string to a valid filename.

### filenamify.path(path, options?)
### filenamifyPath(path, options?)

Convert the filename in a path a valid filename and return the augmented path.

Expand Down Expand Up @@ -59,7 +59,7 @@ Systems generally allow up to 255 characters, but we default to 100 for usabilit
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');
import filenamify from 'filenamify/browser';

filenamify('<foo/bar>');
//=> 'foo!bar'
Expand Down
11 changes: 7 additions & 4 deletions test.js
@@ -1,6 +1,9 @@
import path from 'path';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import filenamify from '.';
import filenamify, {filenamifyPath} from './index.js';

const directoryName = path.dirname(fileURLToPath(import.meta.url));

test('filnamify()', t => {
t.is(filenamify('foo/bar'), 'foo!bar');
Expand All @@ -24,8 +27,8 @@ test('filnamify()', t => {
t.is(filenamify('c/n', {replacement: 'con'}), 'cconn');
});

test('filenamify.path()', t => {
t.is(path.basename(filenamify.path(path.join(__dirname, 'foo:bar'))), 'foo!bar');
test('filenamifyPath()', t => {
t.is(path.basename(filenamifyPath(path.join(directoryName, 'foo:bar'))), 'foo!bar');
});

test('filenamify length', t => {
Expand Down