diff --git a/lib/gui/app/models/store.js b/lib/gui/app/models/store.js index 3c6c559e..cf53fc72 100644 --- a/lib/gui/app/models/store.js +++ b/lib/gui/app/models/store.js @@ -23,6 +23,7 @@ const uuidV4 = require('uuid/v4') const constraints = require('../../../shared/drive-constraints') const supportedFormats = require('../../../shared/supported-formats') const errors = require('../../../shared/errors') +// eslint-disable-next-line node/no-missing-require const fileExtensions = require('../../../shared/file-extensions') const utils = require('../../../shared/utils') const settings = require('./settings') diff --git a/lib/shared/file-extensions.js b/lib/shared/file-extensions.ts similarity index 58% rename from lib/shared/file-extensions.js rename to lib/shared/file-extensions.ts index b56f748e..af0ed8d9 100644 --- a/lib/shared/file-extensions.js +++ b/lib/shared/file-extensions.ts @@ -14,63 +14,50 @@ * limitations under the License. */ -'use strict' - -const mime = require('mime-types') -const _ = require('lodash') +import * as _ from 'lodash'; +import { lookup } from 'mime-types'; /** * @summary Get the extensions of a file - * @function - * @public - * - * @param {String} filePath - file path - * @returns {String[]} extensions * * @example * const extensions = fileExtensions.getFileExtensions('path/to/foo.img.gz'); * console.log(extensions); * > [ 'img', 'gz' ] */ -exports.getFileExtensions = _.memoize((filePath) => { - return _.chain(filePath) - .split('.') - .tail() - .map(_.toLower) - .value() -}) +export function getFileExtensions(filePath: string): string[] { + return _.chain(filePath) + .split('.') + .tail() + .map(_.toLower) + .value(); +} /** * @summary Get the last file extension - * @function - * @public - * - * @param {String} filePath - file path - * @returns {(String|Null)} last extension * * @example * const extension = fileExtensions.getLastFileExtension('path/to/foo.img.gz'); * console.log(extension); * > 'gz' */ -exports.getLastFileExtension = (filePath) => { - return _.last(exports.getFileExtensions(filePath)) || null +export function getLastFileExtension(filePath: string): string | null { + return _.last(getFileExtensions(filePath)) || null; } /** * @summary Get the penultimate file extension - * @function - * @public - * - * @param {String} filePath - file path - * @returns {(String|Null)} penultimate extension * * @example * const extension = fileExtensions.getPenultimateFileExtension('path/to/foo.img.gz'); * console.log(extension); * > 'img' */ -exports.getPenultimateFileExtension = (filePath) => { - const ext = _.last(_.initial(exports.getFileExtensions(filePath))) - return !_.isNil(ext) && mime.lookup(ext) ? ext : null +export function getPenultimateFileExtension(filePath: string): string | null { + const extensions = getFileExtensions(filePath); + if (extensions.length >= 2) { + const ext = extensions[extensions.length - 2]; + return lookup(ext) ? ext : null; + } + return null; } diff --git a/lib/shared/supported-formats.js b/lib/shared/supported-formats.js index 09592b51..e80747da 100644 --- a/lib/shared/supported-formats.js +++ b/lib/shared/supported-formats.js @@ -21,6 +21,7 @@ const _ = require('lodash') const mime = require('mime-types') const path = require('path') +// eslint-disable-next-line node/no-missing-require const fileExtensions = require('./file-extensions') /** diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 0c756ab7..7f4f4095 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1186,6 +1186,12 @@ "resolved": "https://registry.npmjs.org/@types/marked/-/marked-0.3.0.tgz", "integrity": "sha512-CSf9YWJdX1DkTNu9zcNtdCcn6hkRtB5ILjbhRId4ZOQqx30fXmdecuaXhugQL6eyrhuXtaHJ7PHI+Vm7k9ZJjg==" }, + "@types/mime-types": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz", + "integrity": "sha1-nKUs2jY/aZxpRmwqbM2q2RPqenM=", + "dev": true + }, "@types/minimatch": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz", diff --git a/package.json b/package.json index e0915bca..9692ae64 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,7 @@ "@babel/plugin-proposal-function-bind": "^7.2.0", "@babel/preset-env": "^7.6.0", "@babel/preset-react": "^7.0.0", + "@types/mime-types": "^2.1.0", "@types/node": "^12.12.24", "@types/react-dom": "^16.8.4", "babel-loader": "^8.0.4", diff --git a/tests/shared/file-extensions.spec.js b/tests/shared/file-extensions.spec.js index e5f241b6..3d43aca8 100644 --- a/tests/shared/file-extensions.spec.js +++ b/tests/shared/file-extensions.spec.js @@ -18,6 +18,7 @@ const m = require('mochainon') const _ = require('lodash') +// eslint-disable-next-line node/no-missing-require const fileExtensions = require('../../lib/shared/file-extensions') describe('Shared: fileExtensions', function () {