Why this "if" does work no as I expect with regular expression in JS - process

Could somebody explain to me why in this code:
const textInputs=document.querySelectorAll(selector);
textInputs.forEach(i=>{
i.addEventListener('keypress',(e)=>{
if(e.key.match(/[^0-9]/ig)){
e.preventDefault();
}
});
});
Condition "if" does work not as I expect. I expect the e.preventDefault() work when I enter any digits but it works exactly the opposite. I mean e.preventDefault() work when I enter any other symbol but a digits

In your regex the ^ is a negation meaning the regex matches anything that is not digits. Try
if(e.key.match(/[0-9]/ig)){
e.preventDefault();
}

Related

Space character doesn't get recognized in lodash's debounce method with b-taginput in buefy?

I am using buefy's b-taginput with lodash's debounce method to fetch data from an api source during the #typing event. The issue is when I hit spacebar in the input field , inside the debounce method the input character is not recognized as an actual character.
<b-field label="Roles">
<b-taginput
:value="this.objectData.roles"
:data="filteredTags"
autocomplete
field="role"
icon="label"
placeholder="add role..."
#focus="getAsyncRole"
#typing="getAsyncRole"
#input="(newValue) => {updateValue(newValue, 'roles')}"
>
<template slot-scope="props">
<p>{{props.option.role}}</p>
</template>
<template slot="empty">There are no items</template>
</b-taginput>
</b-field>
getAsyncRole: debounce(function(name) {
console.log('inside getAsyncRole and name.length is '+name.length) // the length is 0 when i hit
spacebar but why?
if (!name.length) {
this.filteredTags = [];
return; //exits the function if length of input is zero
}
this.isFetching = true;
api
.getSearchData(this.sessionData.key,`/role/?filter={role} LIKE '%25${name}%25'`)
.then(response => {
console.log('response for getasync role is'+JSON.stringify(response))
this.filteredTags = [];
response.forEach(item => {
this.filteredTags.push(item);
});
})
.catch(error => {
this.filteredTags = [];
throw error;
})
.finally(() => {
this.isFetching = false;
});
}, 500),
The above mentioned code works if I type any alphabetic character (i.e. it give's me the possible autocomplete results based on input character). But I also want it to list out all the autocomplete results (total results) when I hit spacebar into the b-taginput. Since it doesn't recognize the space character as an actual character, name.length become zero, and then it exits the function without making the api call.
NOTE: I noticed that this issue occurs only for b-taginput. This issue does not occur in the case of <b-autocomplete>. With <b-autocomplete> if I hit spacebar then I get all the results as desired. Therefore, I think this issue is specific only to b-taginput. Please help by advising a workaround for this.
The source code indicates that #typing trims the input before emitting it. This leaves a couple options, the best one (by far) is to pre-fetch the unfiltered list. With the list in hand, you can filter exactly as the example code does, searching for the input string within the list.
(The example code works because the empty string '' emitted by typing a space is "found" in every string)
Think about this: you're debouncing the API because you're concerned about hitting it too hard. Drop the debounce and just hit it once. Worried that fetching all tags is too long to wait? Just wait once and never wait again (consider that you were willing to incur this wait on every blank input).

Checking button text matches a certain string in Nightwatch.js

I'm having a heck of a time trying to write a test where I check that text on a button matches a certain string. I tried ".valueContains", ".attributeContains" and got blank or null, and I've tried getText(), but that only seems to return an object.
I feel like it's something obvious I'm missing, so any help would be appreciated!
Based on what you have written so far in your question, I am wondering if there is there a reason you cannot use .containsText?
.waitForElementVisible('.yourclass', this.timeout)
.assert.containsText('.yourclass', 'Text of Button you expect to match')
http://nightwatchjs.org/api#assert-containsText
Without actually looking at the code its little difficult to predict whats going on. However all of the methods in selenium return a promise, so you need to wait for it to resolve.
function async getTextOfButton() {
const element = await driver.findElement(By.className('item-class'));
const text = await element.getText();
}
If you are not using async/await you could do
driver.findElement(By.className('item-class')).then(function(element) {
element.getText().then(function(text) {
console.log(text);
});
});

react-native I do not understand the sentences

I just do not know how the code works in below, mind if any one can tell me how its working this?? I do not understand especially "=>" what does this do?
React.Children.map(this.props.children, (child) => {};
This is an arrow expression. It creates a function. There's nothing in the curly braces, so no code will execute. It's part of JavaScript, not React Native-related.
For example, you could write a function named myFunction like this:
var myFunction = (name) => {
console.log("Hello, " + name);
}
See the MDN docs for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

mvc4 multiple ifs in document.ready giving syntax error

I am writing code for a view in document.ready I write the following conditions but the code is not executed and even in design time it is showing in green the errors I have given below.
This is mvc4
Please Help. thanks.
$(document).ready(function () {
#{
if(ViewBag.itemName!="" || ViewBag.itemName!=null)
{
if(ViewBag.itemName=="PORTFOLIO")
{
//showing expected expression
<text> $('#_ktPortfolio').val("#ViewBag.itemId");</text>;
}
if(ViewBag.itemName=="PHASE")
{
//showing expected expression
<text> $('#_ktPhases').val("#ViewBag.itemId");</text>;
}
} // end of if //this is showing syntax error
}
});
Here are a few things I'd change/check.
Your check for a null or empty string isn't needed.
It also looks like you've get some extra semi colons after your </text> tags.
You are also switching between ' and " for your strings in JavaScript. I'd pick ' and stick with (except when you can't). I find it helps make it clear what is run on the client and what is run on the server.
I'd also make sure you have this wrapped in a client side script tag.
$(document).ready(function () {
#{
if(ViewBag.itemName=="PORTFOLIO"){
<text> $('#_ktPortfolio').val('#ViewBag.itemId');</text> //showing expected expression
}
if(ViewBag.itemName=="PHASE"){
<text> $('#_ktPhases').val('#ViewBag.itemId');</text> //showing expected expression
}
}
});

Prevent unnecessary white space in Webstorm

Is there a way to delete unnecessary white space,two space instead of one for example, when auto formatting a JavaScript file in Webstorm
before
function test () {
return 'test' ;
}
after
function test() {
return 'test';
}
At the moment it's formatted to:
function test() {
return 'test';
}
As I can see the only problem is with the number of spaces after return. I've created a new issue for it, please star/vote. If you find other cases where formatting doesn't work as expected, feel free to report them directly to YouTrack.