prettier formats ternary statement weird - visual-studio-code

Prettier in vsc formats my ternary statement from this:
(req.body.remember_me)
? req.session.cookie.maxAge = 1000 * 60 * 60 * 24 * 30
: req.session.cookie.maxAge = null
to that:
req.body.remember_me
? (req.session.cookie.maxAge = 1000 * 60 * 60 * 24 * 30)
: (req.session.cookie.maxAge = null)
is there any chance to disable or change this approach? or is it good?

Related

Firestore security rules for birthdate

I would like to have a Firestore security rule for bithdate field in my users collections where user age should be >=18 and <=80. I tried the following rule but I know it is not going to work especially for ages that are close to 18 or 80. Any idea how to make this work.
let now = request.time;
let thisYear = now.year();
let thisMonth = now.month();
let thisDay = now.day();
request.resource.data.birthdate >= timestamp.date(thisYear-80,thisMonth,thisDay) &&
request.resource.data.birthdate <= timestamp.date(thisYear-18,thisMonth,thisDay)
You can achieve the same result by comparing it with the request time but in seconds. here’s one example on how this will work:
function isAbove18(date) {
return date.seconds <= request.time.seconds - 18 * 365.25 * 24 * 60 * 60; //considering leap year with 0.25 🙃
}
function isBelow80(date) {
return date.seconds >= request.time.seconds - 80 * 365.25 * 24 * 60 * 60;
}
allow read, write: if isAbove18(resource.data.birthdate) && isBelow80(resource.data.birthdate);
For more about this you can go through this docs which takes some different approaches.

How do i format time into seconds in lua?

