Casting types with Flows comment syntax - flowtype

I have this function:
([key, value]) => key + "=" + encodeURIComponent(value)
And get the error, that value is mixed but encodeURIComponent expects string.
I'm using Flows comment syntax, so I tried this:
([key, value /*: string */]) => key + "=" + encodeURIComponent(value)
which didn't work.

The type annotation for function params goes between the parameter and the optional default value, e.g.
var fn = (param = 45) => {};
would annotate as
var fn = (param: number = 45) => {};
So in your case [key, value] is param, so the annotation goes after that as
([key, value] /*: [string, string] */) =>

Related

Converting string to chemical formula

convert a string "C11H15NO3" into chemical formula in Nuxt Js
var string = 'C11H15NO3',
result = string
.split(/(\d+)/)
.map((s, i) => i % 2 ? `<sub>${s}</sub>` : s)
.join('');
console.log(result)
Out Put :
C11H15NO3
But I need out put like : C11H15NO3
likethis
That is just the output of console.log() because it is still just a string. If you write to the document it will load into the DOM and render as html.
var string = 'C11H15NO3',
result = string
.split(/(\d+)/)
.map((s, i) => i % 2 ? `<sub>${s}</sub>` : s)
.join('');
console.log(result)
document.write(result)

Flow $ObjMap on type parameter

Flow isn't producing the error I expect in the following. Is it a bug with Flow (probably) or do I misunderstand something about type parameters?
function test<A: Object>(
obj: A,
mapper: $ObjMap<A, <V>(V) => V => any>
) {
}
test(
{
foo: 1,
bar: '2',
},
{
foo: (x: number) => String(x),
bar: (x: number) => parseInt(x),
// ^^^^^^
// should error that this is the wrong argument type
}
)
It seems like a bug because the following produces an error, albeit in the wrong place:
const a = {foo: 1, bar: '2'}
type A = typeof a
type B = $ObjMap<A, <V>(V) => V => any>
const b: B = {
foo: x => String(x),
bar: (x: number) => parseInt(x),
}
Error:
3: type B = $ObjMap<A, <V>(V) => V => any>
^ Cannot instantiate `$ObjMap` because string [1] is incompatible with number [2] in the first argument.
References:
1: const a = {foo: 1, bar: '2'}
^ [1]
7: bar: (x: number) => parseInt(x),
^ [2]
Okay, I was able to get it to work with
declare function test<A: Object, M: $ReadOnly<$ObjMap<A, <I, O>(I) => I => O>>>(
obj: A,
mapper: M
): $ObjMap<M, <I, O>((I) => O) => O>
Not ideal because the output is only assignable to a $ReadOnly shape, but it covers all the type checking I need.

Is it possible to create a vector with references to lazy-static values?

The following code compiles:
let x = Regex::new(r"\d+").unwrap();
let y = Regex::new(r"asdf\d+").unwrap();
let regexes = vec![x, y];
But this code does not:
lazy_static! {
static ref X_PRIME: Regex = Regex::new(r"\d+").unwrap();
static ref Y_PRIME: Regex = Regex::new(r"asdf\d+").unwrap();
}
let regexes = vec![X_PRIME, Y_PRIME];
The error is:
error[E0308]: mismatched types
--> src\syntax\lex.rs:19:33
|
19 | let regexes = vec![X_PRIME, Y_PRIME];
| ^^^^^^^ expected struct `syntax::lex::lex::X_PRIME`, found struct `syntax::lex::lex::Y_PRIME`
|
= note: expected type `syntax::lex::lex::X_PRIME`
= note: found type `syntax::lex::lex::Y_PRIME`
Yes. lazy_static gives X_PRIME and Y_PRIME distinct types, but they both implement Deref<Regex>, so you could write:
let regexes = vec![&*X_PRIME, &*Y_PRIME];
// The * dereferences the values to a `Regex` type
// The & turn them back into references `&Regex`.
You could also just define another static:
lazy_static! {
static ref X_PRIME: Regex = Regex::new(r"\d+").unwrap();
static ref Y_PRIME: Regex = Regex::new(r"asdf\d+").unwrap();
static ref REGEXES: Vec<&'static Regex> = vec![&X_PRIME, &Y_PRIME];
}

Getting the value of wildcard arm

How would I get the value of the wildcard arm in a match statement?
For example:
let a = 1i;
let b = 2i;
match a.cmp(&b) {
Greater => println!("is greater"),
_ => println!("is {}", _) // error: unexpected token: `_`
}
I'm hoping for something cleaner than storing the enum being matched in a variable:
let a = 1i;
let b = 2i;
let ord = a.cmp(&b);
match ord {
Greater => println!("is greater"),
_ => println!("is {}", ord)
}
Is this what you're asking for?
let a = 1i;
let b = 2i;
match a.cmp(&b) {
Greater => println!("is greater"),
e => println!("is {}", e)
}

Unable to Use IsLike operator on first table in nhibernate

public IEnumerable<Mp_ProviderProfile> Find(string customerName = null
, string emailId = null, string providercode = null, string providercity = null)
{
var query = Session.QueryOver<Mp_ProviderProfile>()
.JoinQueryOver<User>(x => x.AccountInfo);
if (!string.IsNullOrEmpty(customerName))
query = query.And(x => x.UserName.IsLike("%" + customerName + "%"));
if (!string.IsNullOrEmpty(emailId))
query = query.And(x => x.EmailId.IsLike("%" + emailId + "%"));
return query.List();
}
if (!string.IsNullOrEmpty(ProviderCode))
MpProviderProfiles = MpProviderProfiles
.Where(x => x.ProviderCode.IsLike("%" + ProviderCode + "%"));
Unable to get property of Mp_ProviderProfile in where condition in query
First two query of method Find is working fine but I am not able to apply is IsLike operator on this query
MpProviderProfiles = MpProviderProfiles
.Where(x => x.ProviderCode.IsLike("%" + ProviderCode + "%"));
One solution would be to split QueryOver definition into 2 pieces:
var query = Session.QueryOver<Mp_ProviderProfile>();
var userQuery = query.JoinQueryOver<User>(x => x.AccountInfo);
Now, we do have access to both parts and we can query their tables like this
// Mp_ProviderProfile
query.Where(x => x.ProviderCode.IsLike("%" + ProviderCode + "%"));
// User
userQuery.And(x => x.UserName.IsLike("%" + customerName + "%"));
NOTE: the assignment query = query.And(... is not needed. These methods (Where(), And()) will add the Restriction into inner collection

Resources