mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-23 19:26:33 +00:00
Convert file-extensions.js to typescript
Change-type: patch
This commit is contained in:
parent
db24ee4d37
commit
23b295c7c1
@ -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')
|
||||
|
@ -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;
|
||||
}
|
@ -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')
|
||||
|
||||
/**
|
||||
|
6
npm-shrinkwrap.json
generated
6
npm-shrinkwrap.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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 () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user