Skip to content

Commit

Permalink
refactor: update object destructuring in all pkgs & examples
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 22, 2024
1 parent 0ab67f0 commit f36aeb0
Show file tree
Hide file tree
Showing 39 changed files with 147 additions and 237 deletions.
6 changes: 1 addition & 5 deletions examples/devcards/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ function bmi(state: IAtom<BMIState>) {
// computes new BMI value (if weight was changed) or
// new weight value (if BMI was changed by user)
const calc = (updateWeight = false) => {
let { height, weight, bmi } = state.deref() || {
height: 0,
weight: 0,
bmi: 0,
};
let { height = 0, weight = 0, bmi = 0 } = state.deref();
height *= 0.01;
if (updateWeight) {
state.resetIn(["weight"], bmi * height * height);
Expand Down
16 changes: 5 additions & 11 deletions examples/related-images/src/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ interface LayoutOpts {
// and a function to query the final (or current) max height...
export const columnLayout = (opts: LayoutOpts) => {
const {
gap = 8,
margin = 0,
extraHeight = 0,
snap = 1,
cols,
width: totalWidth,
gap,
margin,
extraHeight,
snap,
} = {
gap: 8,
margin: 0,
extraHeight: 0,
snap: 1,
...opts,
};
} = opts;
// vector of vertical column offsets
const offsets = new Int32Array(cols);
// column width
Expand Down
8 changes: 2 additions & 6 deletions packages/axidraw/src/polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ import { DOWN, MOVE, PEN, UP } from "./commands.js";
*/
export function* polyline(
pts: ReadonlyVec[],
opts: Partial<PolylineOpts>
opts: Partial<PolylineOpts> = {}
): IterableIterator<DrawCommand> {
if (!pts.length) return;
const { speed, delayDown, delayUp, down, onlyGeo } = {
speed: 1,
onlyGeo: false,
...opts,
};
const { speed = 1, onlyGeo = false, delayDown, delayUp, down } = opts;
if (onlyGeo) {
for (let p of pts) yield MOVE(p, speed);
return;
Expand Down
8 changes: 2 additions & 6 deletions packages/bench/src/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,8 @@ export class Profiler
protected _enabled!: boolean;
protected _overhead = 0;

constructor(opts?: Partial<ProfilerOpts>) {
const { warmup, enabled } = {
warmup: 1e6,
enabled: true,
...opts,
};
constructor(opts: Partial<ProfilerOpts> = {}) {
const { warmup = 1e6, enabled = true } = opts;
this.enable();
if (warmup > 0) this.warmup(warmup);
enabled ? this.reset() : this.disable();
Expand Down
13 changes: 8 additions & 5 deletions packages/cellular/src/1d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,14 @@ export class MultiCA1D implements IClear {
pixels.length >= this.width * height,
"target pixel buffer too small"
);
const { cells, mask, prob, probabilistic, rnd, onupdate } = {
probabilistic: false,
rnd: SYSTEM,
...opts,
};
const {
probabilistic = false,
rnd = SYSTEM,
cells,
mask,
prob,
onupdate,
} = opts;
const $ = (id: Target, conf?: Partial<UpdateBufferOpts>) => {
conf &&
conf.perturb &&
Expand Down
5 changes: 1 addition & 4 deletions packages/colored-noise/src/blue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { preseed, sum } from "./utils.js";
* @param opts -
*/
export function* blue(opts?: Partial<ColoredNoiseOpts>) {
const { bins, scale, rnd } = {
...DEFAULT_OPTS,
...opts,
};
const { bins, scale, rnd } = { ...DEFAULT_OPTS, ...opts };
const state = preseed(bins, scale, rnd);
state.forEach((x, i) => (state[i] = i & 1 ? x : -x));
const invN = 1 / bins;
Expand Down
6 changes: 1 addition & 5 deletions packages/colored-noise/src/pink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import { preseed, sum } from "./utils.js";
* @param opts -
*/
export function* pink(opts?: Partial<ColoredNoiseOpts>) {
const { bins, scale, rnd } = {
...DEFAULT_OPTS,
bins: 8,
...opts,
};
const { bins = 8, scale, rnd } = { ...DEFAULT_OPTS, ...opts };
const state = preseed(bins, scale, rnd);
const invN = 1 / bins;
let acc = sum(state);
Expand Down
5 changes: 1 addition & 4 deletions packages/colored-noise/src/red.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { preseed, sum } from "./utils.js";
* @param opts -
*/
export function* red(opts?: Partial<ColoredNoiseOpts>) {
const { bins, scale, rnd } = {
...DEFAULT_OPTS,
...opts,
};
const { bins, scale, rnd } = { ...DEFAULT_OPTS, ...opts };
const state = preseed(bins, scale, rnd);
const invN = 1 / bins;
let acc = sum(state);
Expand Down
9 changes: 2 additions & 7 deletions packages/csv/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@ export function formatCSV(
src: Iterable<CSVRow | CSVRecord>
): IterableIterator<string>;
export function formatCSV(
opts?: Partial<CSVFormatOpts>,
opts: Partial<CSVFormatOpts> = {},
src?: Iterable<CSVRow | CSVRecord>
): any {
return isIterable(src)
? iterator(formatCSV(opts), src)
: (rfn: Reducer<any, string>) => {
let { header, cols, delim, quote } = {
delim: ",",
quote: `"`,
cols: [],
...opts,
};
let { cols = [], delim = ",", quote = '"', header } = opts;
let colTx: Nullable<Stringer<any>>[];
const reQuote = new RegExp(quote, "g");
const reduce = rfn[2];
Expand Down
21 changes: 17 additions & 4 deletions packages/csv/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ export function parseCSV(opts?: Partial<CSVOpts>, src?: Iterable<string>): any {
return isIterable(src)
? iterator1(parseCSV(opts), src)
: (rfn: Reducer<any, CSVRecord>) => {
const { all, cols, delim, quote, comment, trim, header } = {
all: true,
const {
all = true,
cols,
comment,
delim,
header,
quote,
trim,
} = {
...DEFAULT_OPTS,
...opts,
};
Expand Down Expand Up @@ -207,8 +214,14 @@ export function parseCSVSimple(
return isIterable(src)
? iterator1(parseCSVSimple(opts), src)
: (rfn: Reducer<any, CSVRecord>) => {
const { cols, delim, quote, comment, trim, header } = {
header: true,
const {
header = true,
cols,
comment,
delim,
quote,
trim,
} = {
...DEFAULT_OPTS,
...opts,
};
Expand Down
12 changes: 5 additions & 7 deletions packages/file-io/src/file-chunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ export interface FileChunkOpts {
* @param path
* @param opts
*/
export async function* fileChunks(path: string, opts?: Partial<FileChunkOpts>) {
let { logger, size, start, end } = {
size: 1024,
start: 0,
end: Infinity,
...opts,
};
export async function* fileChunks(
path: string,
opts: Partial<FileChunkOpts> = {}
) {
let { size = 1024, start = 0, end = Infinity, logger } = opts;
logger &&
logger.debug(`start reading file chunks (size: 0x${size}): ${path}`);
let fd: Nullable<FileHandle> = undefined;
Expand Down
9 changes: 2 additions & 7 deletions packages/fuzzy-viz/src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,9 @@ export const fuzzySetToSvg =
serialize(convertTree(fuzzySetToHiccup(opts)(fn, domain, res)));

export const fuzzySetToAscii =
(opts?: Partial<AsciiVizOpts>): InstrumentFn<string> =>
(opts: Partial<AsciiVizOpts> = {}): InstrumentFn<string> =>
(fn, domain, res) => {
const { width, height, empty } = {
width: 100,
height: 16,
empty: ".",
...opts,
};
const { width = 100, height = 16, empty = "." } = opts;
const [min, max] = domain;
const delta = (max - min) / width;
const vals: number[] = [];
Expand Down
22 changes: 7 additions & 15 deletions packages/fuzzy-viz/src/var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,13 @@ export const varToHiccup = (
opts: Partial<VizualizeVarOpts> = {}
) => {
const {
samples,
width,
height,
labels,
stroke: strokeFn,
fill: fillFn,
} = {
samples: 200,
width: 600,
height: 100,
labels: true,
stroke: (x: number) => `hsl(${(x * 360) | 0},100%,40%)`,
fill: (x: number) => `hsla(${(x * 360) | 0},100%,50%,20%)`,
...opts,
};
samples = 200,
width = 600,
height = 100,
labels = true,
stroke: strokeFn = (x: number) => `hsl(${(x * 360) | 0},100%,40%)`,
fill: fillFn = (x: number) => `hsla(${(x * 360) | 0},100%,50%,20%)`,
} = opts;
const keys = Object.keys(terms);
const dt = (max - min) / samples;
const ds = width / samples;
Expand Down
14 changes: 10 additions & 4 deletions packages/geom-axidraw/src/as-axidraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ function* __points(
opts?: Partial<AsAxiDrawOpts>
): IterableIterator<DrawCommand> {
if (!pts.length) return;
const { clip, delayDown, delayUp, down, skip, speed, sort, interleave } = {
sort: pointsByNearestNeighbor(),
...__axiAttribs(attribs),
};
const {
sort = pointsByNearestNeighbor(),
clip,
delayDown,
delayUp,
down,
interleave,
skip,
speed,
} = __axiAttribs(attribs);
const clipPts = clip || opts?.clip;
if (clipPts) {
pts = pts.filter((p) => !!pointInPolygon2(p, clipPts));
Expand Down
8 changes: 1 addition & 7 deletions packages/geom-trace-bitmap/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,7 @@ export const traceLines = (
tx: PointTransform2D,
acc: VecPair[] = []
) => {
let { img, select, clear, last, min, max } = {
clear: 0,
last: true,
min: 2,
max: Infinity,
...opts,
};
let { clear = 0, last = true, min = 2, max = Infinity, img, select } = opts;
min--;
let curr: [number, number][] = [];
let prevBorder = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/grid-iterators/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { GridIterOpts2D } from "./api.js";
import { ident } from "./transforms.js";

export const __opts = (opts: GridIterOpts2D) => {
let { cols, rows, tx } = { rows: opts.cols, tx: ident, ...opts };
let { cols, rows = opts.cols, tx = ident } = opts;
[cols, rows] = asInt(cols, rows);
return { cols, rows, tx: tx(cols, rows) };
};
8 changes: 1 addition & 7 deletions packages/hiccup-canvas/src/packed-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ const __drawPoints = (
cmd: "fill" | "stroke",
cmdR: "fillRect" | "strokeRect"
) => {
const { start, cstride, estride, size } = {
start: 0,
cstride: 1,
estride: 2,
size: 1,
...opts,
};
const { start = 0, cstride = 1, estride = 2, size = 1 } = opts;
let num =
opts && opts.num != null
? opts.num
Expand Down
7 changes: 1 addition & 6 deletions packages/hiccup-canvas/src/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ export const __drawPackedPoly = (
opts: IObjectOf<any>,
pts: ArrayLike<number>
) => {
const { start, cstride, estride } = {
start: 0,
cstride: 1,
estride: 2,
...opts,
};
const { start = 0, cstride = 1, estride = 2 } = opts;
let num =
opts && opts.num != null
? opts.num
Expand Down
4 changes: 2 additions & 2 deletions packages/msgpack/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const BMAX64 = BigInt("0xffffffffffffffff");
const MIN64 = Number(BMIN64);
const MAX64 = Number(BMAX64);

export const serialize = (src: any, opts?: Partial<EncodeOpts>) => {
const { initial, resolve } = { initial: 256, ...opts };
export const serialize = (src: any, opts: Partial<EncodeOpts> = {}) => {
const { initial = 256, resolve } = opts;
let buf = new Uint8Array(initial);
let view = new DataView(buf.buffer);
let pos = 0;
Expand Down
4 changes: 2 additions & 2 deletions packages/oquery/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export const matchMultiple = <T extends QueryObj = QueryObj, V = any>(
key: QueryTerm["q"][0],
includes: V[],
excludes: V[],
opts?: Partial<MatchMultipleOpts<V>>
opts: Partial<MatchMultipleOpts<V>> = {}
): QueryTerm<T> => {
const { union, value: valueFn } = { union: false, ...opts };
const { union = false, value: valueFn } = opts;
return excludes.length
? {
q: [
Expand Down
8 changes: 2 additions & 6 deletions packages/pixel-dither/src/dither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ import type { DitherKernel, DitherOpts } from "./api.js";
export const ditherWith = (
kernel: DitherKernel,
img: IntBuffer,
opts?: Partial<DitherOpts>
opts: Partial<DitherOpts> = {}
) => {
const { channels, bleed, threshold } = {
bleed: 1,
threshold: 0.5,
...opts,
};
const { bleed = 1, threshold = 0.5, channels } = opts;
const { format, width, height } = img;
const { ox, oy, weights, shift } = kernel;
let p: number, err: number;
Expand Down
16 changes: 5 additions & 11 deletions packages/pixel/src/convolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,12 @@ const initKernel = (
/** @internal */
const initConvolve = (src: FloatBuffer, opts: ConvolveOpts) => {
const {
channel = 0,
offset = 0,
scale = 1,
stride: sampleStride = 1,
kernel,
channel,
stride: sampleStride,
scale,
offset,
} = {
channel: 0,
offset: 0,
scale: 1,
stride: 1,
...opts,
};
} = opts;
const size = kernel.size;
const [kw, kh] = __asIntVec(size);
const [strideX, strideY] = __asIntVec(sampleStride);
Expand Down
Loading

0 comments on commit f36aeb0

Please sign in to comment.