From 25754b91b737e8f2e15c519acebc37edac1b08e0 Mon Sep 17 00:00:00 2001 From: atjn Date: Thu, 13 May 2021 19:34:50 +0200 Subject: [PATCH] Change 'encodedAs' to 'encodedWith' --- api/README.md | 11 ++++++----- api/src/index.js | 7 ++++--- cli/src/index.js | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/api/README.md b/api/README.md index db302cfa..30a1d29b 100644 --- a/api/README.md +++ b/api/README.md @@ -81,7 +81,7 @@ When you have encoded an image, you normally want to write it to a file. This example takes an image that has been encoded as a `jpg` and writes it to a file: ```js -const rawEncodedImage = (await image.encodedAs.jpg).binary; +const rawEncodedImage = (await image.encodedWidth.mozjpeg).binary; fs.writeFile('/path/to/new/image.jpg', rawEncodedImage); ``` @@ -91,8 +91,8 @@ This example iterates through all encoded versions of the image and writes them ```js const newImagePath = '/path/to/image.'; //extension is added automatically -for(const [extension, encodedImage] of Object.entries(image.encodedAs)){ - fs.writeFile(newImagePath + extension, (await encodedImage).binary); +for(const encodedImage of Object.values(image.encodedWith)){ + fs.writeFile(newImagePath + (await encodedImage).extension, (await encodedImage).binary); } ``` @@ -119,11 +119,11 @@ console.log(await image.decoded); } ``` -Information about an encoded image can be found at `Image.encodedAs[extension]`. It looks something like this: +Information about an encoded image can be found at `Image.encodedWith[encoderName]`. It looks something like this: ```js -console.log(await image.encodedAs.jxl); +console.log(await image.encodedWith.jxl); // Returns: { optionsUsed: { @@ -139,6 +139,7 @@ console.log(await image.encodedAs.jxl); 9, 10, 10, 9, ... //the entire raw encoded image ], + extension: 'jxl', size: 1266975 //bytes } ``` diff --git a/api/src/index.js b/api/src/index.js index 0d6c0fb9..321d7d0e 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -91,6 +91,7 @@ async function encodeImage({ return { optionsUsed, binary, + extension: encoders[encName].extension, size: binary.length, }; } @@ -117,7 +118,7 @@ class Image { constructor (workerPool, file) { this.workerPool = workerPool; this.decoded = workerPool.dispatchJob({operation: 'decode', file}); - this.encodedAs = {}; + this.encodedWith = {}; } /** @@ -165,7 +166,7 @@ class Image { encRef.defaultEncoderOptions, options, ); - this.encodedAs[encRef.extension] = this.workerPool.dispatchJob({ + this.encodedWith[encName] = this.workerPool.dispatchJob({ operation: 'encode', bitmap, encName, @@ -178,7 +179,7 @@ class Image { ), }); } - await Promise.all(Object.values(this.encodedAs)); + await Promise.all(Object.values(this.encodedWith)); } } diff --git a/cli/src/index.js b/cli/src/index.js index 26651120..bbc4452e 100755 --- a/cli/src/index.js +++ b/cli/src/index.js @@ -171,8 +171,8 @@ async function processFiles(files) { .then(async () => { jobsFinished++; const outputPath = path.join(program.opts().outputDir, program.opts().suffix + path.basename(originalFile, path.extname(originalFile))); - for(const [extension, output] of Object.entries(image.encodedAs)){ - const outputFile = `${outputPath}.${extension}`; + for(const output of Object.values(image.encodedWith)){ + const outputFile = `${outputPath}.${(await output).extension}`; await fsp.writeFile(outputFile, (await output).binary); results.get(image).outputs.push( Object.assign(