How to replace moment.calendar() with date-fns? - momentjs

I used moment as below:
moment(new Date(dateString)).calendar();
Is there a way to implement it by date-fns?

Related

Dynamic filter with LINQ (EntityFramework)

the problem is quite simple. We have some store with many products.
For example each item has fields (and many others):
Height
Weight
Length
Name
Color
Price
We implement some page where we can filter items by range for numbers (like price, length) and string (color).
So, the problem is here:
- we need to allow people to filter items by any of criteria above (color+price, color+length+weight).
The basic approach with predefined SELECT+WHERE is too hard to main.
Is there any other option?
Thank you.
Jonathan is right. I think using if cases would be easiest and would look something like this.
var res = _dbContextorSource.table.Where(x => x.ID > 0); //something to get all of them
//or (x => x.ID >= 0 && x.ID <= 50) if you're showing 50 on a page
if(FirstFilterIsUsed){
res = res.table.Where(x => x.FirstField == FirstFilter);
}
if(SecondFilterIsUsed){
res = res.table.Where(x => x.SecondField == SecondFilter);
}
//etc
I think you could implement a more clean solution that loops through each filter. This is super pseudo but I've used solutions like this.
var filters = GetUserFilters();
foreach(Filter filter in filters){
res = res.table.Where(x => x.GetType().
GetProperty(filter.MatchingName).GetValue(x) == filter.FilterValue);
}
var result = res.ToList();
Common Solution
The most common solution is performing a big if` case. It takes some time to support all your fields but will work great at the end.
However, if you want to do it dynamically, 2 third-party libraries can help you with this
LINQ Dynamic
https://www.nuget.org/packages/System.Linq.Dynamic.Core/
The syntax required is a little bit different from C# but work great. It's the most popular library to do such a thing.
C# Eval Expression
Disclaimer: I'm the owner of the project C# Eval Expression
The library is not free, but you can do pretty much any dynamic LINQ using the same syntax as C#.
So you will be able to build a string to evaluate and the library will do the rest for you.
Here are some example using EF Classic:
https://dotnetfiddle.net/Otm0Aa
https://dotnetfiddle.net/mwTqW7

Moment without time output date type not string

var moment = require('moment');
var c = moment().toDate();
How to output date without time in this format DD-MM-YYYY
Thanks
If you want to output the date in the format DD-MM-YYYY, simply use
var moment = require('moment);
var c = moment().format("DD-MM-YYYY");
toDate() converts the moment instance into a JS date, which it doesn't sound like you want to do.
I would advise looking at the Moment documentation for questions like this, it's very detailed and easy to understand.

How to make an if statement point free

I have the following if statement:
var formatCity =
obj => R.both(has('city'), has('state'))(obj) ? appendCommaToCity(obj) : obj
I would like to make this code point free, but can not figure out a way around the if statement.
That's quite simple actually using the ifElse function - or its specialisation, when:
const formatCity = R.when(R.both(has('city'), has('state')), appendCommaToCity);

Sinon Stub/Spy Using WithArgs Not Behaving As Expected

When I specify withArgs for a sinon spy or stub, I expect the callCount to only count calls with those arguments. This doesn't seem to be happening, though.
If I run the following:
var mySpy = sinon.spy();
mySpy.withArgs("foo");
mySpy("bar");
expect(mySpy.callCount).to.be(0);
I get "expected 1 to equal 0". Am I crazy, is this a bug, or is there another way to do this?
You have to add withArgs to the assertion, too, like so:
var mySpy = sinon.spy();
mySpy.withArgs("foo");
mySpy("bar");
expect(mySpy.withArgs("foo").callCount).to.be(0);

How do I create variable paths using e4X?

I need to know how I can parse a variable path in Flex 3 & e4X. For example, I have two XML strings where the name of one element is the only difference.
<NameOfRoot>
<NameOfChild1>
<data>1</data>
</NameOfChild1>
</NameOfRoot>
<NameOfRoot>
<NameOfChild2>
<data>2</data>
</NameOfChild2>
</NameOfRoot>
Currently I am accessing variables like this:
var data1:String = NameOfRoot.*::NameOfChild1.*::data;
var data2:String = NameOfRoot.*::NameOfChild2.*::data;
I would rather make this task more abstract so that if "NameOfChild3" is introduced I do not need to update the code. For example:
var data:String = NameOfRoot.*::{variable}.*::data;
Does anyone have insights into how this can be done?
Use the child property (LiveDocs example here):
var tagName:String = "NameOfChild1";
var data:String = NameOfRoot.child(tagName).data;
That's with no namespacing--not sure whether it's necessary in your case, but I assume you'd add some *::'s?
this also works:
var data:String = NameOfRoot..data;
but if you have more than 1 data node you'll have to sort some stuff out.
It looks like the ".*." operation will work. I wonder if this is the easiest way to handle this problem.
var data:String = NameOfRoot.*.*::data;

Resources