Skip to content

Commit

Permalink
Compose util
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 committed Feb 25, 2017
1 parent bd9bff0 commit f6e6c9e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const convert = ({ files, outputPath, outputType, name }) => {
execS('which convert', (error) => {
if (error) reject(error)

const outputName = name || createOutputFileName(files, outputType)
const outputName = name || createOutputFileName(outputType)(files)
const command = [
'convert',
...files.map(replaceSpaceCharacters), // input files
Expand Down
5 changes: 2 additions & 3 deletions src/components/Converter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
STAGING,
CONVERTING,
FAILED,
DONE,
operations
DONE
} from '../helpers/constants'

const drop = (props, monitor, component) => {
Expand Down Expand Up @@ -132,7 +131,7 @@ class Sanitizer extends Component {
{/* TODO make this compose better */}
<input
type="text"
value={displayOutputFileName(this.state.files, this.state.outputType)}
value={displayOutputFileName(this.state.outputType)(this.state.files)}
/>
<div className="row">
<div className="switch">
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/functional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const pluck = key => obj => obj[key]

const map = f => mappable => mappable.map(f)

const compose = (...fns) => res => fns.reduce((accum, next) => next(accum), res)

module.exports = {
pluck,
map,
compose
}
9 changes: 5 additions & 4 deletions src/helpers/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { pluck, compose, map } = require('./functional')

const replaceSpaceCharacters = str =>
str.replace(/\s/g, '\\ ')

Expand All @@ -13,17 +15,16 @@ const filterImages = files => Object.keys(files)
.map(key => files[key])
.filter(file => file.type.includes('image'))

const createOutputFileName = (files, outputType) => `ALCHEMY-${concatFiles(files)}.${outputType || 'pdf'}`
const createOutputFileName = outputType => files => `ALCHEMY-${concatFiles(files)}.${outputType || 'pdf'}`

function centerEllipsis(str, length = 7) {
return (str.length > (length * 2) + 1) ?
`${str.substr(0, length)}...${str.substr(str.length - length, str.length)}` :
str
}

// TODO curry?
const displayOutputFileName = (files, outputType) =>
centerEllipsis(createOutputFileName(filterImages(files).map(f => f.path), outputType))
const displayOutputFileName = outputType =>
compose(filterImages, map(pluck('path')), createOutputFileName(outputType), centerEllipsis)

const uniqueFiles = (files, newArray) =>
newArray.reduce((accum, next) => {
Expand Down

0 comments on commit f6e6c9e

Please sign in to comment.