Skip to content

New wildcard function for replace#546

Open
josephgarnier wants to merge 2 commits into
jlegewie:masterfrom
josephgarnier:new-wildcard-function-for-replace
Open

New wildcard function for replace#546
josephgarnier wants to merge 2 commits into
jlegewie:masterfrom
josephgarnier:new-wildcard-function-for-replace

Conversation

@josephgarnier

Copy link
Copy Markdown

Please note that the PR include my previous PR #544.

Currently for a "rename and move" action, it is not possible to specify a user-defined wildcard that use a replace function with something other than a string as replacement. For example, if I want my files to be moved into directories named after my collection names after they have been transformed according to the "every word starts with a capital letter" rule, this is not possible.

00
01

To perform the operation described above, the regex and json to be used are the following:

"2": {
    "default": {
      "field": "collectionPaths",
      "operations": [
        {
          "function": "replace",
          "regex": "(^|\\s+|_+|\\/)(\\w{1})(?![nd|r]{1})",
          "replacement": "????"
        }
      ]
    }
  }

But it is clear that it is impossible to specify what to replace the matches with. So in this PR, I propose the possibility to use as replacement "a string" (the current behavior) or a "js function". Behind the scene, this means that we won't just use string.replace with a string as parameter, but rather, string.replace with a function as parameter (see the js doc).

Thus with this PR, it is now possible to perform a complex operation on a match using this json format:

"<id>": {
    "default": {
      "field": "<a field>",
      "operations": [
        {
          "function": "replace",
          "regex": "<a regex>",
          "replacementFunction": {
            "arguments": [
              "<arg1>",
              "<arg2>",
              "...",
              "<argn>"
            ],
            "body": "<my js code>; return <my returned string>;"
          }
        }
      ]
    }

Applied to my initial example, this would give :

"2": {
    "default": {
      "field": "collectionPaths",
      "operations": [
        {
          "function": "replace",
          "regex": "(^|\\s+|_+|\\/)(\\w{1})(?![nd|r]{1})",
          "replacementFunction": {
            "arguments": [
              "match",
              "g1",
              "g2",
              "offset",
              "string"
            ],
            "body": "return g1.concat(g2.toUpperCase());"
          }
        }
      ]
    }

The field CollectionPaths can now be used in user wildcards for sub-folders renaming
Add the "replacementFunction" option, that take a javascript function, in the wildcard function "replace" for richer replacement possibilities
@raphaelhuleux

Copy link
Copy Markdown

+1

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.

2 participants