Eshu

 view release on metacpan or  search on metacpan

t/0107-realworld-js-patterns.t  view on Meta::CPAN

}
END
    is(Eshu->indent_js($in), $exp, 'JS: WeakRef cache with FinalizationRegistry callback');
}

# CommonJS module pattern
{
    my $in = <<'END';
'use strict';

const path = require('path');
const fs   = require('fs');

const DEFAULT_OPTS = {
encoding: 'utf8',
recursive: false,
filter: null,
};

function readDir(dir, opts) {
opts = Object.assign({}, DEFAULT_OPTS, opts);
const results = [];

t/0107-realworld-js-patterns.t  view on Meta::CPAN


walk(dir);
return results;
}

module.exports = { readDir };
END
    my $exp = <<'END';
'use strict';

const path = require('path');
const fs   = require('fs');

const DEFAULT_OPTS = {
	encoding: 'utf8',
	recursive: false,
	filter: null,
};

function readDir(dir, opts) {
	opts = Object.assign({}, DEFAULT_OPTS, opts);
	const results = [];

t/0107-realworld-js-patterns.t  view on Meta::CPAN

	}
	return { host, port: Math.floor(port), tls: Boolean(tls) };
}
END
    is(Eshu->indent_js($in), $exp, 'JS: type guard functions with destructuring and template literals');
}

# Worker thread message handler pattern
{
    my $in = <<'END';
const { parentPort, workerData } = require('worker_threads');

const handlers = {
ping(data) {
return { type: 'pong', ts: Date.now() };
},
process(data) {
const { items } = data;
const results = items.map((item) => {
try {
return { ok: true,  value: transform(item) };

t/0107-realworld-js-patterns.t  view on Meta::CPAN

}
try {
const reply = handler(msg.data);
parentPort.postMessage(reply);
} catch (err) {
parentPort.postMessage({ type: 'error', message: err.message });
}
});
END
    my $exp = <<'END';
const { parentPort, workerData } = require('worker_threads');

const handlers = {
	ping(data) {
		return { type: 'pong', ts: Date.now() };
	},
	process(data) {
		const { items } = data;
		const results = items.map((item) => {
			try {
				return { ok: true,  value: transform(item) };



( run in 3.004 seconds using v1.01-cache-2.11-cpan-6aa56a78535 )