Normalizing date input with redux-form example - redux-form

My goal is to normalize date input by this format DD-MM-YYYY.
My goal this technique: technique
I am searching example how to do it in a good way.

Following exacly that example you just need to apply some regex on the field, like:
export function normalizeDate(value) {
if (!value) {
return value
}
return value
.substr(0, 9)
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/(\d{2})(\d)/, '$1/$2/$3')
.substr(0, 9)
}

Related

Filtering in Tablesorter. Unexpected behaviour

In my tablesorter I applied this addParser to the column I'm showing here in this question. And it works well, but I found an unexpected behaviour when I filter in a way.
The results without filtering will be like this next picture:
The code for the addParser is the next one:
$.tablesorter.addParser({
// set a unique id
id: 'kilogramos',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
return parseFloat(s.replace(' Kg','').replace('.',''));
},
// set type, either numeric or text
type: 'numeric'
});
If I use the ">=" it seems to apply the addParser, because I can get rid the "." and the " Kg" of and it finds the 11.689 Kg results.
But seems that if I don't use the operators like ">", or ">=", etc. the behaviour change and it needs the dot to find what you are trying to get. In the next pictures I show what I mean.
In this last picture, I don't use the operators and I doesn't find any results. Instead, it needs now the "." and also even the " Kg" it works. The next image proves that:
I just don't want to need this "." or " Kg" to be used in any case.
Any help? Thanks
I think all you're missing is a "filter-parsed" class in the header (demo)
<th class="sorter-kilogramos filter-parsed">Kg</th>

How to set default value for Dry::Validation.Params scheme?

I have next scheme
Dry::Validation.Params do
optional(:per_page).filled(:int?, lteq?: 1000)
optional(:page).filled(:int?)
end
If I pass empty hash for validation I get empty output but I want to set default values for my data.
I tried Dry::Types.default but it does not add default values in output. That's what I tried.
Dry::Validation.Params do
optional(:per_page).filled(Dry::Types['strict.integer'].default(10), lteq?: 1000)
optional(:page).filled(:int?)
end
Is it possible to do what I want?
The Dry::Validation has not this purpose.
I recommend you to use dry-initializer on your params before pass it to the validation.
You can do something like this:
optional(:per_page).filled(Types::Integer.constructor { _1 || 10 })
Or define your own fallback strategy as here https://github.com/dry-rb/dry-types/pull/410
optional(:per_page).filled(Types::Integer.constructor { |input, type| type.(input) { 10 } })

Validate Date FORMAT (not date string) using MomentJS?

I've seen that you can use an ".isValid()" function to check that a given string is in a date format:
moment('2007-05-05', 'YYYY-MM-DD', true).isValid()
But is there a way to confirm that the format is correct? For example:
'YYYY-MM-DD' should return true, but
'YYYY-MM-DDsadsadl' should return false since the characters at the end of the string aren't valid DateTime chars.
We're working on a tool that allows a user to input an existing date format, and then a second input to enter the desired format, but we need validation to ensure the string can properly parse and convert, but they aren't entering a specific date.
The application must accept any and all possible date formats.
Use the following function to validate your format.
validFormat = function(inputFormat){
var validation = moment(moment('2017-06-17').format(inputFormat), inputFormat).inspect();
if(validation.indexOf('invalid') < 0)
return true;
else
return false;
}
Do spend some time to understand this. This simply does a reverse verification using inspect(). The date 2017-06-17 can be replaced by any valid date.
This Moment Js Docs will help you identify the valid formats.
Just make a call to this function as
validFormat('YYYY MM DD')
const getIsValid = inputFormat => moment(moment().format(inputFormat), inputFormat).isValid()
Explanation:
moment().format(inputFormat) - Create a date string from the current time from that format
This is then wrapped with moment() to make that string a moment date object, defining the format to parse it with. Finally we call the isValid() property on that moment date object. This ensures we are able to both create and parse a moment with our custom format.

Time scale won't convert dates to numbers

I am having trouble with using the time scale. I have dates in the format 'YYYYMMDD', I parse these with:
parseDate = d3.time.format("%Y%m%d").parse
I set the domain to static dates using the same above function. I can see the dates in correct format in the console. But when applying the scale function x, it returns 'NaN' WAT?
It's probably something small I'm not seeing, it's driving me mad...
Code can be found here: http://bl.ocks.org/pberden/5668581
I think the problem is in the way you call d3.nest. According to the spec
The key function will be invoked for each element in the input array, and must return a string identifier that is used to assign the element to its group.
You convert the Day in your csv file to a Date but then, as you are building a map from the array using d3.nest(), the invocation of the key function turns this Date to a String by doing an implicit conversion.
To fix this I think you could try to force your line generator to turn a String into Date like so
var line = d3.svg.line()
.x(function(d) { return x(new Date(d.key)); })
.y(function(d) { return y(d.values.Value); });

How to replace every occurrence of a string in dataSetRow?

I have some very large rptdesign report definition files.
I would like to do something like in the example below:
<expression name="expression">dataSetRow["WORK_DESCRIPTION"].replace(new RegExp('&lt;', 'g'), '<');</expression>
But for any occurrence of string in any dataset in any cell in any row.
Is this possible to do in rptdesign?
Or is there other way to accomplish this task?
One way you could do this is to create a style (use predefined data style) and add a map to it. Put a script in first expression like:
importPackage(Packages.java.lang);
if( _jsContext.getContent().getValue() instanceof String ){
if( _jsContext.getContent().getValue() == "S18_1749" ){
_jsContext.getContent().setValue(_jsContext.getContent().getValue()+"--");
}
}
true;
This will always return true. Set the second expression to false, so the map never occurs. It is a bit ugl

Resources