utilitas 1999.1.25 → 1999.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bot.mjs CHANGED
@@ -1,13 +1,10 @@
1
1
  // @todo: New text of the message, 1-4096 characters after entities parsing
2
2
 
3
3
  import {
4
- log as _log,
5
- base64Encode, countKeys, ensureArray, ensureString, getTimeIcon,
6
- humanReadableBoolean, ignoreErrFunc, insensitiveCompare, insensitiveHas,
7
- isSet, lastItem,
8
- need, parseJson, prettyJson, splitArgs,
9
- throwError,
10
- timeout, trim, which,
4
+ log as _log, base64Encode, countKeys, ensureArray, ensureString,
5
+ getTimeIcon, humanReadableBoolean, ignoreErrFunc, insensitiveCompare,
6
+ insensitiveHas, isSet, lastItem, need, parseJson, prettyJson, splitArgs,
7
+ throwError, timeout, trim, which,
11
8
  } from './utilitas.mjs';
12
9
 
13
10
  import { readdirSync } from 'fs';
@@ -34,7 +31,6 @@ const lines = (arr, sep = '\n') => arr.join(sep);
34
31
  const lines2 = arr => lines(arr, '\n\n');
35
32
  const uList = arr => lines(arr.map(x => `- ${x}`));
36
33
  const oList = arr => lines(arr.map((v, k) => `${k + 1}. ${v}`));
37
- const map = obj => uList(Object.entries(obj).map(([k, v]) => `${k}: ${v}`));
38
34
  const isMarkdownError = e => e?.description?.includes?.("can't parse entities");
39
35
  const sendMd = (cId, cnt, opt) => send(cId, cnt, { parse_mode, ...opt || {} });
40
36
  const getFile = async (id, op) => (await get(await getFileUrl(id), op)).content;
@@ -424,12 +420,14 @@ const subconscious = [{
424
420
  ctx.complete = async (options) => await ctx.ok('☑️', options);
425
421
  ctx.json = async (obj, options) => await ctx.ok(json(obj), options);
426
422
  ctx.list = async (list, options) => await ctx.ok(uList(list), options);
427
- ctx.map = async (obj, options) => await ctx.ok(map(obj), options);
428
423
  ctx.media = async (fnc, src, options) => ctx.done.push(await ctx[fnc]({
429
424
  [src?.toLowerCase?.()?.startsWith?.('http') ? 'url' : 'source']: src
430
425
  }, getExtra(ctx, options)));
431
426
  ctx.audio = async (sr, op) => await ctx.media('replyWithAudio', sr, op);
432
427
  ctx.image = async (sr, op) => await ctx.media('replyWithPhoto', sr, op);
428
+ ctx.sendConfig = async (obj, options, _ctx) => await ctx.ok(prettyJson(
429
+ obj, { code: true, md: true }
430
+ ), options);
433
431
  ctx.speech = async (cnt, options) => {
434
432
  let file;
435
433
  if (Buffer.isBuffer(cnt)) {
@@ -892,13 +890,13 @@ const subconscious = [{
892
890
  try {
893
891
  const _config = {
894
892
  ...ctx.session.config = {
895
- ...ctx.session.config,
896
- ...ctx.config = parsed || await parseArgs(ctx.cmd.args),
893
+ ...ctx.session.config, ...ctx.config = parsed
894
+ || await parseArgs(ctx.cmd.args, ctx),
897
895
  }
898
896
  };
899
897
  assert(countKeys(ctx.config), 'No option matched.');
900
- Object.keys(ctx.config).map(x => _config[x] += ' <-- SET');
901
- await ctx.map(_config);
898
+ Object.keys(ctx.config).map(x => _config[x] += ' 🖋');
899
+ await ctx.sendConfig(_config, null, ctx);
902
900
  } catch (err) {
903
901
  await ctx.er(err.message || err);
904
902
  }
@@ -1038,7 +1036,7 @@ const establish = (bot, module, options) => {
1038
1036
  } : module.func);
1039
1037
  };
1040
1038
 
1041
- const parseArgs = async args => {
1039
+ const parseArgs = async (args, ctx) => {
1042
1040
  const { values, tokens } = _parseArgs({
1043
1041
  args: splitArgs((args || '').replaceAll('—', '--')),
1044
1042
  options: bot._.args, tokens: true
@@ -1046,7 +1044,7 @@ const parseArgs = async args => {
1046
1044
  const result = {};
1047
1045
  for (let x of tokens) {
1048
1046
  result[x.name] = bot._.args[x.name]?.validate
1049
- ? await bot._.args[x.name].validate(values[x.name])
1047
+ ? await bot._.args[x.name].validate(values[x.name], ctx)
1050
1048
  : values[x.name];
1051
1049
  }
1052
1050
  return result;
@@ -1155,14 +1153,20 @@ export {
1155
1153
  COMMAND_LIMIT,
1156
1154
  EMOJI_BOT,
1157
1155
  EMOJI_SPEECH,
1158
- EMOJI_THINKING, end, GROUP_LIMIT,
1159
- HELLO, init,
1156
+ EMOJI_THINKING,
1157
+ GROUP_LIMIT,
1158
+ HELLO,
1159
+ MESSAGE_LENGTH_LIMIT,
1160
+ MESSAGE_SOFT_LIMIT,
1161
+ PRIVATE_LIMIT,
1162
+ end,
1163
+ init,
1160
1164
  lines,
1161
1165
  lines2,
1162
- map, MESSAGE_LENGTH_LIMIT,
1163
- MESSAGE_SOFT_LIMIT, newCommand,
1166
+ newCommand,
1164
1167
  oList,
1165
- paging, PRIVATE_LIMIT, send,
1168
+ paging,
1169
+ send,
1166
1170
  sendMd,
1167
1171
  uList
1168
1172
  };
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1999.1.25",
4
+ "version": "1999.1.27",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/utilitas.mjs CHANGED
@@ -268,7 +268,9 @@ const assembleUrl = (url, componens) => {
268
268
 
269
269
  const prettyJson = (object, opt) => {
270
270
  let resp = JSON.stringify(object, opt?.replacer ?? null, ~~opt?.space || 2);
271
- opt?.code ? (resp = renderCode(resp, opt)) : (opt?.log && console.log(resp));
271
+ opt?.code ? (
272
+ resp = renderCode(resp, { ...opt || {}, md: opt?.md && 'json' })
273
+ ) : (opt?.log && console.log(resp));
272
274
  return resp;
273
275
  };
274
276
 
@@ -549,7 +551,9 @@ const renderCode = (code, options) => {
549
551
  const resp = arrCode.map(
550
552
  x => `${String(i++).padStart(bits, '0')} ${s} ${ensureString(x).replace('```', '\\`\\`\\`')}`
551
553
  );
552
- const output = options?.asArray ? resp : resp.join('\n');
554
+ const output = (
555
+ options?.md ? `\`\`\`${options.md === true ? '' : options.md}\n` : ''
556
+ ) + (options?.asArray ? resp : resp.join('\n')) + (options.md ? '\n```' : '');
553
557
  options?.log && console.log(output);
554
558
  return output;
555
559
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1999.1.25",
4
+ "version": "1999.1.27",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",