utilitas 1992.4.21 → 1992.4.23

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/index.mjs CHANGED
@@ -5,6 +5,7 @@ import * as uuid from 'uuid';
5
5
  // features
6
6
  import _ from './lib/horizon.mjs'
7
7
  import * as bot from './lib/bot.mjs';
8
+ import * as boxes from './lib/boxes.mjs';
8
9
  import * as cache from './lib/cache.mjs';
9
10
  import * as callosum from './lib/callosum.mjs';
10
11
  import * as dbio from './lib/dbio.mjs';
@@ -32,15 +33,15 @@ export {
32
33
  // dependencies
33
34
  fileType, math, uuid,
34
35
  // features
35
- bot, cache, callosum, color, dbio, email, encryption, event, hal, manifest,
36
- network, sentinel, shekel, shell, shot, sms, speech, storage, tape, uoid,
37
- utilitas,
36
+ bot, boxes, cache, callosum, color, dbio, email, encryption, event, hal,
37
+ manifest, network, sentinel, shekel, shell, shot, sms, speech, storage,
38
+ tape, uoid, utilitas,
38
39
  };
39
40
 
40
41
  if (utilitas.inBrowser() && !globalThis.utilitas) {
41
42
  globalThis.utilitas = {
42
- color, encryption, event, manifest, math, shekel, shot, storage, uoid,
43
- utilitas, uuid,
43
+ boxes, color, encryption, event, manifest, math, shekel, shot, storage,
44
+ uoid, utilitas, uuid,
44
45
  };
45
46
  // top-level await workaround
46
47
  (async () => {
package/lib/boxes.mjs ADDED
@@ -0,0 +1,102 @@
1
+ // Based on: https://github.com/sindresorhus/cli-boxes
2
+ // Repackaged to ESM by: @Leask
3
+
4
+ const single = {
5
+ "topLeft": "┌",
6
+ "top": "─",
7
+ "topRight": "┐",
8
+ "right": "│",
9
+ "bottomRight": "┘",
10
+ "bottom": "─",
11
+ "bottomLeft": "└",
12
+ "left": "│"
13
+ };
14
+
15
+ const double = {
16
+ "topLeft": "╔",
17
+ "top": "═",
18
+ "topRight": "╗",
19
+ "right": "║",
20
+ "bottomRight": "╝",
21
+ "bottom": "═",
22
+ "bottomLeft": "╚",
23
+ "left": "║"
24
+ };
25
+
26
+ const round = {
27
+ "topLeft": "╭",
28
+ "top": "─",
29
+ "topRight": "╮",
30
+ "right": "│",
31
+ "bottomRight": "╯",
32
+ "bottom": "─",
33
+ "bottomLeft": "╰",
34
+ "left": "│"
35
+ };
36
+
37
+ const bold = {
38
+ "topLeft": "┏",
39
+ "top": "━",
40
+ "topRight": "┓",
41
+ "right": "┃",
42
+ "bottomRight": "┛",
43
+ "bottom": "━",
44
+ "bottomLeft": "┗",
45
+ "left": "┃"
46
+ };
47
+
48
+ const singleDouble = {
49
+ "topLeft": "╓",
50
+ "top": "─",
51
+ "topRight": "╖",
52
+ "right": "║",
53
+ "bottomRight": "╜",
54
+ "bottom": "─",
55
+ "bottomLeft": "╙",
56
+ "left": "║"
57
+ };
58
+
59
+ const doubleSingle = {
60
+ "topLeft": "╒",
61
+ "top": "═",
62
+ "topRight": "╕",
63
+ "right": "│",
64
+ "bottomRight": "╛",
65
+ "bottom": "═",
66
+ "bottomLeft": "╘",
67
+ "left": "│"
68
+ };
69
+
70
+ const classic = {
71
+ "topLeft": "+",
72
+ "top": "-",
73
+ "topRight": "+",
74
+ "right": "|",
75
+ "bottomRight": "+",
76
+ "bottom": "-",
77
+ "bottomLeft": "+",
78
+ "left": "|"
79
+ };
80
+
81
+ const arrow = {
82
+ "topLeft": "↘",
83
+ "top": "↓",
84
+ "topRight": "↙",
85
+ "right": "←",
86
+ "bottomRight": "↖",
87
+ "bottom": "↑",
88
+ "bottomLeft": "↗",
89
+ "left": "→"
90
+ };
91
+
92
+ export default round;
93
+ export {
94
+ single,
95
+ double,
96
+ round,
97
+ bold,
98
+ singleDouble,
99
+ doubleSingle,
100
+ classic,
101
+ arrow,
102
+ };
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": "1992.4.21",
4
+ "version": "1992.4.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -23,8 +23,8 @@ const manifest = {
23
23
  "devDependencies": {
24
24
  "@google-cloud/speech": "^5.4.0",
25
25
  "@google-cloud/text-to-speech": "^4.2.1",
26
- "@sentry/node": "^7.43.0",
27
- "@waylaidwanderer/chatgpt-api": "^1.30.0",
26
+ "@sentry/node": "^7.45.0",
27
+ "@waylaidwanderer/chatgpt-api": "^1.32.8",
28
28
  "browserify-fs": "^1.0.0",
29
29
  "buffer": "^6.0.3",
30
30
  "fast-geoip": "^1.1.88",
@@ -37,7 +37,7 @@ const manifest = {
37
37
  "ping": "^0.4.4",
38
38
  "telegraf": "^4.12.2",
39
39
  "telesignsdk": "^2.2.3",
40
- "twilio": "^4.8.0",
40
+ "twilio": "^4.9.0",
41
41
  "url": "github:Leask/node-url",
42
42
  "webpack-cli": "^5.0.1"
43
43
  }
package/lib/utilitas.mjs CHANGED
@@ -3,6 +3,7 @@ import { basename as _basename, dirname, join, sep } from 'path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { promisify } from 'util';
5
5
  import { validate as verifyUuid } from 'uuid';
6
+ import * as boxes from './boxes.mjs';
6
7
  import color from './color.mjs';
7
8
 
8
9
  const call = (f, ...a) => promisify(Array.isArray(f) ? f[0].bind(f[1]) : f)(...a);
@@ -38,6 +39,7 @@ const resolve = async (resp) => resp instanceof Promise ? await resp : resp;
38
39
  const assembleApiUrl = (hst, path, args) => assembleUrl(`${hst}/${path}`, args);
39
40
  const encode = (any, isBuf, e) => (isBuf ? any : Buffer.from(any)).toString(e);
40
41
  const shiftTime = (dif, base) => new Date((base ?? new Date()).getTime() + dif);
42
+ const ensureLines = (any, op) => Array.isArray(any) ? any : lineSplit(any, op);
41
43
  const modules = {};
42
44
 
43
45
  const __ = (url, r) => {
@@ -309,8 +311,13 @@ const log = (content, filename, options) => {
309
311
  return console.info.apply(null, args);
310
312
  };
311
313
 
314
+ const locate = async (rootPack) => {
315
+ await assertPath(rootPack, 'F', 'R');
316
+ return globalThis._manifest = rootPack;
317
+ };
318
+
312
319
  const which = async (any) => { // @todo: need more precise way to detect by @Leask
313
- if (!Object.isObject(any)) {
320
+ if (!Object.isObject(any = any || globalThis._manifest)) {
314
321
  any = any || import.meta.url;
315
322
  any = any.startsWith('file://') ? fileURLToPath(any) : any;
316
323
  if (!any?.endsWith?.('.json')) {
@@ -451,7 +458,7 @@ const lineSplit = (string, options) => {
451
458
 
452
459
  const renderCode = (code, options) => {
453
460
  let i = 0;
454
- const arrCode = Array.isArray(code) ? code : lineSplit(code);
461
+ const arrCode = ensureLines(code);
455
462
  const bits = String(arrCode.length).length;
456
463
  const s = options?.separator ?? '|';
457
464
  const resp = arrCode.map(
@@ -461,7 +468,7 @@ const renderCode = (code, options) => {
461
468
  };
462
469
 
463
470
  const renderText = (text, options) => {
464
- const [arrText, code, result] = [lineSplit(text), [], []];
471
+ const [arrText, code, result] = [ensureLines(text), [], []];
465
472
  arrText.map(line => {
466
473
  const codeBlock = /^```/.test(line);
467
474
  if (codeBlock && !code.length) { // start code block
@@ -479,7 +486,42 @@ const renderText = (text, options) => {
479
486
  result.push(line);
480
487
  }
481
488
  });
482
- return result.join('\n');
489
+ return options?.asArray ? result : result.join('\n');
490
+ };
491
+
492
+ const renderBox = (content, options) => {
493
+ const [minWidth, defWidth, maxWidth, output, style] = [
494
+ 10, 80, process?.stdout?.columns || 1024, [],
495
+ boxes[options?.style || 'round']
496
+ ];
497
+ let [width, title] = [~~options?.width, options?.title || ''];
498
+ assert((content = toString(content)).length, 'Content must not be empty.');
499
+ assert(style, `Style must be one of ${Object.keys(boxes).join(', ')}.`);
500
+ assert(
501
+ !width || (width >= minWidth && width <= maxWidth),
502
+ `Width must be between $ and ${maxWidth}.`
503
+ );
504
+ const innWidth = (width || (width = defWidth)) - 2;
505
+ const cntWidth = innWidth - 2;
506
+ const pushContent = cnt => output.push(
507
+ [style.left, cnt.padEnd(cntWidth, ' '), style.right].join(' ')
508
+ );
509
+ title.length > cntWidth && (title = `${title.slice(0, cntWidth - 3)}...`);
510
+ title.length && (title = ` ${title} `);
511
+ output.push(style.topLeft + title + style.top.repeat(
512
+ innWidth - title.length
513
+ ) + style.topRight);
514
+ title.length && pushContent(' ');
515
+ ensureLines(content).map(line => {
516
+ while (line.length) {
517
+ pushContent(line.slice(0, cntWidth));
518
+ line = line.slice(cntWidth);
519
+ }
520
+ });
521
+ output.push(
522
+ style.bottomLeft + style.bottom.repeat(innWidth) + style.bottomRight
523
+ );
524
+ return options?.asArray ? output : output.join('\n');
483
525
  };
484
526
 
485
527
  const range = (from, to, options) => {
@@ -641,6 +683,7 @@ export {
641
683
  ensureArray,
642
684
  ensureDate,
643
685
  ensureInt,
686
+ ensureLines,
644
687
  ensureString,
645
688
  exclude,
646
689
  extError,
@@ -671,6 +714,7 @@ export {
671
714
  isSet,
672
715
  isUndefined,
673
716
  lineSplit,
717
+ locate,
674
718
  log,
675
719
  makeStringByLength,
676
720
  mapKeys,
@@ -685,6 +729,7 @@ export {
685
729
  prettyJson,
686
730
  purgeEmoji,
687
731
  range,
732
+ renderBox,
688
733
  renderCode,
689
734
  renderText,
690
735
  resolve,
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": "1992.4.21",
4
+ "version": "1992.4.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -34,8 +34,8 @@
34
34
  "devDependencies": {
35
35
  "@google-cloud/speech": "^5.4.0",
36
36
  "@google-cloud/text-to-speech": "^4.2.1",
37
- "@sentry/node": "^7.43.0",
38
- "@waylaidwanderer/chatgpt-api": "^1.30.0",
37
+ "@sentry/node": "^7.45.0",
38
+ "@waylaidwanderer/chatgpt-api": "^1.32.8",
39
39
  "browserify-fs": "^1.0.0",
40
40
  "buffer": "^6.0.3",
41
41
  "fast-geoip": "^1.1.88",
@@ -48,7 +48,7 @@
48
48
  "ping": "^0.4.4",
49
49
  "telegraf": "^4.12.2",
50
50
  "telesignsdk": "^2.2.3",
51
- "twilio": "^4.8.0",
51
+ "twilio": "^4.9.0",
52
52
  "url": "github:Leask/node-url",
53
53
  "webpack-cli": "^5.0.1"
54
54
  }