Jest expect.stringMatching() SyntaxError: Invalid regular expression - jestjs

We have:
A function that returns random values (In our case, crypto -> aes)
What do we want to do?
Compare that this string is not equal to the past
We mean that the object in which the string is located is large enough, so we compare the string inside the object, and not separately
How do we compare?
const oldCiper = "oldRandom+String-/FromAny==Characters";
// Pay attention to the plus sign at the beginning of a new line
const newCiper = "+newRandomString-\From/Any==Charac+ters";
// We will get an error here
// SyntaxError: Invalid regular expression...
expect({cipher: oldCiper }).toEqual({cipher: expect.not.stringMatching(newCiper)})
Full Error:
SyntaxError: Invalid regular expression: /+newRandomString-\From/Any==Charac+ters/: Nothing to repeat

Solution
Use stringContaining instead of stringMatching
const oldCiper = "oldRandom+String-/FromAny==Characters";
const newCiper = "+newRandomString-\From/Any==Charac+ters";
expect({cipher: oldCiper }).toEqual({cipher: expect.not.stringContaining(newCiper)})
The first character for an error can be not only a plus, but also "W", "L", etc.

Related

Provide Decimal Point Number to .toLocaleString() in TypeScript

I am trying to form a number having a decimal separator from a string in order to be able to apply .toLocaleString() method , which cannot be applied on strings.
const initialString = 122; //number
const decimalStr = initialString.toFixed(initialString); //string
const formattedStr = decimalStr.toLocaleString('de-DE');//error,decimalStr is a string
If any conversion is to be applied to the decimalStr, the decimal digits would be lost ( i.e. decimalStr = +decimalStr or decimalStr = Number(decimalStr). => problem, need to keep decimal digits.
How can i keep the decimal points and make the .toLocaleString()
see calling value as a number?
The Number.toLocaleString method accepts a second argument called options.
You can use this argument to control the number of fraction digits. Therefore, your call would be:
const initialNumber = 122;
const formattedStr = initialNumber.toLocaleString('de-DE', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
For more details check here.
Also, notice that both toFixed and the maximumFractionDigits have limits: 100 and 20, respectively. So the example you provide fails on the second line.

Getting error while encoding str in UTF8 format

This is working code.
line = 'line'
another_line = 'new ' + line
another_line.encode('utf-8')
output
b'new line'
Now I'm trying to figure out why I'm getting the error for below code in python3 vs I'm getting concatenated string in python2 ?.
line = 'line'
'new '+line.encode('utf-8')
TypeError: Can't convert 'bytes' object to str implicitly
As the error states, Python3 will not automatically convert a byte type to a string (the + operator sees a string first so wants a string on the right as well) implicitly (automatically) so you need to tell it explicitly to do so.
line = 'line'
print('new '+str(line.encode('utf-8')))
note that this gives slightly different output.
If you want the exact same output then this works:
line = 'line'
print('new '.encode('utf-8')+line.encode('utf-8'))
From the docs
"The + (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both be sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated." and "Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side."

Parameterized query using node-postgres isn't passed between quotes

I'm using the npm module pg with Node.js, and have the following query:
query = "SELECT * FROM territories WHERE '($1, -122.26), (47.57, -122.36)'::box \
#> point(nwlat, nwlng) ORDER BY nwlat, nwlng;"
client.query(query, [lat+.05], callback);
When I run this, I get the following error:
invalid input syntax for type box: "($1, -122.26), (47.57, -122.36)"
But when I replace $1 with a decimal literal, like 47.67, it executes normally. What am I doing incorrectly?
Your problem is that this:
'$1'
doesn't have a placeholder in it, it is a string literal that just happens to contain some characters that look like a numbered placeholder. So this:
'($1, -122.26), (47.57, -122.36)'
doesn't have a placeholder either, that's just a string literal that happens to contain the characters $ and 1. Consider the difference between this:
let x = 6;
let y = 'x'; // String that contains the name of a variable.
and this:
let x = 6;
let y = x; // The actual variable itself.
in JavaScript, same idea.
You can build your box string using string concatenation:
WHERE ('(' || $1 || ', -122.26), (47.57, -122.36)')::box
but that's not very pretty. A cleaner solution would be to bypass strings and casting altogether by using the point and box functions:
WHERE box(point($1, -122.26), point(47.57, -122.36))
Extending on the answer by #mu is too short
With pg-promise query formatting you would get exactly what you expect ;)

Groovy split on period and return only first value

I have the input as
var = primarynode.domain.local
and now I Need only primarynode from it.
I was looking both split and tokenize but not able to do it in one line code. does anyone know how to do it in one line code?
Well assuming that you want to just get the first word(before . )
from the input string.
You can use the tokenize operator of the String
If you have
def var = "primarynode.domain.local"
then you can do
def firstValue = ​var.tokenize(".")[0]​
println firstValue
output
primarynode
The split method works, you just have to be aware that the argument is a regular expression and not a plain String. And since "." means "any character" in a regular expression, you'll need to escape it...
var = 'primarynode.domain.local'.split(/\./)[0]
...or use a character class (the "." is not special inside a character class)
var = 'primarynode.domain.local'.split(/[.]/)[0]

AS3: Get all substrings from a string in a specified array

This is not a duplicate because all the other questions were not in AS3.
Here is my problem: I am trying to find some substrings that are in the "storage" string, that are in another string. I need to do this because my game server is sending the client random messages that contain on of the strings in the "storage" string. The strings sent from the server will always begin with: "AA_".
My code:
private var storage:String = AA_word1:AA_word2:AA_word3:AA_example1:AA_example2";
if(test.indexOf("AA_") >= 0) {
//i dont even know if this is right...
}
}
If there is a better way to do this, please let me know!
Why not just using String.split() :
var storage:String = 'AA_word1:AA_word2:AA_word3:AA_example1:AA_example2';
var a:Array = storage.split('AA_');
// gives : ,word1:,word2:,word3:,example1:,example2
// remove the 1st ","
a.shift();
trace(a); // gives : word1:,word2:,word3:,example1:,example2
Hope that can help.
Regular Expressions are the right tool for this job:
function splitStorage(storage: String){
var re: RegExp = /AA_([\w]+):?/gi;
// Execute the regexp until it
// stops returning results.
var strings = [];
var result: String;
while(result = re.exec(storage)){
strings.push(result[1]);
}
return strings;
}
The important part of this is the regular expression itself: /AA_([\w]+):?/gi
This says find a match starting with AA_, followed by one-or-more alphanumeric characters (which we capture) ([\w]+), optionally followed by a colon.
The match is then made global and case insensitive with /gi.
If you need to capture more than just letters and numbers - like this: "AA_word1 has spaces and [special-characters]:" - then add those characters to the character set inside the capture group.
e.g. ([-,.\[\]\s\w]+) will also match hyphen, comma, full-stop, square brackets, whitespace and alphanumeric characters.
Also you could do it with just one line, with a more advanced regular expression:
var storage:String = 'AA_word1:AA_word2:AA_word3:AA_example1:AA_example2';
const a:Array = storage.match(/(?<=AA_)\w+(?=:|$)/g);
so this means: one or more word char, preceeded by "AA_" and followed by ":" or the end of string. (note that "AA_" and ":" won't be included into the resulting match)

Resources