From ed23623be00c077234390ded1da8585ef4b9096c Mon Sep 17 00:00:00 2001 From: Tykayn Date: Wed, 5 Jul 2023 22:34:32 +0200 Subject: [PATCH] enable testing mjs files --- babel.config.js | 7 +++++++ finder.mjs | 34 +++++++++++++++++++--------------- finder.test.js | 17 +++++++++++++++++ index.mjs | 16 +++++++--------- main.test.js | 17 +++-------------- 5 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 babel.config.js create mode 100644 finder.test.js diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..fbeebd3 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,7 @@ +const presets = [ + [ + "@babel/preset-env", + ] +]; + +module.exports = { presets }; \ No newline at end of file diff --git a/finder.mjs b/finder.mjs index 68fe9c6..5ed233a 100644 --- a/finder.mjs +++ b/finder.mjs @@ -4,7 +4,8 @@ import { tagSectionSeparator, tagSeparator } from './configs.mjs' import exifr from 'exifr' import moment from 'moment' -import path from 'path' +import log from 'log_level' + /** * finds patterns for file name @@ -25,8 +26,8 @@ export default class finder { } static findFormattedDate (filepath) { - let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ig) - // console.log('match findFormattedDate', match) + let match = filepath.match(/\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}/ig) + log.debug('match findFormattedDate', match) let result = '' if (match && match[0]) { result = match[0] @@ -35,8 +36,11 @@ export default class finder { } static findFileExtension (inputString) { + if(!inputString){ + return ''; + } 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, '') - // console.log('extensionFile', extensionFile) + log.debug('extensionFile', extensionFile) if (inputString.includes(tagSectionSeparator)) { - // console.log('inputString', inputString) + log.debug('inputString', inputString) if (inputString.length) { let boom = inputString.split(tagSectionSeparator) - // console.log('boom', boom) + log.debug('boom', boom) if (boom.length) { let fileSectionsName = boom.splice(tagSeparator) listOfTags = [...fileSectionsName[1].trim().split(tagSeparator)] - // console.log('listOfTags', listOfTags) + log.debug('listOfTags', listOfTags) } else { console.log('no boom', boom) } @@ -164,17 +168,17 @@ export default class finder { 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) { - // 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) } 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) } 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) } if (exifData.FileAccessDateTime) { @@ -184,11 +188,11 @@ export default class finder { moments.push(exifData.FileInodeChangeDateTime) } 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) } 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) } @@ -198,7 +202,7 @@ export default class finder { }) let minDate = moment.min(moments) - // console.log('minDate :::::::::', minDate) + log.debug('minDate :::::::::', minDate) console.log('minDate :::::::::', minDate.format('yyyy-MM-DDTHH:mm:ss')) return minDate.format('yyyy-MM-DDTHH:mm:ss') diff --git a/finder.test.js b/finder.test.js new file mode 100644 index 0000000..bb8ed14 --- /dev/null +++ b/finder.test.js @@ -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') + }) +}) \ No newline at end of file diff --git a/index.mjs b/index.mjs index 9148a60..4175b49 100644 --- a/index.mjs +++ b/index.mjs @@ -11,7 +11,7 @@ import minimist from 'minimist' /** --------------------- custom utilities and configuration --------------------- */ -import { enableTestsLocally, reportStatistics,tagSectionSeparator, tagSeparator } from './configs.mjs' +import { enableTestsLocally, reportStatistics, tagSectionSeparator, tagSeparator } from './configs.mjs' import { TestFindFormattedDate, TestScreenShotIsFoundAndRenamed, @@ -46,14 +46,14 @@ function prependFileName (fileProperties, newText) { return fileProperties } -function makeFileNameFromProperties(fileProperties) { +function makeFileNameFromProperties (fileProperties) { let tagPlace = '' if (fileProperties.tags.length) { tagPlace = ' ' + tagSectionSeparator + ' ' } // return finder.cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension).replace(+' ' + tagSectionSeparator + ' ' + '.', '.') - return ''+fileProperties.dateStampExif + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension + return '' + fileProperties.dateStampExif + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension } function shouldWeChangeName (structureForFile) { @@ -65,10 +65,9 @@ function shouldWeChangeName (structureForFile) { console.log('\n ancien nom :', structureForFile.fileNameOriginal) // console.log(' nouveau nom:', foundDate +structureForFile.freeText + structureForFile.tags.join(tagSeparator) + structureForFile.extension ) console.log(' nouveau nom:', newName) - if(! mini_arguments.dryRun){ + if (!mini_arguments.dryRun) { renameFile(structureForFile.fullPath, structureForFile.folderPath + newName) - } - else{ + } else { console.log('no renaming for real, this is a dry run') } } else { @@ -101,13 +100,12 @@ async function guessFileNameOnAllFilesFromArguments () { console.log(' =>>>>>>> foundDate : ', foundDate) if (foundDate) { - // finder.findEarliestDateInExifData(fullPath).then(response => { // console.log(' ... trouvée') // if (response) { - structureForFile.dateStampExif = foundDate + structureForFile.dateStampExif = foundDate - shouldWeChangeName(structureForFile) + shouldWeChangeName(structureForFile) // } // }) diff --git a/main.test.js b/main.test.js index be3d3c6..5205ae5 100644 --- a/main.test.js +++ b/main.test.js @@ -1,5 +1,4 @@ -import finder from "./finders.mjs"; -// const finders = require('./finders.mjs') +import finder from "./finder.mjs"; 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'); }); 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); - } - } -}); - +}) \ No newline at end of file