Skip to content

Commit

Permalink
style: format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 31, 2023
1 parent be8facd commit 9713626
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 140 deletions.
Empty file added .prettierrc
Empty file.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"scripts": {
"build": "unbuild",
"dev": "nodemon",
"lint": "eslint --ext .ts .",
"lint": "eslint --ext .ts . && prettier -c src test",
"lint:fix": "eslint --ext .ts . --fix && prettier -w src test",
"prepack": "pnpm build",
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
"start": "node bin/ipx.js",
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { listen } from "listhen";
import { createIPX } from "./ipx";
import { createIPXMiddleware } from "./middleware";

async function main () {
async function main() {
const ipx = createIPX({});
const middleware = createIPXMiddleware(ipx);
await listen(middleware, {
clipboard: false
clipboard: false,
});
}

Expand Down
85 changes: 49 additions & 36 deletions src/handlers/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Handler } from "../types";
import { clampDimensionsPreservingAspectRatio, VArg as VArgument } from "./utils";
import {
clampDimensionsPreservingAspectRatio,
VArg as VArgument,
} from "./utils";

// --------- Context Modifers ---------

Expand All @@ -8,7 +11,7 @@ export const quality: Handler = {
order: -1,
apply: (context, _pipe, quality) => {
context.quality = quality;
}
},
};

// https://sharp.pixelplumbing.com/api-resize#resize
Expand All @@ -17,7 +20,7 @@ export const fit: Handler = {
order: -1,
apply: (context, _pipe, fit) => {
context.fit = fit;
}
},
};

// https://sharp.pixelplumbing.com/api-resize#resize
Expand All @@ -26,7 +29,7 @@ export const position: Handler = {
order: -1,
apply: (context, _pipe, position) => {
context.position = position;
}
},
};

const HEX_RE = /^([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i;
Expand All @@ -36,11 +39,14 @@ export const background: Handler = {
order: -1,
apply: (context, _pipe, background) => {
background = String(background);
if (!background.startsWith("#") && (HEX_RE.test(background) || SHORTHEX_RE.test(background))) {
if (
!background.startsWith("#") &&
(HEX_RE.test(background) || SHORTHEX_RE.test(background))
) {
background = "#" + background;
}
context.background = background;
}
},
};

// --------- Resize ---------
Expand All @@ -49,21 +55,25 @@ export const enlarge: Handler = {
args: [],
apply: (context) => {
context.enlarge = true;
}
},
};

export const width: Handler = {
args: [VArgument],
apply: (context, pipe, width) => {
return pipe.resize(width, undefined, { withoutEnlargement: !context.enlarge });
}
return pipe.resize(width, undefined, {
withoutEnlargement: !context.enlarge,
});
},
};

export const height: Handler = {
args: [VArgument],
apply: (context, pipe, height) => {
return pipe.resize(undefined, height, { withoutEnlargement: !context.enlarge });
}
return pipe.resize(undefined, height, {
withoutEnlargement: !context.enlarge,
});
},
};

export const resize: Handler = {
Expand All @@ -78,24 +88,27 @@ export const resize: Handler = {
}
// sharp's `withoutEnlargement` doesn't respect the requested aspect ratio, so we need to do it ourselves
if (!context.enlarge) {
const clamped = clampDimensionsPreservingAspectRatio(context.meta, { width, height });
const clamped = clampDimensionsPreservingAspectRatio(context.meta, {
width,
height,
});
width = clamped.width;
height = clamped.height;
}
return pipe.resize(width, height, {
fit: context.fit,
position: context.position,
background: context.background
background: context.background,
});
}
},
};

// https://sharp.pixelplumbing.com/api-resize#trim
export const trim: Handler = {
args: [VArgument],
apply: (_context, pipe, threshold) => {
return pipe.trim(threshold);
}
},
};

// https://sharp.pixelplumbing.com/api-resize#extend
Expand All @@ -107,9 +120,9 @@ export const extend: Handler = {
left,
bottom,
right,
background: context.background
background: context.background,
});
}
},
};

// https://sharp.pixelplumbing.com/api-resize#extract
Expand All @@ -121,9 +134,9 @@ export const extract: Handler = {
left,
bottom,
right,
background: context.background
background: context.background,
});
}
},
};

// --------- Operations ---------
Expand All @@ -133,49 +146,49 @@ export const rotate: Handler = {
args: [VArgument],
apply: (context, pipe, angel) => {
return pipe.rotate(angel, {
background: context.background
background: context.background,
});
}
},
};

