Skip to content
This repository has been archived by the owner on Apr 19, 2020. It is now read-only.

Commit

Permalink
Use ava
Browse files Browse the repository at this point in the history
  • Loading branch information
thiamsantos committed Mar 24, 2017
1 parent 4fb07e3 commit 94e7121
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
25 changes: 6 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
const fs = require('fs')
const path = require('path')
const filesize = require('filesize')
const imageSize = require('image-size')
const resizeImage = require('resize-img')

function getOutputFileName(input) {
const inputPath = path.parse(input)
return path.resolve(inputPath.dir, inputPath.name + '-lowly' + inputPath.ext)
}

function lowly(input) {
const fileInput = fs.readFileSync(input)
const fileOutput = getOutputFileName(input)

const dimensions = imageSize(fileInput)
function lowly(image) {
const dimensions = imageSize(image)
const finalWidth = 30
const finalHeight = Math.round(finalWidth / dimensions.width * dimensions.height)
resizeImage(fileInput, {width: finalWidth, height: finalHeight})
.then(buff => {
fs.writeFileSync(fileOutput, buff)
console.log('Final size: ' + filesize(buff.length))
})
const finalHeight = Math.round(
finalWidth / dimensions.width * dimensions.height
)
return resizeImage(image, {width: finalWidth, height: finalHeight})
}

module.exports = lowly
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
"description": "Build low quality images",
"main": "index.js",
"scripts": {
"test": "xo && tape test/index.js",
"test": "xo && ava -v",
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
},
"author": "Thiago Santos",
"bin": {
"lowly": "./bin.js"
},
"license": "MIT",
"dependencies": {
"filesize": "^3.5.6",
"image-size": "^0.5.1",
"resize-img": "^1.1.2"
"resize-img": "^1.1.2",
"round-to": "^2.0.0"
},
"devDependencies": {
"ava": "^0.18.2",
"nyc": "^10.1.2",
"tape": "^4.6.3",
"pify": "^2.3.0",
"xo": "^0.18.0"
},
"xo": {
Expand Down
Binary file removed test/fixture-lowly.jpeg
Binary file not shown.
28 changes: 14 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const fs = require('fs')
const path = require('path')
const test = require('tape')
const lowly = require('../')
import fs from 'fs'
import path from 'path'
import test from 'ava'
import pify from 'pify'
import lowly from '../'

test('lowly', t => {
const initialSize = fs.readFileSync(
path.resolve(__dirname, 'fixture.jpeg')
).length
lowly(path.resolve(__dirname, 'fixture.jpeg'))
const finalSize = fs.readFileSync(
path.resolve(__dirname, 'fixture-lowly.jpeg')
).length
const readFile = pify(fs.readFile)

t.ok(initialSize > finalSize, 'the final size should be much lower')
t.end()
test('lowly', async t => {
const image = await readFile(path.resolve(__dirname, 'fixture.jpeg'))
const lowlyImage = await lowly(image)

const initialSize = image.length
const finalSize = lowlyImage.length

t.true(initialSize > finalSize, 'the final size should be much lower')
})

0 comments on commit 94e7121

Please sign in to comment.