FHIR Chat · Javascript help · javascript

Stream: javascript

Topic: Javascript help


view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:03):

I need to convert this

(this.urlify = function (A) {
                var t = /[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\]/g;
                return (
                    this.options.truncate || e(this.options),
                    A.trim()
                        .replace(/\'/gi, "")
                        .replace(t, "-")
                        .replace(/-{2,}/g, "-")
                        .substring(0, this.options.truncate)
                        .replace(/^-+|-+$/gm, "")
                        .toLowerCase()
                );
            })

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:03):

to java code that does the same thing

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:03):

I'm not sure how some of the string stuff works

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:06):

I remember that the t = / sequence has some special meaning, but google doesn't seem to know how to help me find what it is. What's the /g at the end?

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:07):

global match, basically do it as many times as you can

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:08):

"abcdeabcdeab".replace(/abc/, "123")
"123deabcdeab"
"abcdeabcdeab".replace(/abc/g, "123")
"123de123deab"```

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:08):

so that's a regex at face value? It doesn't have string quotes around it ? It's the string quotes bit I have some memory of it meaning something

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:10):

and what does the return () mean? it's returning a set?

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:10):

yeah, /slash-escaped-regex/flags is the same as new Regexp("quote-escaped-regex", "flags")

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:11):

hmm I wonder how I do that in java?

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:13):

after squinting at it for a bit, i think the return (foo) is the same as return foo

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:13):

hmm, not sure, given the ',' there

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:14):

I don't know how to read that. It's used here:

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:14):

(u = l = this.urlify(n[r].textContent)), (h = 0);

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:14):

so it's used as if it just returns a string

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:15):

I think this is the same in java:

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:15):

private String urlify(String a) {
    String t = "& +$,:;=?@\"#{}|^~[`%!'<>\\]\\.\\/\\(\\)\\*\\\\";
    return a
        .replaceAll("\\'/", "")
        .replaceAll(t, "-")
        .replaceAll("-{2,}", "-")
//        .substring(0, this.options.truncate)
        .replaceAll("^-+|-+$", "")
        .toLowerCase();
  }

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:15):

it looks to me like this.options.truncate || e(this.options), is just there for side-effects

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:16):

there's also a trim() at the beginning to get rid of WS:

" \ntrim me  \n".trim()
"trim me"

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:16):

duh thanks

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:21):

"--++asdf'in quotes'& +$-x--y---z".trim()
                        .replace(/\'/gi, "")
                        .replace(t, "-")
                        .replace(/-{2,}/g, "-")
                        .substring(0, 99)
                        .replace(/^-+|-+$/gm, "")
                        .toLowerCase()
"asdfin-quotes-x-y-z"

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:21):

that should exercise it pretty well

view this post on Zulip Eric Prud'hommeaux (Feb 10 2021 at 23:22):

(though i forgot the trim)

view this post on Zulip Grahame Grieve (Feb 10 2021 at 23:32):

thanks

view this post on Zulip Jose Costa Teixeira (Mar 22 2021 at 17:27):

Hi. Quick question:
I have this code in a script somewhere:

          var feedbacklabel = 'Feedback';
          {% if feedbackEntry.parameters %}
            {% if feedbackEntry.label %}
              feedbacklabel = '{{feedbackEntry.label}}';  // The feedback label is a string as provided
            {% endif %};

Basically saying "The feedbacklabel var is "Feedback" unless the variable {{feedbackEntry.label}} is defined, in which case the feedbacklabel takes that value. In order to further secure this (e.g. avoid someone wanting to inject code), can I / should I add a toString there?

          var feedbacklabel = 'Feedback';
          {% if feedbackEntry.parameters %}
            {% if feedbackEntry.label %}
              feedbacklabel = '{{feedbackEntry.label}}.toString()';  // The feedback label is a string (and cast as such)
            {% endif %};

?

view this post on Zulip Brian Kaney (Mar 30 2021 at 20:38):

what about a ternary?

var label = feedbackEntry.label ? feedbackEntry.label : "Feedback"

Last updated: Apr 12 2022 at 19:14 UTC