From c2000ac7dafab7dc2287df896efef8142eba3aa5 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Mon, 5 Jul 2021 19:39:28 +1200 Subject: [PATCH] Require Node.js 12.20 and move to ESM (#25) --- .github/workflows/main.yml | 3 +-- filenamify-path.d.ts | 6 ++---- filenamify-path.js | 11 ++++------- filenamify.d.ts | 34 +++++++++++++++------------------- filenamify.js | 15 ++++++--------- index.d.ts | 26 +++++--------------------- index.js | 10 ++-------- index.test-d.ts | 4 ++-- package.json | 17 +++++++++-------- readme.md | 6 +++--- test.js | 11 +++++++---- 11 files changed, 56 insertions(+), 87 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..48143fc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/filenamify-path.d.ts b/filenamify-path.d.ts index 2346602..b10107c 100644 --- a/filenamify-path.d.ts +++ b/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; diff --git a/filenamify-path.js b/filenamify-path.js index 359c119..101e0f5 100644 --- a/filenamify-path.js +++ b/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; +} diff --git a/filenamify.d.ts b/filenamify.d.ts index cc17d11..be19a5f 100644 --- a/filenamify.d.ts +++ b/filenamify.d.ts @@ -1,23 +1,21 @@ -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; } /** @@ -25,7 +23,7 @@ Convert a string to a valid filename. @example ``` -import filenamify = require('filenamify'); +import filenamify from 'filenamify'; filenamify(''); //=> 'foo!bar' @@ -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; diff --git a/filenamify.js b/filenamify.js index a548430..9653be6 100644 --- a/filenamify.js +++ b/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'); } @@ -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; +} diff --git a/index.d.ts b/index.d.ts index 4aea46f..caa2605 100644 --- a/index.d.ts +++ b/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' - - filenamify('foo:"bar"', {replacement: '🐴'}); - //=> 'foo🐴bar' - ``` - */ - (string: string, options?: filenamify.Options): string; - - path: typeof filenamifyPath; +export { + filenamifyPath }; - -export = filenamifyCombined; diff --git a/index.js b/index.js index 260bbd1..e32195b 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,2 @@ -'use strict'; -const filenamify = require('./filenamify'); -const filenamifyPath = require('./filenamify-path'); - -const filenamifyCombined = filenamify; -filenamifyCombined.path = filenamifyPath; - -module.exports = filenamify; +export {default} from './filenamify.js'; +export {default as filenamifyPath} from './filenamify-path.js'; diff --git a/index.test-d.ts b/index.test-d.ts index 94d71e3..e15cbe4 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,6 @@ import {expectType} from 'tsd'; -import filenamify = require('.'); +import filenamify, {filenamifyPath} from './index.js'; expectType(filenamify('')); expectType(filenamify('foo:"bar"', {replacement: '🐴'})); -expectType(filenamify.path('/some/!path')); +expectType(filenamifyPath('/some/!path')); diff --git a/package.json b/package.json index 9b22083..e70a113 100644 --- a/package.json +++ b/package.json @@ -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" @@ -24,10 +29,6 @@ "index.d.ts", "index.js" ], - "exports": { - ".": "./index.js", - "./browser": "./filenamify.js" - }, "keywords": [ "filename", "safe", @@ -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" } } diff --git a/readme.md b/readme.md index 3b0d747..577222e 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ $ npm install filenamify ## Usage ```js -const filenamify = require('filenamify'); +import filenamify from 'filenamify'; filenamify(''); //=> 'foo!bar' @@ -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. @@ -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' diff --git a/test.js b/test.js index 1dbba59..f21e6ca 100644 --- a/test.js +++ b/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'); @@ -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 => {