Skip to content

Commit

Permalink
Change 'encodedAs' to 'encodedWith'
Browse files Browse the repository at this point in the history
  • Loading branch information
atjn committed May 13, 2021
1 parent 875c245 commit 25754b9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 6 additions & 5 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand All @@ -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);
}
```

Expand All @@ -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: {
Expand All @@ -139,6 +139,7 @@ console.log(await image.encodedAs.jxl);
9, 10, 10, 9,
... //the entire raw encoded image
],
extension: 'jxl',
size: 1266975 //bytes
}
```
Expand Down
7 changes: 4 additions & 3 deletions api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async function encodeImage({
return {
optionsUsed,
binary,
extension: encoders[encName].extension,
size: binary.length,
};
}
Expand All @@ -117,7 +118,7 @@ class Image {
constructor (workerPool, file) {
this.workerPool = workerPool;
this.decoded = workerPool.dispatchJob({operation: 'decode', file});
this.encodedAs = {};
this.encodedWith = {};
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -178,7 +179,7 @@ class Image {
),
});
}
await Promise.all(Object.values(this.encodedAs));
await Promise.all(Object.values(this.encodedWith));
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 25754b9

Please sign in to comment.