Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New wildcard function for replace #546

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

josephgarnier
Copy link

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());"
          }
        }
      ]
    }

@raphaelhuleux
Copy link

+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