mirror of
https://github.com/lovell/sharp.git
synced 2026-02-04 13:46:19 +01:00
Update tests to meet semistandard code standards
Switch to const/let instead of var
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
'use strict';
|
||||
/*jshint esversion: 6 */
|
||||
|
||||
const fs = require('fs');
|
||||
const request = require('request');
|
||||
@@ -10,7 +9,7 @@ const client = tumblr.createClient({
|
||||
consumer_secret: '***'
|
||||
});
|
||||
|
||||
const fetchImages = function(offset) {
|
||||
const fetchImages = function (offset) {
|
||||
console.log(`Fetching offset ${offset}`);
|
||||
client.posts('humanae', {
|
||||
type: 'photo',
|
||||
@@ -21,8 +20,7 @@ const fetchImages = function(offset) {
|
||||
response.posts.forEach((post) => {
|
||||
const url = post.photos[0].alt_sizes
|
||||
.filter((image) => image.width === 100)
|
||||
.map((image) => image.url)
|
||||
[0];
|
||||
.map((image) => image.url)[0];
|
||||
const filename = `./images/${post.id}.jpg`;
|
||||
try {
|
||||
fs.statSync(filename);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
'use strict';
|
||||
/*jshint esversion: 6 */
|
||||
|
||||
const fs = require('fs');
|
||||
const child_process = require('child_process');
|
||||
const childProcess = require('child_process');
|
||||
|
||||
const a = [];
|
||||
const b = [];
|
||||
@@ -12,7 +11,7 @@ fs.readdirSync('./images')
|
||||
.forEach((file) => {
|
||||
// Extract one pixel, avoiding first DCT block, and return value of A and B channels
|
||||
const command = `convert ./images/${file}[1x1+8+8] -colorspace lab -format "%[fx:u.g] %[fx:u.b]" info:`;
|
||||
const result = child_process.execSync(command, { encoding: 'utf8' });
|
||||
const result = childProcess.execSync(command, { encoding: 'utf8' });
|
||||
const ab = result.split(' ');
|
||||
a.push(ab[0]);
|
||||
b.push(ab[1]);
|
||||
@@ -22,7 +21,7 @@ a.sort((v1, v2) => v1 - v2);
|
||||
b.sort((v1, v2) => v1 - v2);
|
||||
|
||||
// Convert from 0..1 to -128..128
|
||||
const convert = function(v) {
|
||||
const convert = function (v) {
|
||||
return Math.round(256 * (v - 0.5));
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
'use strict';
|
||||
/*jshint esversion: 6 */
|
||||
|
||||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
@@ -16,7 +15,7 @@ const concurrency = os.cpus().length;
|
||||
|
||||
const scores = {};
|
||||
|
||||
const incrementScore = function(accuracy, crop) {
|
||||
const incrementScore = function (accuracy, crop) {
|
||||
if (typeof scores[accuracy] === 'undefined') {
|
||||
scores[accuracy] = {};
|
||||
}
|
||||
@@ -29,36 +28,36 @@ const incrementScore = function(accuracy, crop) {
|
||||
const userData = require('./userData.json');
|
||||
const files = Object.keys(userData);
|
||||
|
||||
async.eachLimit(files, concurrency, function(file, done) {
|
||||
async.eachLimit(files, concurrency, function (file, done) {
|
||||
const filename = path.join(__dirname, 'Image', file);
|
||||
const salientWidth = userData[file].right - userData[file].left;
|
||||
const salientHeight = userData[file].bottom - userData[file].top;
|
||||
sharp(filename).metadata(function(err, metadata) {
|
||||
sharp(filename).metadata(function (err, metadata) {
|
||||
if (err) console.log(err);
|
||||
async.each(Object.keys(crops), function(crop, done) {
|
||||
async.each(Object.keys(crops), function (crop, done) {
|
||||
async.parallel([
|
||||
// Left edge accuracy
|
||||
function(done) {
|
||||
sharp(filename).resize(salientWidth, metadata.height).crop(crops[crop]).toBuffer(function(err, data, info) {
|
||||
function (done) {
|
||||
sharp(filename).resize(salientWidth, metadata.height).crop(crops[crop]).toBuffer(function (err, data, info) {
|
||||
const accuracy = Math.round(Math.abs(userData[file].left - info.cropCalcLeft) / (metadata.width - salientWidth) * 100);
|
||||
incrementScore(accuracy, crop);
|
||||
done();
|
||||
done(err);
|
||||
});
|
||||
},
|
||||
// Top edge accuracy
|
||||
function(done) {
|
||||
sharp(filename).resize(metadata.width, salientHeight).crop(crops[crop]).toBuffer(function(err, data, info) {
|
||||
function (done) {
|
||||
sharp(filename).resize(metadata.width, salientHeight).crop(crops[crop]).toBuffer(function (err, data, info) {
|
||||
const accuracy = Math.round(Math.abs(userData[file].top - info.cropCalcTop) / (metadata.height - salientHeight) * 100);
|
||||
incrementScore(accuracy, crop);
|
||||
done();
|
||||
done(err);
|
||||
});
|
||||
}
|
||||
], done);
|
||||
}, done);
|
||||
});
|
||||
}, function() {
|
||||
}, function () {
|
||||
const report = [];
|
||||
Object.keys(scores).forEach(function(accuracy) {
|
||||
Object.keys(scores).forEach(function (accuracy) {
|
||||
report.push(
|
||||
Object.assign({
|
||||
accuracy: parseInt(accuracy, 10)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
'use strict';
|
||||
/*jshint esversion: 6, loopfunc: true */
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
@@ -8,8 +7,8 @@ const userDataDir = 'UserData';
|
||||
|
||||
const images = {};
|
||||
|
||||
const median = function(values) {
|
||||
values.sort(function(a,b) {
|
||||
const median = function (values) {
|
||||
values.sort(function (a, b) {
|
||||
return a - b;
|
||||
});
|
||||
const half = Math.floor(values.length / 2);
|
||||
@@ -21,7 +20,7 @@ const median = function(values) {
|
||||
};
|
||||
|
||||
// List of files
|
||||
fs.readdirSync(userDataDir).forEach(function(file) {
|
||||
fs.readdirSync(userDataDir).forEach(function (file) {
|
||||
// Contents of file
|
||||
const lines = fs.readFileSync(path.join(userDataDir, file), {encoding: 'utf-8'}).split(/\r\n/);
|
||||
// First line = number of entries
|
||||
@@ -39,8 +38,11 @@ fs.readdirSync(userDataDir).forEach(function(file) {
|
||||
const regions = lines[linePos].split('; ');
|
||||
linePos = linePos + 2;
|
||||
// Parse human-labelled regions for min/max coords
|
||||
const lefts = [], tops = [], rights = [], bottoms = [];
|
||||
regions.forEach(function(region) {
|
||||
const lefts = [];
|
||||
const tops = [];
|
||||
const rights = [];
|
||||
const bottoms = [];
|
||||
regions.forEach(function (region) {
|
||||
if (region.indexOf(' ') !== -1) {
|
||||
const coords = region.split(' ');
|
||||
lefts.push(parseInt(coords[0], 10));
|
||||
|
||||
Reference in New Issue
Block a user