libp2r2p 0.10.14 → 0.10.15

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/README.md CHANGED
@@ -378,6 +378,29 @@ plus NIP-05 in its standard, root and custom compact spellings.
378
378
  reads the file/media metadata carried in a URL fragment
379
379
  (`#m=image/png&dim=640x480&...`).
380
380
 
381
+ `compactWhitespace(text, options?)` is also exported from `libp2r2p/nip27` as an
382
+ opt-in display helper. It removes carriage returns, collapses spaces and
383
+ tabs, removes spaces around line breaks, limits consecutive line breaks to
384
+ two, keeps the first eight line breaks (replacing subsequent runs with a
385
+ space), and trims the result. Empty strings are accepted; non-string inputs
386
+ throw `ValidationError` with code `INVALID_WHITESPACE_TEXT`. It is not
387
+ applied automatically by `extractMedia()`.
388
+
389
+ The option `maxLineBreaks` (default `8`) sets the total limit and accepts a
390
+ non-negative safe integer. Setting it to `0` replaces all line-break runs
391
+ with spaces. `consecutiveLineBreakThreshold` (default `3`) sets the minimum
392
+ run length that collapses to **two** line breaks; shorter runs are preserved.
393
+ It accepts safe integers of at least `3`. Either option accepts `Infinity`
394
+ to disable its rule. Runs are collapsed before applying the total limit,
395
+ and trimming happens last. Invalid options throw `ValidationError` with
396
+ code `INVALID_WHITESPACE_OPTIONS`.
397
+
398
+ ```js
399
+ import { compactWhitespace } from 'libp2r2p/nip27'
400
+
401
+ compactWhitespace(text, { maxLineBreaks: 12, consecutiveLineBreakThreshold: 5 })
402
+ ```
403
+
381
404
  User references (`npub`, `nprofile`, hex pubkeys and every NIP-05 spelling)
382
405
  are handled by `libp2r2p/nip27`: `decodeUserReference()` returns the decoded
383
406
  form with its canonical compact spelling, `encodeUserReference()` returns
@@ -0,0 +1,33 @@
1
+ import { ValidationError } from '../../error/index.js'
2
+
3
+ // Compacts display text, defaulting to collapsing runs of three or more line
4
+ // breaks to two and keeping eight overall. extractMedia preserves input.
5
+ export function compactWhitespace (text, options = {}) {
6
+ if (typeof text !== 'string') {
7
+ throw new ValidationError('INVALID_WHITESPACE_TEXT', { message: 'TEXT_SHOULD_BE_A_STRING' })
8
+ }
9
+
10
+ if (!options || typeof options !== 'object' || Array.isArray(options)) {
11
+ throw new ValidationError('INVALID_WHITESPACE_OPTIONS', { message: 'OPTIONS_SHOULD_BE_AN_OBJECT' })
12
+ }
13
+ const { maxLineBreaks = 8, consecutiveLineBreakThreshold = 3 } = options
14
+ if (maxLineBreaks !== Infinity && (!Number.isSafeInteger(maxLineBreaks) || maxLineBreaks < 0)) {
15
+ throw new ValidationError('INVALID_WHITESPACE_OPTIONS', { message: 'MAX_LINE_BREAKS_SHOULD_BE_A_NON_NEGATIVE_SAFE_INTEGER_OR_INFINITY' })
16
+ }
17
+ if (consecutiveLineBreakThreshold !== Infinity && (!Number.isSafeInteger(consecutiveLineBreakThreshold) || consecutiveLineBreakThreshold < 3)) {
18
+ throw new ValidationError('INVALID_WHITESPACE_OPTIONS', { message: 'CONSECUTIVE_LINE_BREAK_THRESHOLD_SHOULD_BE_A_SAFE_INTEGER_AT_LEAST_THREE_OR_INFINITY' })
19
+ }
20
+
21
+ let remainingLineBreaks = maxLineBreaks
22
+ return text
23
+ .replace(/\r/g, '')
24
+ .replace(/[\t ]+/g, ' ')
25
+ .replace(/ ?\n ?/g, '\n')
26
+ .replace(/\n+/g, lineBreaks => {
27
+ const consecutive = lineBreaks.length >= consecutiveLineBreakThreshold ? 2 : lineBreaks.length
28
+ const kept = Math.min(remainingLineBreaks, consecutive)
29
+ remainingLineBreaks -= kept
30
+ return lineBreaks.slice(0, kept) + (kept < consecutive ? ' ' : '')
31
+ })
32
+ .trim()
33
+ }
package/nip27/index.js CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  } from './helpers/user-reference.js'
15
15
 
16
16
  export { decodeUserReference, encodeUserReference, tryDecodeUserReference }
17
+ export { compactWhitespace } from './helpers/compact-whitespace.js'
17
18
 
18
19
  const BECH32_BODY = '[ac-hj-np-z02-9]'
19
20
  const BOUNDARY_PREFIX = /(?<=^|[\s"«„「¡¿:{([])/.source
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2r2p",
3
- "version": "0.10.14",
3
+ "version": "0.10.15",
4
4
  "description": "Peer-to-relay-to-peer",
5
5
  "keywords": [
6
6
  "p2r2p",