// https://sharp.pixelplumbing.com/api-operation#flip
export const flip: Handler = {
args: [],
apply: (_context, pipe) => {
return pipe.flip();
}
},
};

// https://sharp.pixelplumbing.com/api-operation#flop
export const flop: Handler = {
args: [],
apply: (_context, pipe) => {
return pipe.flop();
}
},
};

// https://sharp.pixelplumbing.com/api-operation#sharpen
export const sharpen: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (_context, pipe, sigma, flat, jagged) => {
return pipe.sharpen(sigma, flat, jagged);
}
},
};

// https://sharp.pixelplumbing.com/api-operation#median
export const median: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (_context, pipe, size) => {
return pipe.median(size);
}
},
};

// https://sharp.pixelplumbing.com/api-operation#blur
export const blur: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (_context, pipe) => {
return pipe.blur();
}
},
};

// https://sharp.pixelplumbing.com/api-operation#flatten
Expand All @@ -184,41 +197,41 @@ export const flatten: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (context, pipe) => {
return pipe.flatten({
background: context.background
background: context.background,
});
}
},
};

// https://sharp.pixelplumbing.com/api-operation#gamma
export const gamma: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (_context, pipe, gamma, gammaOut) => {
return pipe.gamma(gamma, gammaOut);
}
},
};

// https://sharp.pixelplumbing.com/api-operation#negate
export const negate: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (_context, pipe) => {
return pipe.negate();
}
},
};

// https://sharp.pixelplumbing.com/api-operation#normalize
export const normalize: Handler = {
args: [VArgument, VArgument, VArgument],
apply: (_context, pipe) => {
return pipe.normalize();
}
},
};

// https://sharp.pixelplumbing.com/api-operation#threshold
export const threshold: Handler = {
args: [VArgument],
apply: (_context, pipe, threshold) => {
return pipe.threshold(threshold);
}
},
};

// https://sharp.pixelplumbing.com/api-operation#modulate
Expand All @@ -228,9 +241,9 @@ export const modulate: Handler = {
return pipe.modulate({
brightness,
saturation,
hue
hue,
});
}
},
};

// --------- Colour Manipulation ---------
Expand All @@ -240,15 +253,15 @@ export const tint: Handler = {
args: [VArgument],
apply: (_context, pipe, rgb) => {
return pipe.tint(rgb);
}
},
};

// https://sharp.pixelplumbing.com/api-colour#grayscale
export const grayscale: Handler = {
args: [VArgument],
apply: (_context, pipe) => {
return pipe.grayscale();
}
},
};

// --------- Aliases ---------
Expand Down
25 changes: 19 additions & 6 deletions src/handlers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@ import destr from "destr";
import type { Handler } from "../types";
import * as Handlers from "./handlers";

export function VArg (argument: string) {
export function VArg(argument: string) {
return destr(argument);
}

export function parseArgs (arguments_: string, mappers: Function[]) {
export function parseArgs(
arguments_: string,
mappers: ((...args: any[]) => any)[]
) {
const vargs = arguments_.split("_");
return mappers.map((v, index) => v(vargs[index]));
}

export function getHandler (key): Handler {
export function getHandler(key): Handler {
// eslint-disable-next-line import/namespace
return Handlers[key];
}

export function applyHandler (context, pipe, handler: Handler, argumentsString: string) {
const arguments_ = handler.args ? parseArgs(argumentsString, handler.args) : [];
export function applyHandler(
context,
pipe,
handler: Handler,
argumentsString: string
) {
const arguments_ = handler.args
? parseArgs(argumentsString, handler.args)
: [];
return handler.apply(context, pipe, ...arguments_);
}

export function clampDimensionsPreservingAspectRatio (sourceDimensions, desiredDimensions) {
export function clampDimensionsPreservingAspectRatio(
sourceDimensions,
desiredDimensions
) {
const desiredAspectRatio = desiredDimensions.width / desiredDimensions.height;
let { width, height } = desiredDimensions;
if (width > sourceDimensions.width) {
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export * from "./ipx";
export * from "./middleware";
export type {
Source,
SourceData,
SourceFactory
} from "./types";
export type { Source, SourceData, SourceFactory } from "./types";
Loading

0 comments on commit 9713626

Please sign in to comment.