Skip to content

Commit

Permalink
originMultiplier metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Jun 22, 2020
1 parent b79cca5 commit 84f04e4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
5 changes: 1 addition & 4 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"src/**/*.{ts,tsx}": [
"trigen-scripts lint:ts",
"git add"
]
"src/**/*.{ts,tsx}": "trigen-scripts lint:ts"
}
4 changes: 3 additions & 1 deletion src/ISrcsetVinyl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
} from 'sharp';

export default interface ISrsetVinyl extends Vinyl {
metadata?: Metadata;
metadata?: Metadata & {
originMultiplier?: number;
};
postfix?: string;
}
4 changes: 4 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export async function attachMetadata(source: Vinyl, force = false): Promise<ISrs
}

try {

const originMultiplier = source?.metadata?.originMultiplier;

source.metadata = await Sharp(source.contents as Buffer).metadata();
source.metadata.originMultiplier = originMultiplier;
} catch (err) {
return source;
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,15 @@ export default class SrcsetGenerator {

if (width !== null) {

const calculatedWidth = originWidth && width <= 1
const isMultiplier = width <= 1;
const calculatedWidth = originWidth && isMultiplier
? Math.ceil(width * originWidth)
: width;

if (isMultiplier) {
target.metadata.originMultiplier = width;
}

this.addPostfix(target, calculatedWidth, width, config.postfix);

if (calculatedWidth < originWidth) {
Expand Down
14 changes: 14 additions & 0 deletions test/SrcsetGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,19 @@ describe('SrcsetGenerator', () => {
} as any))
).rejects.toThrow();
});

it('should add originMultiplier to metadate', async () => {

const [
x2,
x1
] = await vinylsFromAsyncIterator(srcset.generate(image, {
scalingUp: false,
width: [1, .5]
}));

expect(x2.metadata.originMultiplier).toBe(1);
expect(x1.metadata.originMultiplier).toBe(.5);
});
});
});

0 comments on commit 84f04e4

Please sign in to comment.