diff --git a/index.js b/index.js index 74dba0e..7c88b2e 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,20 @@ import alfy from 'alfy'; -import {findFilter, findCommand, isFileAction, getClipboardContent} from './src/utils.js'; +import { + findFilter, + findCommand, + isFileActionCaseSensitive, + getClipboardContent, +} from './src/utils.js'; import {processCopyPaste} from './src/process-copy-paste.js'; import {processFilename} from './src/process-filename.js'; import {processRename} from './src/process-rename.js'; const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input); const {inputWithoutCommand, foundCommand} = findCommand(inputWithoutFilter); -let input = inputWithoutCommand || getClipboardContent(); - -// Force File Action input to be with new lines instead of tabs! -// NOTE: this may be problematic if content contains tabs. -input = input.replaceAll('\t', '\n'); +const input = (inputWithoutCommand || getClipboardContent()) + .replaceAll('\t', '\n') + .split('\n') + .map(element => element.trim()); function process(input) { if (foundCommand === 'rename') { @@ -21,7 +25,7 @@ function process(input) { return processFilename(input); } - if (isFileAction(input)) { + if (isFileActionCaseSensitive(input)) { return [ { title: 'Rename file with Slugify', diff --git a/src/utils.js b/src/utils.js index 2bf8eb3..7df2126 100644 --- a/src/utils.js +++ b/src/utils.js @@ -43,18 +43,25 @@ export function getClipboardContent() { }).stdout; } -export function isFileAction(input) { - const lines = input.split('\n').filter(element => element.trim() !== ''); - const filePathLines = lines - .map(line => { - if (input.slice(0, 1) !== '/') { +export function isFileActionCaseSensitive(input) { + const filePaths = input + .filter(element => Boolean(element.trim())) + .filter(filepath => { + if (filepath.slice(0, 1) !== '/') { return false; } - return fileExistsWithCaseSync(line.trim()); - }) - .filter(element => element); - return filePathLines.length > 0; + return fileExistsWithCaseSync(filepath.trim()); + }); + return filePaths.length > 0; +} + +export function fileExistsWithCaseSync(filepath) { + try { + return Boolean(discoverPathSync(filepath.trim())); + } catch { + return false; + } } export function findFilter(input) { @@ -80,14 +87,11 @@ export function findCommand(input) { } export function doSlugify(input, options) { - const lines = input.split('\n'); - input = lines.length > 0 ? lines : [input]; return input.map(line => slugifyLine(line.trim(), options)).join('\n'); } export function doSlugifyFilename(input, options) { - return input.split('\n').map(line => { - line = line.trim(); + return input.map(line => { const {name} = path.parse(line); //=> "hello" const {ext} = path.parse(line); //=> ".html" const dir = path.dirname(line); @@ -116,11 +120,3 @@ export function slugifyLine(output, options) { return output; } - -export function fileExistsWithCaseSync(filepath) { - try { - return Boolean(discoverPathSync(filepath.trim())); - } catch { - return false; - } -}