RegExp for dates Extjs 3.4 - regex

I have problem to make regExp for search panel
&& (item.get('prodAddDate') >= dateStartValue.format("Y-m-d"))
&& (item.get('prodAddDate') <= dateEndValue.format("Y-m-d"));
I'm not sure if this is task for refExp but don't have any other idea
Problem is that when I'm don't fill field with date I can't filtr data with other conditions
I was trying something like this but don't working
&& ((item.get('prodAddDate') >= dateStartValue.format("Y-m-d")) || (new RegExp(dateStartValue)).test(item.get('prodAddDate'))) &&
((item.get('prodAddDate') <= dateEndValue.format("Y-m-d")) || (new RegExp(dateEndValue)).test(item.get('prodAddDate')));

Not entirely sure what you're trying to do, but here are some pieces of information that I hope can help you to solve your problem.
Comparison operators priority
The && operator has priority over ||, which means that:
A || B && C || D
Is equivalent to:
A || (B && C) || D
Not to:
(A || B) && (C || D)
Date comparison
You can compare Date objects directly:
// example data
var d1 = new Date('2012-12-12'),
d2 = new Date('2012-12-12'),
d3 = new Date('2013-01-01');
And get the result you expect with <, >, <=, and >=:
// logical results
d1 < d3 // true
d1 < d2 // false
d2 > d3 // false
d1 <= d2 // true
d1 => d2 // true
But not with equality comparison ==:
d1 == d2 // false
// yet...
d1 <= d2 && d1 => d2 // true
Conclusion: to test if one date is before or after another one, direct comparison is OK. But, in order to test if two dates are identical, use string comparisons:
// is this the same day?
d1.format('Y-m-d') === d2.format('Y-m-d') // true
// is this the same day and hour?
d1.format('Y-m-d H:i:s') === d2.format('Y-m-d H:i:s') // true
Ext.isEmpty()
Ext.isEmpty returns true for: null, undefined and empty strings '' only.
Ext.isEmpty(null) // true
Ext.isEmpty(undefined) // true
Ext.isEmpty('') // true
Ext.isEmpty(' ') // false
Ext.isEmpty(false) // false
// etc.
That may be useful to address your empty field case:
...
&& (Ext.isEmpty(item.get('prodAddDate') || item.get('prodAddDate') >= dateStartValue)
&& (Ext.isEmpty(item.get('prodAddDate') || item.get('prodAddDate') <= dateEndValue)
Finally
What's this obsession about regex? They're useful for complex string testing/extracting, but that's all. You should probably forget about them for a while, dude ;p

Related

How can I get arround Ocaml's type error?

I have this warning and I would like to know why he wait an expression of type Unix.file_descr list * Unix.file_descr list * Unix.file_descr list
Here is the function, if you need more code, just ask me.
let rec run()=
let a = create_bloc() in
trace_bloc(a);
let b : char ref = ref 'f' in
while true do
b := 'f';
if !(a.s) = 1 then run() else
let c = get_move() in
if c = Some 'z' then b:= 'z'
else if c = Some 'q' then b:= 'q'
else if c = Some 's' then b:= 's'
else if c = Some 'd' then b:= 'd';
if !b = 'z' || !b = 's' then
if !(a.o) = 4 && !b = 'z' then begin erase_bloc(a) ;a.o := 1; trace_bloc(a); end
else if !b = 'z' && !(a.o) <> 4 then begin erase_bloc(a); a.o := !(a.o)+1; end;
if !b = 's' && !(a.o)=1 then begin erase_bloc(a);a.o := 4;trace_bloc(a);end else
if !b = 's' && !(a.o) <> 1 then begin erase_bloc(a); a.o := !(a.o)-1;trace_bloc(a);end
else if !b = 'd' || !b = 'q' then
decal(a,!b);
dep_bas(a);
Unix.select [] [] [] 0.35;
done;
;;
The Unix.file_descr list * Unix.file_descr list * Unix.file_descr list type is the type of a value that is retuned by the expression Unix.select [] [] [] 0.35;
OCaml doesn't allow you to ignore a value returned by a function that has a type other than unit. You can, however, use the ignore function to tell explicitly to the compiler that you don't need the result of the select function,
ignore (Unix.select [] [] [] 0.35);
You can also use Unix.sleepf function to implement sleeping that supports fractions of seconds, e.g., you can substitute the above expression with just,
Unix.sleepf 0.35
This will as well pause your program for 350 milliseconds.

how multiple OR's and AND evaluate

This is very simple question, but I can't get it. I have simple condition:
bool c = true || true || true && false;
Why this evaluation is true? As far as I know it evaluates like this:
true || true || true && false => true || true && false => true && false => false
But guess im wrong.
You just have to learn some few basic rules:
(a OR b) is true IF AND ONLY IF at least one of a or b is true.
(a AND b) is true IF AND ONLY IF both of a and b are true.
ORDER of Operators MATTERS: you can't just do the logic any way that you want. computer calculates the output of the a logical statement by an order. a simplified order is like this: First is Grouping (), Second is And, Third is OR.
so when you say bool c = true || true || true && false;. the computer says ok let's first calculate true && false. it is false! and then it calculates true || true || false which is true.
EDIT
Note 1: a complete list of logical operators and their precedence is heavily dependent on the language. you can refer to the documentation for that.
for C#
Note 2: the best practice is to use GROUPING like parenthesis because GROUPING is always of priority. for example it's better to say:
bool c = (true || true) || (true && false);
Think of OR || like addition and AND && as multiplication. There is a priority in there, in fact, you will sometimes see them written as such:
bool c = true + true + true + true * false
In this case, the first evaluation is true * false, then the rest of the ORs. In this particular case, the true order of evaluation of the ORs will depend on language/compiler.
If you want to force a particular order, you can always use parentheses.

What is the difference between this two condition?

What is the difference between this two conditions?. I tried to use both conditions but different output.
if(!(data1 == true || data2 == true))
{
// do something
}
And
if(data1 != true || data2 != true)
{
// do something
}
!(data1 == true || data2 == true) this condition is same as data1 != true && data2 != true
Using ! operator with == gives != and using ! operator with || will give &&.
Your 2nd condition data1 != true || data2 != true will be same as your 1 st condition If you replace || with && in 2nd condition
!(data1 == true || data2 == true)
This is equivalent of (see also De Morgan's laws):
(data1 != true && data2 != true)
Which is obviously different from
(data1 != true || data2 != true)
fist condition :
if(!(data1 == true || data2 == true))
{
// do something
}
It will false the evaluated result if the result is true and if the result is false then the result will be true.
If data1 == true is true it will not check data2 condition and make all condition false. so if data1 == true then whole condition is false. Same as data1. And if data1 or data2 is false (one is false) then the code will be executed.
Second Condition
if(data1 != true || data2 != true)
{
// do something
}
It will do the reverse of the first condition. First it will check that if data1 is not equal to true, so if data1 is false then the code will be executed.

C++ operators precedence

I am using this statement
if ((pm && pn) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
So now it is acting like this:
0 0 = 1
0 1 = 0
1 0 = 1
1 1 = 1
but I need it to work like this:
0 0 = 1
0 1 = 0
1 0 = 0
1 1 = 1
can you tell me where am I making mistake?
What you want is simply:
if (pm == pn)
You are checking if pm is true twice. You also need to check if both are the same, not whether they are both true. So,
if ((pm == pn)
^^ ^^
pm && pm
should be
pm && pn
^
The whole expression can be simplified to
pm == pn
if the variables already have bool type.
Why not try xor?
if (!(pm ^ pn)) { /*...*/ }
Or simply equal?
if (pm == pn) { /*...*/ }
if ((pm && pm) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
Because you made a typo. You meant pm && pn.
Instead just write if (pm == pn), which is equivalent along as the only semantic values are indeed true and false for both variables.
Plus, consider making your variable names clearer and more distinct.
Note that operator precedence has nothing to do with this.
Since the question's title asks about precedence, note that || has lower precedence than &&. So the two sets of inner parentheses are redundant, and the original expression is just a longer way of saying
if (pm && pm || pm == false && pn == false)
Now, fixing the obvious typo:
if (pm && pn || pm == false && pn == false)
Removing the unneeded explicit comparisons:
if (pm && pn || !pm && !pn)
And, finally, a less obvious transformation, which others have suggested:
if (pm == pn)

C++ Ternary operator logic

I'm having trouble figuring out what this if statement is doing. This is not my code so I am simply trying to understand what the Ternary operator is doing.
if((model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride &&
(model[NUMVERTS].isLogging == true ? model[NUMVERTS].value : NumVertices) == NumVertices &&
(model[PRIMCOUNT].isLogging == true ? model[PRIMCOUNT].value : primCount) == primCount &&
(model[STARTINDEX].isLogging == true ? model[STARTINDEX].value : startIndex) == startIndex)
{
First,
(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride
could be written:
(model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride
the ternary
model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride
checks to see if model[STRIDE].isLogging is true. If it is, it takes the value model[STRIDE].value. If not, it takes the value g_uiStride. This is then compared to g_uiStride.
So, if it isn't logging, then this portion is automatically true because g_uiStride is compared to itself. If it is logging, it is true if mode[STRIDE].value == g_uiStride
and
#1.
if (model[STRIDE].isLogging is true then
RESULT1 = (model[STRIDE].value == g_uiStride) else
RESULT1 = (g_uiStride == g_uiStride)
)
#2.
if (model[NUMVERTS].isLogging is true then
RESULT2 = (model[NUMVERTS].value == NumVertices) else
RESULT2 = (mVertices == NumVertices)
)
#3.
if (model[PRIMCOUNT].isLogging is true then
RESULT3 = (model[PRIMCOUNT].value == primCount) else
RESULT3 = (primCount == primCount)
}
#4.
if (model[STARTINDEX].isLogging is true then
RESULT4 = (model[STARTINDEX].value == startIndex) else
RESULT4 = (startIndex == startIndex)
)
if (RESULT1 && RESULT2 && RESULT3 && RESULT4) {
/* yay */
} else {
/* damn */
}
In general the ternary conditional operator uses a condition to choose between two alternatives:
condition ? first_alternative : second_alternative
In this case it is very unnecessarily complicated by comparing to true and one object to itself
if((model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride
This can be reduced to
if((model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride
which is also equivalent to
if (model[STRIDE].value == g_uiStride || !model[STRIDE].isLogging
telling us that either value is equal to some global value, or we don't care because we are not logging anyway.
blah = (model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride)
is the same as
if (model[STRIDE].isLogging) {
blah = model[STRIDE].value ;
} else {
blah = g_uiStride;
}
The ternary operator is as follows:
(condition) ? value_for_true_condition : value_for_false_condition
(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) first checks to see if the isLogging == true, the (condition). If the condition is true the model[STRIDE].value value is used, if not true the g_uiStride value is used.
The statement as a whole checks the values on all those members of model, but only if the member .isLogging == true. Otherwise it uses the default value. Note that this statement will always be true if all members have .isLogging variable set to false.