enable testing mjs files

This commit is contained in:
Tykayn 2023-07-05 22:34:32 +02:00 committed by tykayn
parent 70b86ff4bd
commit ed23623be0
5 changed files with 53 additions and 38 deletions

7
babel.config.js Normal file
View File

@ -0,0 +1,7 @@
const presets = [
[
"@babel/preset-env",
]
];
module.exports = { presets };

View File

@ -4,7 +4,8 @@
import { tagSectionSeparator, tagSeparator } from './configs.mjs' import { tagSectionSeparator, tagSeparator } from './configs.mjs'
import exifr from 'exifr' import exifr from 'exifr'
import moment from 'moment' import moment from 'moment'
import path from 'path' import log from 'log_level'
/** /**
* finds patterns for file name * finds patterns for file name
@ -25,8 +26,8 @@ export default class finder {
} }
static findFormattedDate (filepath) { static findFormattedDate (filepath) {
let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ig) let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}/ig)
// console.log('match findFormattedDate', match) log.debug('match findFormattedDate', match)
let result = '' let result = ''
if (match && match[0]) { if (match && match[0]) {
result = match[0] result = match[0]
@ -35,8 +36,11 @@ export default class finder {
} }
static findFileExtension (inputString) { static findFileExtension (inputString) {
if(!inputString){
return '';
}
let result = inputString.match(/\.\w{3,4}$/i) let result = inputString.match(/\.\w{3,4}$/i)
return result return result.shift()
} }
/** /**
@ -73,17 +77,17 @@ export default class finder {
} }
inputString = inputString.replace(extensionFile, '') inputString = inputString.replace(extensionFile, '')
// console.log('extensionFile', extensionFile) log.debug('extensionFile', extensionFile)
if (inputString.includes(tagSectionSeparator)) { if (inputString.includes(tagSectionSeparator)) {
// console.log('inputString', inputString) log.debug('inputString', inputString)
if (inputString.length) { if (inputString.length) {
let boom = inputString.split(tagSectionSeparator) let boom = inputString.split(tagSectionSeparator)
// console.log('boom', boom) log.debug('boom', boom)
if (boom.length) { if (boom.length) {
let fileSectionsName = boom.splice(tagSeparator) let fileSectionsName = boom.splice(tagSeparator)
listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)] listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)]
// console.log('listOfTags', listOfTags) log.debug('listOfTags', listOfTags)
} else { } else {
console.log('no boom', boom) console.log('no boom', boom)
} }
@ -164,17 +168,17 @@ export default class finder {
let moments = [] let moments = []
// console.log('exif data : ', exifData) // Do something with your data! log.debug('exif data : ', exifData) // Do something with your data!
if (exifData.DateTimeOriginal) { if (exifData.DateTimeOriginal) {
// console.log('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data! log.debug('image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal) // Do something with your data!
moments.push(exifData.DateTimeOriginal) moments.push(exifData.DateTimeOriginal)
} }
if (exifData.ModificationDateTime) { if (exifData.ModificationDateTime) {
// console.log('image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data! log.debug('image créée le : ModificationDateTime : ', exifData.ModificationDateTime) // Do something with your data!
moments.push(exifData.ModificationDateTime) moments.push(exifData.ModificationDateTime)
} }
if (exifData.ModifyDate) { if (exifData.ModifyDate) {
// console.log('image créée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data! log.debug('image créée le : ModifyDate : ', exifData.ModifyDate) // Do something with your data!
moments.push(exifData.ModifyDate) moments.push(exifData.ModifyDate)
} }
if (exifData.FileAccessDateTime) { if (exifData.FileAccessDateTime) {
@ -184,11 +188,11 @@ export default class finder {
moments.push(exifData.FileInodeChangeDateTime) moments.push(exifData.FileInodeChangeDateTime)
} }
if (exifData.FileModificationDateTime) { if (exifData.FileModificationDateTime) {
// console.log('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data! log.debug('image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime) // Do something with your data!
moments.push(exifData.FileModificationDateTime) moments.push(exifData.FileModificationDateTime)
} }
if (exifData.CreateDate) { if (exifData.CreateDate) {
// console.log('image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data! log.debug('image créée le : CreateDate : ', exifData.CreateDate) // Do something with your data!
moments.push(exifData.CreateDate) moments.push(exifData.CreateDate)
} }
@ -198,7 +202,7 @@ export default class finder {
}) })
let minDate = moment.min(moments) let minDate = moment.min(moments)
// console.log('minDate :::::::::', minDate) log.debug('minDate :::::::::', minDate)
console.log('minDate :::::::::', minDate.format('yyyy-MM-DDTHH:mm:ss')) console.log('minDate :::::::::', minDate.format('yyyy-MM-DDTHH:mm:ss'))
return minDate.format('yyyy-MM-DDTHH:mm:ss') return minDate.format('yyyy-MM-DDTHH:mm:ss')

17
finder.test.js Normal file
View File

@ -0,0 +1,17 @@
import finder from './finder.mjs'
describe('rangement file name', () => {
test('detects date in file name', () => {
expect(finder
.findFormattedDate('2023-06-23T18.36.47 -- machin bidule.jpg')
)
.toBe('2023-06-23T18.36.47')
})
test('detects file extension in file name', () => {
expect(finder
.findFileExtension('2023-06-23T18.36.47 -- machin bidule.jpg')
)
.toBe('.jpg')
})
})

View File

@ -67,8 +67,7 @@ function shouldWeChangeName (structureForFile) {
console.log(' nouveau nom:', newName) console.log(' nouveau nom:', newName)
if (!mini_arguments.dryRun) { if (!mini_arguments.dryRun) {
renameFile(structureForFile.fullPath, structureForFile.folderPath + newName) renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
} } else {
else{
console.log('no renaming for real, this is a dry run') console.log('no renaming for real, this is a dry run')
} }
} else { } else {
@ -101,7 +100,6 @@ async function guessFileNameOnAllFilesFromArguments () {
console.log(' =>>>>>>> foundDate : ', foundDate) console.log(' =>>>>>>> foundDate : ', foundDate)
if (foundDate) { if (foundDate) {
// finder.findEarliestDateInExifData(fullPath).then(response => { // finder.findEarliestDateInExifData(fullPath).then(response => {
// console.log(' ... trouvée') // console.log(' ... trouvée')
// if (response) { // if (response) {

View File

@ -1,5 +1,4 @@
import finder from "./finders.mjs"; import finder from "./finder.mjs";
// const finders = require('./finders.mjs')
describe('rangement file name', () => { describe('rangement file name', () => {
@ -7,16 +6,6 @@ describe('rangement file name', () => {
expect(finder.findFormattedDate('2023-06-23T18.36.47 -- machin bidule.jpg')).toBe('2023-06-23T18.36.47'); expect(finder.findFormattedDate('2023-06-23T18.36.47 -- machin bidule.jpg')).toBe('2023-06-23T18.36.47');
}); });
test('detects file extension in file name', () => { test('detects file extension in file name', () => {
expect(finder.findFileExtension()('2023-06-23T18.36.47 -- machin bidule.jpg')).toBe('jpg'); expect(finder.findFileExtension('2023-06-23T18.36.47 -- machin bidule.jpg')).toBe('.jpg');
}); });
}) })
console.log('finders', finder)
test('adding positive numbers is not zero', () => {
for (let a = 1; a < 10; a++) {
for (let b = 1; b < 10; b++) {
expect(a + b).not.toBe(0);
}
}
});