Stream: javascript
Topic: Javascript help
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()
);
})
Grahame Grieve (Feb 10 2021 at 23:03):
to java code that does the same thing
Grahame Grieve (Feb 10 2021 at 23:03):
I'm not sure how some of the string stuff works
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?
Eric Prud'hommeaux (Feb 10 2021 at 23:07):
global match, basically do it as many times as you can
Eric Prud'hommeaux (Feb 10 2021 at 23:08):
"abcdeabcdeab".replace(/abc/, "123")
"123deabcdeab"
"abcdeabcdeab".replace(/abc/g, "123")
"123de123deab"```
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
Grahame Grieve (Feb 10 2021 at 23:10):
and what does the return () mean? it's returning a set?
Eric Prud'hommeaux (Feb 10 2021 at 23:10):
yeah, /slash-escaped-regex/flags
is the same as new Regexp("quote-escaped-regex", "flags")
Grahame Grieve (Feb 10 2021 at 23:11):
hmm I wonder how I do that in java?
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
Eric Prud'hommeaux (Feb 10 2021 at 23:13):
hmm, not sure, given the ',' there
Grahame Grieve (Feb 10 2021 at 23:14):
I don't know how to read that. It's used here:
Grahame Grieve (Feb 10 2021 at 23:14):
(u = l = this.urlify(n[r].textContent)), (h = 0);
Grahame Grieve (Feb 10 2021 at 23:14):
so it's used as if it just returns a string
Grahame Grieve (Feb 10 2021 at 23:15):
I think this is the same in java:
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();
}
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
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"
Grahame Grieve (Feb 10 2021 at 23:16):
duh thanks
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"
Eric Prud'hommeaux (Feb 10 2021 at 23:21):
that should exercise it pretty well
Eric Prud'hommeaux (Feb 10 2021 at 23:22):
(though i forgot the trim)
Grahame Grieve (Feb 10 2021 at 23:32):
thanks
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 %};
?
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