I want to create a Live Template for createSelector:
export const someSelector = createSelector(getThis, getThat, getSomethingElse, (this, that, somethingElse) =>
$END$
)
I can get it to work pretty well with a single argument (e.g., only getThis which then results in (this) in the arrow function args).
// template text
createSelector($someSelector$, ($variable$) => $END$)
// variables
// expression for "variable":
decapitalize(regularExpression(someSelector, "get", ""))
This works correctly with a single argument as mentioned above, and almost works correctly with multiple arguments, except for the capitalization:
createSelector(getThis, getThat, getSomethingElse, (this, That, SomethingElse) => /* $end$ */)
I tried wrapping that whole thing in camelCase but then of course the commas and spaces are gone.
The issue is clearly that I'm processing the whole string at once so the whole string is run through whatever string formatting function. There doesn't appear to be any way to treat individual instances of "get" separately.
I tried capture groups which I really thought would work:
decapitalize(regularExpression(someSelector, "get(\w+)", "$1"))
But that doesn't replace anything, it just copies the whole thing:
createSelector(getThis, getThat, (getThis, getThat) => )
Is there any way to accomplish this?
UPDATE:
I even learned Groovy script and wrote the following, which works in a groovy playground, but gets in WebStorm gets the same result as my final example above!
groovyScript("return _1.replaceAll(/get(\w+)/) { it[1].uncapitalize() };", someSelector)
This could be done with RegEx .. but Java does not seem to support \l replacement modifier (to be used as \l$1 instead of $1 in your initial regularExpression() code).
Live example (works in PCRE2, e.g. in PHP): https://regex101.com/r/6faVqC/1
Docs on replacement modifiers: https://www.regular-expressions.info/refreplacecase.html
In any case: this whole thing is handled by Java and you are passing RegEx pattern or GrovyScript code inside double quotes. Therefore any \ symbols would need to be escaped.
You need to replace get(\w+) by get(\\w+).
The following seems to work just fine for me here (where someSelector is the Live Template variable):
groovyScript("return _1.replaceAll(/get(\\w+)/) { it[1].uncapitalize() };", someSelector)
Related
I've got this function here:
my #modifiers = <command option>;
sub triple(|c(Str:D $app1!, Str:D $app2!, Str:D $key! where .chars == 1, Str:D $mod where ($mod ~~ any #modifiers) = 'command' )) {
print_template(|c);
}
sub print_template(*#values) {
...work done here...
}
The problem I'm having is if I call it without the 4th argument, with something like triple 'App1', 'App2', 'p';, the default $mod argument does not get passed on to the print_template argument.
Is there a way to accomplish this?
For full context, this is the toy program here: https://paste.debian.net/1226675/
TL;DR 1. An explanation of the problem. 2. A DRY solution. 3. The DRYest solution: a parameters-capture function.
An explanation of the problem
A call proceeds in two steps:
Construct a call, without knowing what routine will hear the call. This includes constructing a capture of the call arguments. This capture is already done and dusted, immutable, before step 2.
See what routine candidates there are that might bind to the call. Try to bind the capture to their signatures. Some parameters in a routine's signature might specify defaults in case an argument is missing.
So, given a capture c, you can't alter it to make up for any arguments that aren't in it, and Raku doesn't automatically create a new capture that pretends any arguments that aren't in it are now in it. Instead you're going to have to manually add the missing values.
A DRY solution
The string 'command' appears twice in the suggested solution in your answer.
One way to write a DRY solution is to use the whole capture, which will include all the passed arguments, and then append any parameters for which corresponding arguments were not passed. That is to say, instead of:
my \d = \(|c[0..2], c[3] // 'command');
write this:
my \d = \(|c, |($mod if not c[3]));
The DRYest solution: a parameters-capture function
Ultimately what your scenario calls for is a function which completely ignores the arguments used to call a routine and instead just creates a new capture consisting of all of a routine's parameters. Then one could just write, say:
print_template(|parameters-capture);
That strikes me as pretty non-trivial. It would mean walking a routine's parameter data. This would presumably go via something like &?ROUTINE.signature.params. But &?ROUTINE is a compile-time variable and is relative to the current routine, so how do you get to that in a function you've called from the routine whose parameters you're interested in? And even if you can get to it, how do you get from compile-time parameter data structures to the run-time values that end up bound to the parameters? It's all way past my paygrade. It's perhaps a lot easier to do this sort of guts coding than it is in, say, the Perl world, where it means C coding, but still.
OK, based on responses in IRC, this does not appear to be possible. One suggested workaround:
sub triple(|c(Str:D $app1!,
Str:D $app2!,
Str:D $key! where .chars == 1,
Str:D $mod where ($mod ~~ any #modifiers) = 'command' )) {
my \d = \(|c[0..2], c[3] // 'command');
print_template(|d);
}
Another way to get to the same overall result (and probably the way I'd go) would be to split this out into a multi and dispatch based on the number of parameters. Here's one way that could look (with validation of the shared params moved to the proto:
my #modifiers = <command option>;
proto triple(Str:D, Str:D, Str:D $ where .chars == 1, $?) {*}
multi triple(|c($, $, $, Str:D $ where any #modifiers)) { print_template |c }
multi triple(|c($, $, $)) { print_template |c, 'command' }
sub print_template(*#values) {
# work done here
}
say triple 'App1', 'App2', 'p';
We use i18next in a CMS to power its internationalization features. Since developers can build however they want with the CMS there is opportunity for them to add l10n keys that include colons, including as part of HTML, such as Find more info here.
As has been documented, with default namespace separator settings i18next will think the colon is identifying a namespace/key pair. Since the CMS uses its own namespace (so devs won't accidentally overwrite UI strings), we don't have the option to turn off namespacing completely (with nsSeparator: false).
What I'm looking for is a way for i18next to only recognize registered namespaces as namespaces. So if we tell i18next that the valid namespaces are ['ns1', 'ns2'] and it receives Title: Subtitle, that string will be treated as a key, not a namespace/key pair.
I saw the loadNamespaces method, but that looks to simply register them to the ns option on the instance. Is there a way for i18next to essentially disallow any unregistered namespace?
This is definitely a workaround, but it doesn't feel too hackey, so I was comfortable using it indefinitely. It's been several weeks, so I don't remember if I got this from somewhere else, but it's possible, for the sake of not taking the credit if undeserved.
First, I added the appendNamespaceToMissingKey: true option to the init function. This adds the default namespace to any keys that don't already have a namespace. It also includes an unknown namespace (or something it thinks is a NS) with the "missing key" for parsing.
Then I added the parseMissingKeyHandler option assigned to the following function:
function (key) {
if (key.startsWith(`${this.defaultNS[0]}:`)) {
return key.slice(this.defaultNS[0].length + 1);
} else {
return key;
}
}
Since I didn't have to hard-code the namespace I felt okay with that.
So if a "key" comes in here with the default namespace, which will include all unlocalized strings without colons (e.g., 'Some text'), that namespace is removed and the string continues on normally. Since i18next doesn't have a value for that string, the string is printed as-is.
If an unlocalized string comes in containing a colon (e.g., '🏛', i18next thinks the first part before the colon is a namespace, so the default is not applied. Therefore this colon-ized string is returned from the function the same as it entered. Again, since i18next doesn't have a value for this string, the string is printed as-is. In this case, that includes the part before the colon as well as the colon separator. We end up with the full link HTML, for example.
So in addition to the other options in place, it looks like:
i18next.init({
...otherOptions,
appendNamespaceToMissingKey: true,
parseMissingKeyHandler (key) {
// We include namespaces with unrecognized l10n keys using
// `appendNamespaceToMissingKey: true`. This passes strings containing
// colons that were never meant to be localized through to the UI.
//
// Strings that do not include colons ("Content area") are given the
// default namespace by i18next ("translation," by default). Here we
// check if the key starts with that default namespace, meaning it
// belongs to no other registered namespace, then remove that default
// namespace before passing this through to be processed and displayed.
if (key.startsWith(`${this.defaultNS[0]}:`)) {
return key.slice(this.defaultNS[0].length + 1);
} else {
return key;
}
}
});
I'm including my code comment since you may also want to include something like this to remind yourself later why you include this convoluted handler.
Why this "value" can not be written as "pricevalue" or other, otherwise input will not convert non-numeric values
In the props element, you are defining the properties your component will attach to. You can call them whatever you want. You need to be clear about a couple of things...
You define the name here in camelCase, but when you call the component in the parent markup, use kebab-case.
methods only run when they are called. If you put your formatting on the downstream side (receiving a value and displaying it), everything will be reactive and all your values should display correctly. It will all just work whenever the source value changes. So do your formatting in a computed, like this...
js
computed: {
formattedPriceValue(){
return Number.parseFloat(this.priceValue).toFixed(2)
}
}
You can also just do it inline...
markup
<input type="number" :value="Number.parseFloat(priceValue).toFixed(2)">
The value you want to emit is probably the unformatted output of Number.parseFloat #change="$emit('price-changed', Number.parseFloat(event.target.value))"
Then, you will live longer if you do your number formatting with the Number functions provided.
Also, why don't you use the new template (multi-line) strings, delimited by a backtick `. They're much cleaner than the line continuation character you're using.
ps. I love seeing the chinese (?) comments in the code. I've copied and pasted them into my code. I hope there's no swearing. Unicode rocks.
I have to write the following code (using Zend\Filter\Inflector):
$inflector = new Inflector(':string');
$inflector->setRules([
':string' => [
new StringToLower(),
new UnderscoreToSeparator(),
new DashToCamelCase(),
new UpperCaseWords(),
]
]);
As you see, it uses 4 times the new keyword, immediately instantiating classes (following Zend Filter Interface). In this case autocomplete works fine, PhpStorm easily found what I wanted typing after new.
But better notation, using factories, is using strings, instead of direct instantiation using new:
$inflector = new Inflector(':string');
$inflector->setRules([
':string' => [
'StringToLower',
'UnderscoreToSeparator',
'DashToCamelCase',
'UpperCaseWords',
]
]);
Is there a way to have autocomplete for those strings? Maybe some annotation hint or something?
Why don't you use UpperCaseWords::class? The resulting value (that will be available during runtime) will be FQN.
I'm not familiar with Zend Framework so I'm just not sure if Zend\Filter\Inflector accepts FQN or it limited to/requires class names only (it should accept FQN ... so user-made classes would also be accepted/it's expected behaviour).
The benefit: refactoring / find usages will also be supported (since this is a piece of code and not just a string).
In any case: class name completion in strings should work since 2017.1.4 (works fine here in current stable 2017.2.4).
You just invoke code completion one more time (e.g. Ctrl + Space twice (or whatever else shortcut you have there on your computer/OS for Code | Completion | Basic)) .. or just use Ctrl + Alt + Space straight away (class name completion).
Obviously, it will work if completion is invoked on the beginning of the string. If it's in the middle/end of it (e.g. "use [CLASS_NAME_EXPECTED_HERE]") -- type whole thing manually or try other completion methods (e.g. Cyclic Expand Word if such class name was already mentioned in current file).
How can I check if a riot tag has already been loaded and compiled (in-browser with script tag), in order to avoid doing it again, programmatically.
In other words, what should I use instead of doesTagExist function in my simplified code, below?
if (!doesTagExist('my-tag')) {
riot.compile('/path/to/my-tag', function() {
riot.mount('dom-node', 'my-tag');
});
} else {
riot.mount('dom-node', 'my-tag');
}
had same problem. After bit of research I think you can't get it directly. Implementation is stored inside __TAG_IMPL which is not accessible from outside. You can however access selector for all implemented tags via riot.util.tags.selectTags(), which returns comma separated list of selectors i.e. datepicker,[data-is="datepicker"].
Oneliner for convenience
riot.util.tags.selectTags().search(/(^|,)my-tag($|,)/g) >= 0
or depending on your purity inclination
riot.util.tags.selectTags().search('"my-tag"')
Note, that first version is future-proof, if riot decides to start using single commas in selector.