Skip to content

Add the rr object, pad() and print() to --js to close #856 - #861

Merged
mathiasrw merged 3 commits into
mainfrom
fix/856-rr
Oct 27, 2026
Merged

mathiasrw merged 3 commits into
mainfrom
fix/856-rr

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #856

Stacked on #860. In place of #857, after the review of 26 October.

Change

  • rr has the values of RexReplace with long names, directly on it:
    • the file: filePath, filePathRel, dirPath, dirPathRel, dirName, fileName, name, ext, bytes, size, mtime, ctime, mtimeObj, ctimeObj
    • the numbers: fileNumber ($n), filesTotal ($t), matchedFileNumber ($i), changedFileNumber ($c), matchThisFile ($m), matchUntilNow ($M)
    • text, find, stdin (the piped data), cwd, time, timeObj
    • the groups: rr[0], rr[1] and rr.year, which overwrite a name they share
  • print() and printErr(), also as rr.print() and rr.printErr(), print to stdout and stderr with a line break, also with --quiet-total. print() without rr runs once per file, so it can print once for a file.
  • pad(value, width = 3, char = '0'), like pad($1) for 007.
  • file_path and file_path_rel, the same as file and file_rel, with their space cousins.
  • Code that names rr runs once per match, as it can reach a group in a way the search of the code can not see, like rr[name]. rr is found as a whole word, so {...rr} and const r = rr count too, and brr does not.

rr is a plain object: every value is its own, so {...rr}, Object.keys(rr) and JSON.stringify(rr) have them all. It is only made when the code names rr, and the values of the file are made once per file. 20 000 matches: $1.toUpperCase() takes 205.5 ms, and rr[1].toUpperCase() 205.6 ms.

Tests

test/fix/856.test.js is new, and 15 of its 17 tests fail without the change. It covers the groups by number and by name, the long names of a file and of the numbers over three files, text, find, cwd, time, stdin, a group that overwrites rr.name, a spread of rr, the run per match, that brr.n is not rr, print, rr.print, printErr and rr.printErr with --quiet-total, pad(), and file_path. The help and the README tell all of it. yarn test-ci passes.

From the code review of #855

The README row of --js says that under the flag n only the code has no $1, while a replacement text still reads $1 as the first named group. It is here, as this pull request changes that row too.

@mathiasrw
mathiasrw merged commit 7c29f62 into main Oct 27, 2026
6 checks passed
@mathiasrw
mathiasrw deleted the fix/856-rr branch October 27, 2026 16:01
mathiasrw added a commit that referenced this pull request Oct 27, 2026
* Add Regex+ engines, with rr as the default engine

* Make regex+ the Regex+ engine with v, x and n, add regex+n and regex+x, and drop regex+u

* Add --flags, keep only regex+ with v, x and n, and name the groups of --js $year

* Check --flags against the flag options in the CLI, take v for regex+ by default, and warn on $1 under n

* Name the unknown flags of --flags, and check the messages of the stopped runs in the tests

* Take regex from npm and copy only its subroutines, drop V8, JSC, V and the $1 warning, and fix [(?>]\1

* Run --js code that runs once per file at its first match

* Add the rr object with long names, print(), printErr(), pad() and file_path to --js

* Build RexReplace as an ES module, and the engine also as CommonJS

* Say in src/libs that regex is imported, not bundled

* Load yargs as CommonJS so the help breaks at words, and use the require of the CommonJS build, not the path of the build

* Let a group named like a cousin win in any order, and say that only --js code has no $1 under n

* Leave the README row of --js to #861, which changes it too

* Add the rr object with long names, print(), printErr(), pad() and file_path to --js

* Tell that functions have no _ cousin, and that rr in a string or a comment also runs the code per match

* Say in the README that only --js code has no $1 under n

* Show the \\n in the help of --status as text, and bring the README row of --debug in line with its help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add the rr object, pad() and print() to --js

1 participant