Bläddra i källkod

@uppy/utils: refactor to ESM (#3721)

Antoine du Hamel 2 år sedan
förälder
incheckning
b8e1f9c995
77 ändrade filer med 142 tillägg och 113 borttagningar
  1. 1 0
      .eslintrc.js
  2. 4 0
      packages/@uppy/utils/package.json
  3. 3 3
      packages/@uppy/utils/src/AbortController.js
  4. 2 1
      packages/@uppy/utils/src/AbortController.test.js
  5. 2 2
      packages/@uppy/utils/src/ErrorWithCause.js
  6. 4 3
      packages/@uppy/utils/src/ErrorWithCause.test.js
  7. 1 1
      packages/@uppy/utils/src/EventTracker.js
  8. 1 1
      packages/@uppy/utils/src/FOCUSABLE_ELEMENTS.js
  9. 1 1
      packages/@uppy/utils/src/NetworkError.js
  10. 1 1
      packages/@uppy/utils/src/ProgressTimeout.js
  11. 2 5
      packages/@uppy/utils/src/RateLimitedQueue.js
  12. 2 1
      packages/@uppy/utils/src/RateLimitedQueue.test.js
  13. 2 2
      packages/@uppy/utils/src/Translator.js
  14. 2 1
      packages/@uppy/utils/src/Translator.test.js
  15. 1 1
      packages/@uppy/utils/src/canvasToBlob.js
  16. 1 1
      packages/@uppy/utils/src/dataURItoBlob.js
  17. 3 2
      packages/@uppy/utils/src/dataURItoBlob.test.js
  18. 2 2
      packages/@uppy/utils/src/dataURItoFile.js
  19. 3 2
      packages/@uppy/utils/src/dataURItoFile.test.js
  20. 2 2
      packages/@uppy/utils/src/delay.js
  21. 3 2
      packages/@uppy/utils/src/delay.test.js
  22. 2 2
      packages/@uppy/utils/src/emitSocketProgress.js
  23. 2 2
      packages/@uppy/utils/src/fetchWithNetworkError.js
  24. 2 2
      packages/@uppy/utils/src/findAllDOMElements.js
  25. 2 2
      packages/@uppy/utils/src/findDOMElement.js
  26. 1 1
      packages/@uppy/utils/src/findIndex.js
  27. 2 1
      packages/@uppy/utils/src/findIndex.test.js
  28. 1 1
      packages/@uppy/utils/src/generateFileID.js
  29. 2 1
      packages/@uppy/utils/src/generateFileID.test.js
  30. 1 1
      packages/@uppy/utils/src/getBytesRemaining.js
  31. 2 1
      packages/@uppy/utils/src/getBytesRemaining.test.js
  32. 3 3
      packages/@uppy/utils/src/getDroppedFiles/index.js
  33. 2 2
      packages/@uppy/utils/src/getDroppedFiles/utils/fallbackApi.js
  34. 1 1
      packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getFilesAndDirectoriesFromDirectory.js
  35. 1 1
      packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getRelativePath.js
  36. 4 4
      packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/index.js
  37. 3 3
      packages/@uppy/utils/src/getETA.js
  38. 2 1
      packages/@uppy/utils/src/getETA.test.js
  39. 1 1
      packages/@uppy/utils/src/getFileNameAndExtension.js
  40. 2 1
      packages/@uppy/utils/src/getFileNameAndExtension.test.js
  41. 3 3
      packages/@uppy/utils/src/getFileType.js
  42. 2 1
      packages/@uppy/utils/src/getFileType.test.js
  43. 1 1
      packages/@uppy/utils/src/getFileTypeExtension.js
  44. 2 1
      packages/@uppy/utils/src/getFileTypeExtension.test.js
  45. 1 1
      packages/@uppy/utils/src/getSocketHost.js
  46. 2 1
      packages/@uppy/utils/src/getSocketHost.test.js
  47. 1 1
      packages/@uppy/utils/src/getSpeed.js
  48. 2 1
      packages/@uppy/utils/src/getSpeed.test.js
  49. 1 1
      packages/@uppy/utils/src/getTextDirection.js
  50. 1 1
      packages/@uppy/utils/src/getTimeStamp.js
  51. 1 1
      packages/@uppy/utils/src/hasProperty.js
  52. 1 1
      packages/@uppy/utils/src/isDOMElement.js
  53. 1 1
      packages/@uppy/utils/src/isDragDropSupported.js
  54. 1 1
      packages/@uppy/utils/src/isMobileDevice.js
  55. 3 2
      packages/@uppy/utils/src/isMobileDevice.test.js
  56. 1 1
      packages/@uppy/utils/src/isNetworkError.js
  57. 2 1
      packages/@uppy/utils/src/isNetworkError.test.js
  58. 1 1
      packages/@uppy/utils/src/isObjectURL.js
  59. 2 1
      packages/@uppy/utils/src/isObjectURL.test.js
  60. 1 1
      packages/@uppy/utils/src/isPreviewSupported.js
  61. 2 1
      packages/@uppy/utils/src/isPreviewSupported.test.js
  62. 1 1
      packages/@uppy/utils/src/isTouchDevice.js
  63. 10 9
      packages/@uppy/utils/src/isTouchDevice.test.js
  64. 1 1
      packages/@uppy/utils/src/mimeTypes.js
  65. 2 2
      packages/@uppy/utils/src/prettyETA.js
  66. 2 1
      packages/@uppy/utils/src/prettyETA.test.js
  67. 2 2
      packages/@uppy/utils/src/remoteFileObjToLocal.js
  68. 0 0
      packages/@uppy/utils/src/sampleImageDataURI.js
  69. 1 1
      packages/@uppy/utils/src/secondsToTime.js
  70. 2 1
      packages/@uppy/utils/src/secondsToTime.test.js
  71. 1 1
      packages/@uppy/utils/src/settle.js
  72. 2 1
      packages/@uppy/utils/src/settle.test.js
  73. 1 1
      packages/@uppy/utils/src/toArray.js
  74. 2 1
      packages/@uppy/utils/src/toArray.test.js
  75. 1 1
      packages/@uppy/utils/src/truncateString.js
  76. 2 1
      packages/@uppy/utils/src/truncateString.test.js
  77. 1 0
      yarn.lock

+ 1 - 0
.eslintrc.js

@@ -232,6 +232,7 @@ module.exports = {
         'packages/@uppy/unsplash/src/**/*.js',
         'packages/@uppy/transloadit/src/**/*.js',
         'packages/@uppy/url/src/**/*.js',
+        'packages/@uppy/utils/src/**/*.js',
         'packages/@uppy/vue/src/**/*.js',
         'packages/@uppy/webcam/src/**/*.js',
         'packages/@uppy/xhr-upload/src/**/*.js',

+ 4 - 0
packages/@uppy/utils/package.json

@@ -5,6 +5,7 @@
   "license": "MIT",
   "main": "lib/index.js",
   "types": "types/index.d.ts",
+  "type": "module",
   "keywords": [
     "file uploader",
     "uppy"
@@ -19,5 +20,8 @@
   },
   "dependencies": {
     "lodash.throttle": "^4.1.1"
+  },
+  "devDependencies": {
+    "@jest/globals": "^27.4.2"
   }
 }

+ 3 - 3
packages/@uppy/utils/src/AbortController.js

@@ -1,6 +1,6 @@
 /**
  * Little AbortController proxy module so we can swap out the implementation easily later.
  */
-exports.AbortController = globalThis.AbortController
-exports.AbortSignal = globalThis.AbortSignal
-exports.createAbortError = (message = 'Aborted') => new DOMException(message, 'AbortError')
+export const { AbortController } = globalThis
+export const { AbortSignal } = globalThis
+export const createAbortError = (message = 'Aborted') => new DOMException(message, 'AbortError')

+ 2 - 1
packages/@uppy/utils/src/AbortController.test.js

@@ -1,4 +1,5 @@
-const { AbortController, AbortSignal } = require('./AbortController')
+import { describe, expect, it, jest } from '@jest/globals'
+import { AbortController, AbortSignal } from './AbortController.js'
 
 function flushInstantTimeouts () {
   return new Promise(resolve => setTimeout(resolve, 0))

+ 2 - 2
packages/@uppy/utils/src/ErrorWithCause.js

@@ -1,4 +1,4 @@
-const hasProperty = require('./hasProperty')
+import hasProperty from './hasProperty.js'
 
 class ErrorWithCause extends Error {
   constructor (message, options = {}) {
@@ -10,4 +10,4 @@ class ErrorWithCause extends Error {
   }
 }
 
-module.exports = ErrorWithCause
+export default ErrorWithCause

+ 4 - 3
packages/@uppy/utils/src/ErrorWithCause.test.js

@@ -1,6 +1,7 @@
-const ErrorWithCause = require('./ErrorWithCause')
-const NetworkError = require('./NetworkError')
-const isNetworkError = require('./isNetworkError')
+import { describe, expect, it } from '@jest/globals'
+import ErrorWithCause from './ErrorWithCause.js'
+import NetworkError from './NetworkError.js'
+import isNetworkError from './isNetworkError.js'
 
 describe('ErrorWithCause', () => {
   it('should support a `{ cause }` option', () => {

+ 1 - 1
packages/@uppy/utils/src/EventTracker.js

@@ -2,7 +2,7 @@
  * Create a wrapper around an event emitter with a `remove` method to remove
  * all events that were added using the wrapped emitter.
  */
-module.exports = class EventTracker {
+export default class EventTracker {
   #emitter
 
   #events = []

+ 1 - 1
packages/@uppy/utils/src/FOCUSABLE_ELEMENTS.js

@@ -1,4 +1,4 @@
-module.exports = [
+export default [
   'a[href]:not([tabindex^="-"]):not([inert]):not([aria-hidden])',
   'area[href]:not([tabindex^="-"]):not([inert]):not([aria-hidden])',
   'input:not([disabled]):not([inert]):not([aria-hidden])',

+ 1 - 1
packages/@uppy/utils/src/NetworkError.js

@@ -8,4 +8,4 @@ class NetworkError extends Error {
   }
 }
 
-module.exports = NetworkError
+export default NetworkError

+ 1 - 1
packages/@uppy/utils/src/ProgressTimeout.js

@@ -39,4 +39,4 @@ class ProgressTimeout {
   }
 }
 
-module.exports = ProgressTimeout
+export default ProgressTimeout

+ 2 - 5
packages/@uppy/utils/src/RateLimitedQueue.js

@@ -2,7 +2,7 @@ function createCancelError () {
   return new Error('Cancelled')
 }
 
-class RateLimitedQueue {
+export class RateLimitedQueue {
   #activeRequests = 0
 
   #queuedHandlers = []
@@ -224,7 +224,4 @@ class RateLimitedQueue {
   get isPaused () { return this.#paused }
 }
 
-module.exports = {
-  RateLimitedQueue,
-  internalRateLimitedQueue: Symbol('__queue'),
-}
+export const internalRateLimitedQueue = Symbol('__queue')

+ 2 - 1
packages/@uppy/utils/src/RateLimitedQueue.test.js

@@ -1,4 +1,5 @@
-const { RateLimitedQueue } = require('./RateLimitedQueue')
+import { describe, expect, it } from '@jest/globals'
+import { RateLimitedQueue } from './RateLimitedQueue.js'
 
 const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
 

+ 2 - 2
packages/@uppy/utils/src/Translator.js

@@ -1,4 +1,4 @@
-const has = require('./hasProperty')
+import has from './hasProperty.js'
 
 function insertReplacement (source, rx, replacement) {
   const newParts = []
@@ -73,7 +73,7 @@ function interpolate (phrase, options) {
  *
  * Usage example: `translator.translate('files_chosen', {smart_count: 3})`
  */
-module.exports = class Translator {
+export default class Translator {
   /**
    * @param {object|Array<object>} locales - locale or list of locales.
    */

+ 2 - 1
packages/@uppy/utils/src/Translator.test.js

@@ -1,4 +1,5 @@
-const Translator = require('./Translator')
+import { describe, expect, it } from '@jest/globals'
+import Translator from './Translator.js'
 
 const english = {
   strings: {

+ 1 - 1
packages/@uppy/utils/src/canvasToBlob.js

@@ -4,7 +4,7 @@
  * @param {HTMLCanvasElement} canvas
  * @returns {Promise}
  */
-module.exports = function canvasToBlob (canvas, type, quality) {
+export default function canvasToBlob (canvas, type, quality) {
   return new Promise((resolve) => {
     canvas.toBlob(resolve, type, quality)
   })

+ 1 - 1
packages/@uppy/utils/src/dataURItoBlob.js

@@ -1,6 +1,6 @@
 const DATA_URL_PATTERN = /^data:([^/]+\/[^,;]+(?:[^,]*?))(;base64)?,([\s\S]*)$/
 
-module.exports = function dataURItoBlob (dataURI, opts, toFile) {
+export default function dataURItoBlob (dataURI, opts, toFile) {
   // get the base64 data
   const dataURIData = DATA_URL_PATTERN.exec(dataURI)
 

+ 3 - 2
packages/@uppy/utils/src/dataURItoBlob.test.js

@@ -1,5 +1,6 @@
-const dataURItoBlob = require('./dataURItoBlob')
-const sampleImageDataURI = require('./sampleImageDataURI')
+import { describe, expect, it } from '@jest/globals'
+import dataURItoBlob from './dataURItoBlob.js'
+import sampleImageDataURI from './sampleImageDataURI.js'
 
 describe('dataURItoBlob', () => {
   it('should convert a data uri to a blob', () => {

+ 2 - 2
packages/@uppy/utils/src/dataURItoFile.js

@@ -1,5 +1,5 @@
-const dataURItoBlob = require('./dataURItoBlob')
+import dataURItoBlob from './dataURItoBlob.js'
 
-module.exports = function dataURItoFile (dataURI, opts) {
+export default function dataURItoFile (dataURI, opts) {
   return dataURItoBlob(dataURI, opts, true)
 }

+ 3 - 2
packages/@uppy/utils/src/dataURItoFile.test.js

@@ -1,5 +1,6 @@
-const dataURItoFile = require('./dataURItoFile')
-const sampleImageDataURI = require('./sampleImageDataURI')
+import { describe, expect, it } from '@jest/globals'
+import dataURItoFile from './dataURItoFile.js'
+import sampleImageDataURI from './sampleImageDataURI.js'
 
 describe('dataURItoFile', () => {
   it('should convert a data uri to a file', () => {

+ 2 - 2
packages/@uppy/utils/src/delay.js

@@ -1,4 +1,4 @@
-const { createAbortError } = require('./AbortController')
+import { createAbortError } from './AbortController.js'
 
 /**
  * Return a Promise that resolves after `ms` milliseconds.
@@ -7,7 +7,7 @@ const { createAbortError } = require('./AbortController')
  * @param {{ signal?: AbortSignal }} [opts] - An abort signal that can be used to cancel the delay early.
  * @returns {Promise<void>} A Promise that resolves after the given amount of `ms`.
  */
-module.exports = function delay (ms, opts) {
+export default function delay (ms, opts) {
   return new Promise((resolve, reject) => {
     if (opts?.signal?.aborted) {
       return reject(createAbortError())

+ 3 - 2
packages/@uppy/utils/src/delay.test.js

@@ -1,5 +1,6 @@
-const delay = require('./delay')
-const { AbortController } = require('./AbortController')
+import { describe, expect, it } from '@jest/globals'
+import { AbortController } from './AbortController.js'
+import delay from './delay.js'
 
 describe('delay', () => {
   it('should wait for the specified time', async () => {

+ 2 - 2
packages/@uppy/utils/src/emitSocketProgress.js

@@ -1,4 +1,4 @@
-const throttle = require('lodash.throttle')
+import throttle from 'lodash.throttle'
 
 function emitSocketProgress (uploader, progressData, file) {
   const { progress, bytesUploaded, bytesTotal } = progressData
@@ -12,7 +12,7 @@ function emitSocketProgress (uploader, progressData, file) {
   }
 }
 
-module.exports = throttle(emitSocketProgress, 300, {
+export default throttle(emitSocketProgress, 300, {
   leading: true,
   trailing: true,
 })

+ 2 - 2
packages/@uppy/utils/src/fetchWithNetworkError.js

@@ -1,9 +1,9 @@
-const NetworkError = require('./NetworkError')
+import NetworkError from './NetworkError.js'
 
 /**
  * Wrapper around window.fetch that throws a NetworkError when appropriate
  */
-module.exports = function fetchWithNetworkError (...options) {
+export default function fetchWithNetworkError (...options) {
   return fetch(...options)
     .catch((err) => {
       if (err.name === 'AbortError') {

+ 2 - 2
packages/@uppy/utils/src/findAllDOMElements.js

@@ -1,4 +1,4 @@
-const isDOMElement = require('./isDOMElement')
+import isDOMElement from './isDOMElement.js'
 
 /**
  * Find one or more DOM elements.
@@ -6,7 +6,7 @@ const isDOMElement = require('./isDOMElement')
  * @param {string|Node} element
  * @returns {Node[]|null}
  */
-module.exports = function findAllDOMElements (element) {
+export default function findAllDOMElements (element) {
   if (typeof element === 'string') {
     const elements = document.querySelectorAll(element)
     return elements.length === 0 ? null : Array.from(elements)

+ 2 - 2
packages/@uppy/utils/src/findDOMElement.js

@@ -1,4 +1,4 @@
-const isDOMElement = require('./isDOMElement')
+import isDOMElement from './isDOMElement.js'
 
 /**
  * Find a DOM element.
@@ -6,7 +6,7 @@ const isDOMElement = require('./isDOMElement')
  * @param {Node|string} element
  * @returns {Node|null}
  */
-module.exports = function findDOMElement (element, context = document) {
+export default function findDOMElement (element, context = document) {
   if (typeof element === 'string') {
     return context.querySelector(element)
   }

+ 1 - 1
packages/@uppy/utils/src/findIndex.js

@@ -3,4 +3,4 @@
  * @param {Function} predicate
  * @returns {number}
  */
-module.exports = Function.prototype.call.bind(Array.prototype.findIndex)
+export default Function.prototype.call.bind(Array.prototype.findIndex)

+ 2 - 1
packages/@uppy/utils/src/findIndex.test.js

@@ -1,4 +1,5 @@
-const findIndex = require('./findIndex')
+import { describe, expect, it } from '@jest/globals'
+import findIndex from './findIndex.js'
 
 describe('findIndex', () => {
   it('should return index of an object in an array, that matches a predicate', () => {

+ 1 - 1
packages/@uppy/utils/src/generateFileID.js

@@ -17,7 +17,7 @@ function encodeFilename (name) {
  * @param {object} file
  * @returns {string} the fileID
  */
-module.exports = function generateFileID (file) {
+export default function generateFileID (file) {
   // It's tempting to do `[items].filter(Boolean).join('-')` here, but that
   // is slower! simple string concatenation is fast
 

+ 2 - 1
packages/@uppy/utils/src/generateFileID.test.js

@@ -1,4 +1,5 @@
-const generateFileID = require('./generateFileID')
+import { describe, expect, it } from '@jest/globals'
+import generateFileID from './generateFileID.js'
 
 describe('generateFileID', () => {
   it('should take the filename object and produce a lowercase file id made up of uppy- prefix, file name (cleaned up to be lowercase, letters and numbers only), type, relative path (folder) from file.meta.relativePath, size and lastModified date', () => {

+ 1 - 1
packages/@uppy/utils/src/getBytesRemaining.js

@@ -1,3 +1,3 @@
-module.exports = function getBytesRemaining (fileProgress) {
+export default function getBytesRemaining (fileProgress) {
   return fileProgress.bytesTotal - fileProgress.bytesUploaded
 }

+ 2 - 1
packages/@uppy/utils/src/getBytesRemaining.test.js

@@ -1,4 +1,5 @@
-const getBytesRemaining = require('./getBytesRemaining')
+import { describe, expect, it } from '@jest/globals'
+import getBytesRemaining from './getBytesRemaining.js'
 
 describe('getBytesRemaining', () => {
   it('should calculate the bytes remaining given a fileProgress object', () => {

+ 3 - 3
packages/@uppy/utils/src/getDroppedFiles/index.js

@@ -1,5 +1,5 @@
-const webkitGetAsEntryApi = require('./utils/webkitGetAsEntryApi/index')
-const fallbackApi = require('./utils/fallbackApi')
+import webkitGetAsEntryApi from './utils/webkitGetAsEntryApi/index.js'
+import fallbackApi from './utils/fallbackApi.js'
 
 /**
  * Returns a promise that resolves to the array of dropped files (if a folder is
@@ -15,7 +15,7 @@ const fallbackApi = require('./utils/fallbackApi')
  *
  * @returns {Promise} - Array<File>
  */
-module.exports = function getDroppedFiles (dataTransfer, { logDropError = () => {} } = {}) {
+export default function getDroppedFiles (dataTransfer, { logDropError = () => {} } = {}) {
   // Get all files from all subdirs. Works (at least) in Chrome, Mozilla, and Safari
   if (dataTransfer.items?.[0] && 'webkitGetAsEntry' in dataTransfer.items[0]) {
     return webkitGetAsEntryApi(dataTransfer, logDropError)

+ 2 - 2
packages/@uppy/utils/src/getDroppedFiles/utils/fallbackApi.js

@@ -1,7 +1,7 @@
-const toArray = require('../../toArray')
+import toArray from '../../toArray.js'
 
 // .files fallback, should be implemented in any browser
-module.exports = function fallbackApi (dataTransfer) {
+export default function fallbackApi (dataTransfer) {
   const files = toArray(dataTransfer.files)
   return Promise.resolve(files)
 }

+ 1 - 1
packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getFilesAndDirectoriesFromDirectory.js

@@ -6,7 +6,7 @@
  * @param {Function} logDropError
  * @param {Function} callback - called with ([ all files and directories in that directoryReader ])
  */
-module.exports = function getFilesAndDirectoriesFromDirectory (directoryReader, oldEntries, logDropError, { onSuccess }) {
+export default function getFilesAndDirectoriesFromDirectory (directoryReader, oldEntries, logDropError, { onSuccess }) {
   directoryReader.readEntries(
     (entries) => {
       const newEntries = [...oldEntries, ...entries]

+ 1 - 1
packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getRelativePath.js

@@ -7,7 +7,7 @@
  * be consistent with .relativePath-s of files selected from My Device). If file
  * is in a folder - return its fullPath, e.g. '/simpsons/hi.jpeg'.
  */
-module.exports = function getRelativePath (fileEntry) {
+export default function getRelativePath (fileEntry) {
   // fileEntry.fullPath - "/simpsons/hi.jpeg" or undefined (for browsers that don't support it)
   // fileEntry.name - "hi.jpeg"
   if (!fileEntry.fullPath || fileEntry.fullPath === `/${fileEntry.name}`) {

+ 4 - 4
packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/index.js

@@ -1,8 +1,8 @@
-const toArray = require('../../../toArray')
-const getRelativePath = require('./getRelativePath')
-const getFilesAndDirectoriesFromDirectory = require('./getFilesAndDirectoriesFromDirectory')
+import getRelativePath from './getRelativePath.js'
+import getFilesAndDirectoriesFromDirectory from './getFilesAndDirectoriesFromDirectory.js'
+import toArray from '../../../toArray.js'
 
-module.exports = function webkitGetAsEntryApi (dataTransfer, logDropError) {
+export default function webkitGetAsEntryApi (dataTransfer, logDropError) {
   const files = []
 
   const rootPromises = []

+ 3 - 3
packages/@uppy/utils/src/getETA.js

@@ -1,7 +1,7 @@
-const getSpeed = require('./getSpeed')
-const getBytesRemaining = require('./getBytesRemaining')
+import getSpeed from './getSpeed.js'
+import getBytesRemaining from './getBytesRemaining.js'
 
-module.exports = function getETA (fileProgress) {
+export default function getETA (fileProgress) {
   if (!fileProgress.bytesUploaded) return 0
 
   const uploadSpeed = getSpeed(fileProgress)

+ 2 - 1
packages/@uppy/utils/src/getETA.test.js

@@ -1,4 +1,5 @@
-const getETA = require('./getETA')
+import { describe, expect, it } from '@jest/globals'
+import getETA from './getETA.js'
 
 describe('getETA', () => {
   it('should get the ETA remaining based on a fileProgress object', () => {

+ 1 - 1
packages/@uppy/utils/src/getFileNameAndExtension.js

@@ -4,7 +4,7 @@
  * @param {string} fullFileName
  * @returns {object} {name, extension}
  */
-module.exports = function getFileNameAndExtension (fullFileName) {
+export default function getFileNameAndExtension (fullFileName) {
   const lastDot = fullFileName.lastIndexOf('.')
   // these count as no extension: "no-dot", "trailing-dot."
   if (lastDot === -1 || lastDot === fullFileName.length - 1) {

+ 2 - 1
packages/@uppy/utils/src/getFileNameAndExtension.test.js

@@ -1,4 +1,5 @@
-const getFileNameAndExtension = require('./getFileNameAndExtension')
+import { describe, expect, it } from '@jest/globals'
+import getFileNameAndExtension from './getFileNameAndExtension.js'
 
 describe('getFileNameAndExtension', () => {
   it('should return the filename and extension as an array', () => {

+ 3 - 3
packages/@uppy/utils/src/getFileType.js

@@ -1,7 +1,7 @@
-const getFileNameAndExtension = require('./getFileNameAndExtension')
-const mimeTypes = require('./mimeTypes')
+import getFileNameAndExtension from './getFileNameAndExtension.js'
+import mimeTypes from './mimeTypes.js'
 
-module.exports = function getFileType (file) {
+export default function getFileType (file) {
   if (file.type) return file.type
 
   const fileExtension = file.name ? getFileNameAndExtension(file.name).extension?.toLowerCase() : null

+ 2 - 1
packages/@uppy/utils/src/getFileType.test.js

@@ -1,4 +1,5 @@
-const getFileType = require('./getFileType')
+import { describe, expect, it } from '@jest/globals'
+import getFileType from './getFileType.js'
 
 describe('getFileType', () => {
   it('should trust the filetype if the file comes from a remote source', () => {

+ 1 - 1
packages/@uppy/utils/src/getFileTypeExtension.js

@@ -17,7 +17,7 @@ const mimeToExtensions = {
   'video/x-msvideo': 'avi',
 }
 
-module.exports = function getFileTypeExtension (mimeType) {
+export default function getFileTypeExtension (mimeType) {
   // Remove the ; bit in 'video/x-matroska;codecs=avc1'
   // eslint-disable-next-line no-param-reassign
   [mimeType] = mimeType.split(';', 1)

+ 2 - 1
packages/@uppy/utils/src/getFileTypeExtension.test.js

@@ -1,4 +1,5 @@
-const getFileTypeExtension = require('./getFileTypeExtension')
+import { describe, expect, it } from '@jest/globals'
+import getFileTypeExtension from './getFileTypeExtension.js'
 
 describe('getFileTypeExtension', () => {
   it('should return the filetype based on the specified mime type', () => {

+ 1 - 1
packages/@uppy/utils/src/getSocketHost.js

@@ -1,4 +1,4 @@
-module.exports = function getSocketHost (url) {
+export default function getSocketHost (url) {
   // get the host domain
   const regex = /^(?:https?:\/\/|\/\/)?(?:[^@\n]+@)?(?:www\.)?([^\n]+)/i
   const host = regex.exec(url)[1]

+ 2 - 1
packages/@uppy/utils/src/getSocketHost.test.js

@@ -1,4 +1,5 @@
-const getSocketHost = require('./getSocketHost')
+import { describe, expect, it } from '@jest/globals'
+import getSocketHost from './getSocketHost.js'
 
 describe('getSocketHost', () => {
   it('should get the host from the specified url', () => {

+ 1 - 1
packages/@uppy/utils/src/getSpeed.js

@@ -1,4 +1,4 @@
-module.exports = function getSpeed (fileProgress) {
+export default function getSpeed (fileProgress) {
   if (!fileProgress.bytesUploaded) return 0
 
   const timeElapsed = Date.now() - fileProgress.uploadStarted

+ 2 - 1
packages/@uppy/utils/src/getSpeed.test.js

@@ -1,4 +1,5 @@
-const getSpeed = require('./getSpeed')
+import { describe, expect, it } from '@jest/globals'
+import getSpeed from './getSpeed.js'
 
 describe('getSpeed', () => {
   it('should calculate the speed given a fileProgress object', () => {

+ 1 - 1
packages/@uppy/utils/src/getTextDirection.js

@@ -19,4 +19,4 @@ function getTextDirection (element) {
   return element?.dir
 }
 
-module.exports = getTextDirection
+export default getTextDirection

+ 1 - 1
packages/@uppy/utils/src/getTimeStamp.js

@@ -11,7 +11,7 @@ function pad (number) {
 /**
  * Returns a timestamp in the format of `hours:minutes:seconds`
  */
-module.exports = function getTimeStamp () {
+export default function getTimeStamp () {
   const date = new Date()
   const hours = pad(date.getHours())
   const minutes = pad(date.getMinutes())

+ 1 - 1
packages/@uppy/utils/src/hasProperty.js

@@ -1,3 +1,3 @@
-module.exports = function has (object, key) {
+export default function has (object, key) {
   return Object.prototype.hasOwnProperty.call(object, key)
 }

+ 1 - 1
packages/@uppy/utils/src/isDOMElement.js

@@ -3,6 +3,6 @@
  *
  * @param {*} obj
  */
-module.exports = function isDOMElement (obj) {
+export default function isDOMElement (obj) {
   return obj?.nodeType === Node.ELEMENT_NODE
 }

+ 1 - 1
packages/@uppy/utils/src/isDragDropSupported.js

@@ -3,7 +3,7 @@
  *
  * @returns {boolean}
  */
-module.exports = function isDragDropSupported () {
+export default function isDragDropSupported () {
   const div = document.body
 
   if (!('draggable' in div) || !('ondragstart' in div && 'ondrop' in div)) {

+ 1 - 1
packages/@uppy/utils/src/isMobileDevice.js

@@ -4,7 +4,7 @@
  *
  * @returns {boolean}
  */
-module.exports = function isMobileDevice () {
+export default function isMobileDevice () {
   if (typeof window !== 'undefined'
       && window.navigator
       && window.navigator.userAgent

+ 3 - 2
packages/@uppy/utils/src/isMobileDevice.test.js

@@ -1,8 +1,9 @@
-const isMobileDevice = require('./isMobileDevice')
+import { describe, expect, it } from '@jest/globals'
+import isMobileDevice from './isMobileDevice.js'
 
 let fakeUserAgent = null
 
-Object.defineProperty(global.navigator, 'userAgent', {
+Object.defineProperty(globalThis.navigator, 'userAgent', {
   get () {
     return fakeUserAgent
   },

+ 1 - 1
packages/@uppy/utils/src/isNetworkError.js

@@ -5,4 +5,4 @@ function isNetworkError (xhr) {
   return (xhr.readyState !== 0 && xhr.readyState !== 4) || xhr.status === 0
 }
 
-module.exports = isNetworkError
+export default isNetworkError

+ 2 - 1
packages/@uppy/utils/src/isNetworkError.test.js

@@ -1,4 +1,5 @@
-const isNetworkError = require('./isNetworkError')
+import { describe, expect, it } from '@jest/globals'
+import isNetworkError from './isNetworkError.js'
 
 describe('isNetworkError', () => {
   it('should return true if the specified xhr object contains a network error', () => {

+ 1 - 1
packages/@uppy/utils/src/isObjectURL.js

@@ -4,6 +4,6 @@
  * @param {string} url
  * @returns {boolean}
  */
-module.exports = function isObjectURL (url) {
+export default function isObjectURL (url) {
   return url.startsWith('blob:')
 }

+ 2 - 1
packages/@uppy/utils/src/isObjectURL.test.js

@@ -1,4 +1,5 @@
-const isObjectURL = require('./isObjectURL')
+import { describe, expect, it } from '@jest/globals'
+import isObjectURL from './isObjectURL.js'
 
 describe('isObjectURL', () => {
   it('should return true if the specified url is an object url', () => {

+ 1 - 1
packages/@uppy/utils/src/isPreviewSupported.js

@@ -1,4 +1,4 @@
-module.exports = function isPreviewSupported (fileType) {
+export default function isPreviewSupported (fileType) {
   if (!fileType) return false
   // list of images that browsers can preview
   return /^[^/]+\/(jpe?g|gif|png|svg|svg\+xml|bmp|webp|avif)$/.test(fileType)

+ 2 - 1
packages/@uppy/utils/src/isPreviewSupported.test.js

@@ -1,4 +1,5 @@
-const isPreviewSupported = require('./isPreviewSupported')
+import { describe, expect, it } from '@jest/globals'
+import isPreviewSupported from './isPreviewSupported.js'
 
 describe('isPreviewSupported', () => {
   it('should return true for any filetypes that browsers can preview', () => {

+ 1 - 1
packages/@uppy/utils/src/isTouchDevice.js

@@ -1,3 +1,3 @@
-module.exports = function isTouchDevice () {
+export default function isTouchDevice () {
   return 'ontouchstart' in window || 'maxTouchPoints' in navigator
 }

+ 10 - 9
packages/@uppy/utils/src/isTouchDevice.test.js

@@ -1,23 +1,24 @@
-const isTouchDevice = require('./isTouchDevice')
+import { afterEach, beforeEach, describe, expect, xit } from '@jest/globals'
+import isTouchDevice from './isTouchDevice.js'
 
 describe('isTouchDevice', () => {
-  const RealTouchStart = global.window.ontouchstart
-  const RealMaxTouchPoints = global.navigator.maxTouchPoints
+  const RealTouchStart = globalThis.window.ontouchstart
+  const RealMaxTouchPoints = globalThis.navigator.maxTouchPoints
 
   beforeEach(() => {
-    global.window.ontouchstart = true
-    global.navigator.maxTouchPoints = 1
+    globalThis.window.ontouchstart = true
+    globalThis.navigator.maxTouchPoints = 1
   })
 
   afterEach(() => {
-    global.navigator.maxTouchPoints = RealMaxTouchPoints
-    global.window.ontouchstart = RealTouchStart
+    globalThis.navigator.maxTouchPoints = RealMaxTouchPoints
+    globalThis.window.ontouchstart = RealTouchStart
   })
 
   xit("should return true if it's a touch device", () => {
     expect(isTouchDevice()).toEqual(true)
-    delete global.window.ontouchstart
-    global.navigator.maxTouchPoints = false
+    delete globalThis.window.ontouchstart
+    globalThis.navigator.maxTouchPoints = false
     expect(isTouchDevice()).toEqual(false)
   })
 })

+ 1 - 1
packages/@uppy/utils/src/mimeTypes.js

@@ -3,7 +3,7 @@
 // ___Where to take new extensions from?
 //    https://github.com/jshttp/mime-db/blob/master/db.json
 
-module.exports = {
+export default {
   md: 'text/markdown',
   markdown: 'text/markdown',
   mp4: 'video/mp4',

+ 2 - 2
packages/@uppy/utils/src/prettyETA.js

@@ -1,6 +1,6 @@
-const secondsToTime = require('./secondsToTime')
+import secondsToTime from './secondsToTime.js'
 
-module.exports = function prettyETA (seconds) {
+export default function prettyETA (seconds) {
   const time = secondsToTime(seconds)
 
   // Only display hours and minutes if they are greater than 0 but always

+ 2 - 1
packages/@uppy/utils/src/prettyETA.test.js

@@ -1,4 +1,5 @@
-const prettyETA = require('./prettyETA')
+import { describe, expect, it } from '@jest/globals'
+import prettyETA from './prettyETA.js'
 
 describe('prettyETA', () => {
   it('should convert the specified number of seconds to a pretty ETA', () => {

+ 2 - 2
packages/@uppy/utils/src/remoteFileObjToLocal.js

@@ -1,6 +1,6 @@
-const getFileNameAndExtension = require('./getFileNameAndExtension')
+import getFileNameAndExtension from './getFileNameAndExtension.js'
 
-module.exports = function remoteFileObjToLocal (file) {
+export default function remoteFileObjToLocal (file) {
   return {
     ...file,
     type: file.mimeType,

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
packages/@uppy/utils/src/sampleImageDataURI.js


+ 1 - 1
packages/@uppy/utils/src/secondsToTime.js

@@ -1,4 +1,4 @@
-module.exports = function secondsToTime (rawSeconds) {
+export default function secondsToTime (rawSeconds) {
   const hours = Math.floor(rawSeconds / 3600) % 24
   const minutes = Math.floor(rawSeconds / 60) % 60
   const seconds = Math.floor(rawSeconds % 60)

+ 2 - 1
packages/@uppy/utils/src/secondsToTime.test.js

@@ -1,4 +1,5 @@
-const secondsToTime = require('./secondsToTime')
+import { describe, expect, it } from '@jest/globals'
+import secondsToTime from './secondsToTime.js'
 
 describe('secondsToTime', () => {
   it('converts seconds to an { hours, minutes, seconds } object', () => {

+ 1 - 1
packages/@uppy/utils/src/settle.js

@@ -1,4 +1,4 @@
-module.exports = function settle (promises) {
+export default function settle (promises) {
   const resolutions = []
   const rejections = []
   function resolved (value) {

+ 2 - 1
packages/@uppy/utils/src/settle.test.js

@@ -1,4 +1,5 @@
-const settle = require('./settle')
+import { describe, expect, it } from '@jest/globals'
+import settle from './settle.js'
 
 describe('settle', () => {
   it('should resolve even if all input promises reject', async () => {

+ 1 - 1
packages/@uppy/utils/src/toArray.js

@@ -1,4 +1,4 @@
 /**
  * Converts list into array
  */
-module.exports = Array.from
+export default Array.from

+ 2 - 1
packages/@uppy/utils/src/toArray.test.js

@@ -1,4 +1,5 @@
-const toArray = require('./toArray')
+import { describe, expect, it } from '@jest/globals'
+import toArray from './toArray.js'
 
 describe('toArray', () => {
   it('should convert a array-like object into an array', () => {

+ 1 - 1
packages/@uppy/utils/src/truncateString.js

@@ -7,7 +7,7 @@
  * @returns {string}
  */
 const separator = '...'
-module.exports = function truncateString (string, maxLength) {
+export default function truncateString (string, maxLength) {
   // Return the empty string if maxLength is zero
   if (maxLength === 0) return ''
   // Return original string if it's already shorter than maxLength

+ 2 - 1
packages/@uppy/utils/src/truncateString.test.js

@@ -1,4 +1,5 @@
-const truncateString = require('./truncateString')
+import { describe, expect, it } from '@jest/globals'
+import truncateString from './truncateString.js'
 
 describe('truncateString', () => {
   it('should truncate the string to the length', () => {

+ 1 - 0
yarn.lock

@@ -10223,6 +10223,7 @@ __metadata:
   version: 0.0.0-use.local
   resolution: "@uppy/utils@workspace:packages/@uppy/utils"
   dependencies:
+    "@jest/globals": ^27.4.2
     lodash.throttle: ^4.1.1
   languageName: unknown
   linkType: soft

Vissa filer visades inte eftersom för många filer har ändrats