So basically I'm confused on how I'd make it so that I can convert DD:HH:MM:SS to only seconds while taking into account the amount of numbers there are. (Sorry if I make 0 sense, you should definitely know what I mean by the example below.)
print("05:00":FormatToSeconds()) -- 5 minutes and 0 seconds
-- 300
print("10:30:15":FormatToSeconds()) -- 10 hours, 30 minutes and 15 seconds
-- 37815
print("1:00:00:00":FormatToSeconds()) -- 1 day
-- 86400
print("10:00:00:30":FormatToSeconds()) -- 10 days, 30 seconds
-- 864030
So on and so forth. I think that maybe using gmatch would work but still idk. Help would be greatly appreciated.
Edit:
So I've tried doing it with gmatch, but I don't know if this is the most fastest way of doing this (which it probably isn't), so any help would still be appreciated.
(My code)
function ConvertTimeToSeconds(Time)
local Thingy = {}
local TimeInSeconds = 0
for v in string.gmatch(Time, "%d+") do
if tonumber(string.sub(v, 1, 1)) == 0 then
table.insert(Thingy, tonumber(string.sub(v, 2, 2)))
else
table.insert(Thingy, tonumber(v))
end
end
if #Thingy == 1 then
TimeInSeconds = TimeInSeconds + Thingy[1]
elseif #Thingy == 2 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60) + Thingy[2]
elseif #Thingy == 3 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60 * 60) + (Thingy[2] * 60) + Thingy[3]
elseif #Thingy == 4 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 24 * 60 * 60) + (Thingy[2] * 60 * 60) + (Thingy[3] * 60) + Thingy[4]
end
return TimeInSeconds
end
print(ConvertTimeToSeconds("1:00:00:00"))
Don't worry about execution speed before doing any actual measurements unless you're designing a time-critical program. In any extreme situation you'd probably want to offload risky parts to a C module.
Your approach is just fine. There are parts you can clean up: you can just return the results of calculations as TimeInSeconds doesn't actually act as accumulator in your case; tonumber handles '00' just fine and it can ensure decimal integers with an argument (since 5.3).
I'd go the other way and describe factors in a table:
local Factors = {1, 60, 60 * 60, 60 * 60 * 24}
local
function ConvertTimeToSeconds(Time)
local Components = {}
for v in string.gmatch(Time, "%d+") do
table.insert(Components, 1, tonumber(v, 10))
end
if #Components > #Factors then
error("unexpected time component")
end
local TimeInSeconds = 0
for i, v in ipairs(Components) do
TimeInSeconds = TimeInSeconds + v * Factors[i]
end
return TimeInSeconds
end
Of course, both implementations have problem with pattern being naïve as it would match e.g., '00 what 10 ever 10'. To fix that, you could go another route of using string.match with e.g., '(%d+):(%d+):(%d+):(%d+)' and enforcing strict format, or matching each possible variant.
Otherwise you can go all in and use LPeg to parse the duration.
Another way would be to not use strings internally, but instead convert them into a table like {secs=10, mins=1, hours=10, days=1} and then use these tables instead - getting seconds from that representation would be straight-forward.

How to get date range between two date in Google AppMaker?

I have two datebox that capture a start date & end date. I tried do following binding to get the date range between the two date but it return a negative value
#widget.root.children.DateBox1.value - #widget.root.children.DateBox2.value
following is my form example
// Binding (option 1 - no datasource)
getValidity(#widget.root.children.StartDate.value, #widget.root.children.EndDate.value);
// Binding (option 2 - with datasource)
getValidity(#datasource.item.StartDate, #datasource.item.EndDate);
// Client script
function getValidity(start, end) {
if (start && end) {
var milliseconds = end - start;
// days: 1000ms * 60s * 60m * 24h
return milliseconds / (1000 * 60 * 60 * 24);
// years: 1000ms * 60s * 60m * 24h * 365d
// return milliseconds / (1000 * 60 * 60 * 24 * 365);
}
return 'n/a';
}

Why Cookies will not be stored if setting Timeout = 30 days in GWT?

This is very weird. Ok, the below code works fine
public void setCookie(String cookiesName, String cookiesValue){
final int COOKIE_TIMEOUT = 1000 * 60 * 60 * 24;//1 days
Date expires = new Date((new Date()).getTime() + COOKIE_TIMEOUT);
Cookies.setCookie(cookiesName, cookiesValue, expires);
}
//then
setCookie("currentLang","de");
Collection<String> cookies = Cookies.getCookieNames();
for(String cookie : cookies){
if("currentLang".equals(cookie)){
System.out.println("got currentlang");
}
}
If i run the above code then I can see output: "got currentlang"
However, if I set timeout=30 days final int COOKIE_TIMEOUT = 1000 * 60 * 60 * 24 * 30;//30 days, then nothing got printed out, so "currentLang" has not even been stored if we set 30 days?
Why is that? does Gwt prevent that to happen?
You're using integer arithmetic for a sum that overflows beyond the maximum positive value for an integer. So 1000*60*60*24*30 is negative.
You could try 1000L*60*60*24*30

Multiple 'nth' weekday of month in a single quartz cron expression

I'm trying to write a cron expression for quartz that runs hourly everyday except every second Saturday of the month.
So, by using the '#' notation, I was trying to write the expression like:
0 0 * ? * SUN-FRI,SAT#1,SAT#3,SAT#4,SAT#5
This expression is not working properly. Also, quartz is not complaining about the cron format (quartz usually complains about cron expressions when they are wrong).
So I did some other experiments today. So, today is the third Thursday of the month, I was playing around with THU#N notation, and that's what I've found so far (I changed my expression to minute to make it easier for experimenting):
0 * * ? * SUN-FRI,SAT#1,SAT#3,SAT#4,SAT#5: not triggered
0 * * ? * THU#3: triggered
0 * * ? * THU#3,THU#4: not triggered
0 * * ? * THU#2,THU#3: triggered
I know I can simply split this into 4 additional expression but in my real scenario I have tons of expressions to change and this would increase my expression list to something 5 times longer.
In a brief: Does anyone knows how to condense these:
0 0 * ? * SUN-FRI / 0 0 * ? * SAT#1 / 0 0 * ? * SAT#3 / 0 0 * ? * SAT#4 / 0 0 * ? * SAT#5
...into a single cron expression?
Note: I'm using quartz scheduler 1.5 (I know, I know... pretty outdated)