Skip to content

Commit

Permalink
v23.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
torn4dom4n committed Aug 5, 2023
1 parent 8841e1b commit d3ea78e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
33 changes: 19 additions & 14 deletions src/getImages.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import fs from 'node:fs/promises';
import { promises as fs } from 'fs';
import { glob } from 'glob';
import { getPlaiceholder } from 'plaiceholder';

export async function getImages(pattern) {
const files = glob.sync(pattern, { posix: true });
const imagePromises = files.map(async (file) => {
const src = file.replace('images/', '');
const buffer = await fs.readFile(file);
const {
metadata: { height, width },
base64,
} = await getPlaiceholder(buffer);
return { src, width, height, base64 };
});
try {
const files = glob.sync(pattern, { posix: true });
const imagePromises = files.map(async (file) => {
const src = file.replace(/^.*[\\\/]/, '');
const buffer = await fs.readFile(file);
const {
metadata: { height, width },
base64,
} = await getPlaiceholder(buffer);
return { src, width, height, base64 };
});

const images = await Promise.all(imagePromises);
const images = await Promise.all(imagePromises);

images.sort((a, b) => a.src.localeCompare(b.src));
images.sort((a, b) => a.src.localeCompare(b.src));

return images;
return images;
} catch (error) {
console.error('Error fetching images:', error);
throw error;
}
}
26 changes: 13 additions & 13 deletions tests/getImages.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import { getImages } from '../src/getImages.mjs';

const images = await getImages('images/*.{jpg,png}');

test('function returns an array of images', async () => {
test('getImages should return an array of images', () => {
assert.type(images, 'object');
assert.is(images.length, 2);
assert.equal(images.length, 2);

assert.type(images[0].src, 'string');
assert.type(images[0].width, 'number');
assert.type(images[0].height, 'number');
assert.type(images[0].base64, 'string');
});

test('function sorts images alphabetically', async () => {
assert.is(images[0].src, 'eveling-salazar-TqikiXaDrf4-unsplash.jpg');
assert.is(images[1].src, 'mo-ZLBmpXhbzMk-unsplash.jpg');
test('getImages should sort images alphabetically', () => {
assert.equal(images[0].src, 'eveling-salazar-TqikiXaDrf4-unsplash.jpg');
assert.equal(images[1].src, 'mo-ZLBmpXhbzMk-unsplash.jpg');
});

test('function gets width and height of images', async () => {
assert.is(images[0].width, 480);
assert.is(images[0].height, 600);
test('getImages should get width and height of images', () => {
assert.equal(images[0].width, 480);
assert.equal(images[0].height, 600);

assert.is(images[1].width, 480);
assert.is(images[1].height, 613);
assert.equal(images[1].width, 480);
assert.equal(images[1].height, 613);
});

test('function gets base64 of images', async () => {
assert.is(
test('getImages should get base64 of images', () => {
assert.equal(
images[0].base64,
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAECAIAAADETxJQAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAMUlEQVR4nGP48+TZnJL82cW5DAuampNM9fdP7WNQYWCwEBK6vnM7Q0FAQK6P7/+vXwFwfBHUaTcNcAAAAABJRU5ErkJggg=='
);
assert.is(
assert.equal(
images[1].base64,
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAECAIAAADETxJQAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAM0lEQVR4nAEoANf/AKjg/6zf/7jr/wDt//+0vsPq5+QALjFWNDZOa15vAAAANAAFOgACOPlHEtuYfOsCAAAAAElFTkSuQmCC'
);
Expand Down

0 comments on commit d3ea78e

Please sign in to comment.