PropTypes validate two props against each other - react-proptypes

Valid:
{
arr1: [11, 22],
arr2: ["aa", "bb"]
}
Invalid:
{
arr1: [11, 22],
arr2: ["aa"]
}
arr1 and arr2 need to be of same length.
Can PropTypes validate such rule?

Related

Dart List contains usage

i want to use list (not set) with different items.but its not working if it not constains to add item,
void main() {
List<List<int>> mylistint=[[1, 2, 5, 10],[1, 3],[1, 2, 19, 38],[1, 3],[1, 2, 5, 10],[1, 3],[1, 2, 19, 38],[1, 3]];
List<List<int>> listeddiffintlist(List<List<int>> list){
List<List<int>> returnlist=[];
for(var i=0;i<list.length;i++){
if(!returnlist.contains(list[i])){ //not working if contains it add also
returnlist.add(list[i]);
}
}
return returnlist;
}
List<List<int>> returnlist = listeddiffintlist( mylistint);
print (returnlist.toString());
//expected=> [[1, 2, 5, 10],[1, 3],[1, 2, 19, 38]]
// result=> [[1, 2, 5, 10], [1, 3], [1, 2, 19, 38], [1, 3], [1, 2, 5, 10], [1, 3], [1, 2, 19, 38], [1, 3]] more than 1 it contains [1, 3] or someone
}
contains are just using the == operator on the elements. Since lists in Dart does not implement the == operator, two lists is only equal if they are the same instance in memory. If you want to compare based on the content of the lists, you need to do that yourself or use a package for it.
In your example you can do something like this:
void main() {
List<List<int>> mylistint = [
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3],
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3]
];
List<List<int>> listeddiffintlist(List<List<int>> list) {
List<List<int>> returnlist = [];
for (var i = 0; i < list.length; i++) {
if (!returnlist.any((returnSubLists) =>
list[i].every((element) => returnSubLists.contains(element)))) {
returnlist.add(list[i]);
}
}
return returnlist;
}
List<List<int>> returnlist = listeddiffintlist(mylistint);
print(returnlist.toString()); // [[1, 2, 5, 10], [1, 3], [1, 2, 19, 38]]
}
Alternative, you can use the collection package where the ListEquality can be used:
import 'package:collection/collection.dart';
void main() {
List<List<int>> mylistint = [
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3],
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3]
];
List<List<int>> listeddiffintlist(List<List<int>> list) {
List<List<int>> returnlist = [];
for (var i = 0; i < list.length; i++) {
if (!returnlist.any(
(subList) => const ListEquality<int>().equals(subList, list[i]))) {
returnlist.add(list[i]);
}
}
return returnlist;
}
List<List<int>> returnlist = listeddiffintlist(mylistint);
print(returnlist.toString()); // [[1, 2, 5, 10], [1, 3], [1, 2, 19, 38]]
}
The last solution could also be shorten down to:
import 'package:collection/collection.dart';
void main() {
final myListInt = [
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3],
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3]
];
final returnList = myListInt.fold<List<List<int>>>([], (result, b) {
if (!result.any((a) => const ListEquality<int>().equals(a, b))) {
result.add(b);
}
return result;
}).toList();
print(returnList); // [[1, 2, 5, 10], [1, 3], [1, 2, 19, 38]]
}
Or (if you want to make a very short solution which are not that readable):
import 'package:collection/collection.dart';
void main() {
final myListInt = [
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3],
[1, 2, 5, 10],
[1, 3],
[1, 2, 19, 38],
[1, 3]
];
final returnList = myListInt.fold<List<List<int>>>(
[],
(result, b) => !result.any((a) => const ListEquality<int>().equals(a, b))
? (result..add(b))
: result).toList();
print(returnList); // [[1, 2, 5, 10], [1, 3], [1, 2, 19, 38]]
}

Return Search results metadata with model -- rails

I have the following search results (json) that I need to tie with a model (I expanded the first one to make it more readable:
{"products_matched":
{ "89": //<-- this is a product ID
{ "lines": {
"0": {
"meta_count": 6,
"metas": {
"0": [0, 4], "1": [0, 4], "2": [0, 1], "3": [1, 2], "4": [2, 3], "5": [3, 4]
}
},
"1": {
"meta_count": 5,
"metas": {
"0": [0, 4], "1": [0, 4], "2": [0, 1], "3": [1, 2], "4": [2, 3]
}
}
},
"product_score": 0.0
} ,
"82": {"lines": {"0": {"meta_count": 2, "metas": {"0": [0, 4], "1": [0, 4]}}}, "product_score": 0.55},
"60": {"lines": {"0": {"meta_count": 3, "metas": {"0": [0, 4], "1": [0, 4], "2": [3, 4]}}}, "product_score": 0.0},
"10": {"lines": {"0": {"meta_count": 2, "metas": {"0": [0, 4], "1": [0, 4]}}}, "product_score": 0.0}}
}
In rails, how can I return both the model object and the associated meta data (lines) attached to id?
What I have so far:
product_ids = results["products_matched"].keys # => ["89", "82", "60", "10"]
products = Product.where(id: product_ids) # => #<ActiveRecord::Relation [#<Product id: 82, name: ...],[...] ...>
I could try to collect / map them together under one instance variable, or I could send them separately, but that leaves the view to marry the two.
Simple solution, and I don't know if it is the best, but I just added an attr_accessor to the Product model.
attr_accessor :result
Then I loop through the products:
product_ids = results["products_matched"].keys # => ["89", "82", "60", "10"]
#products = Product.where(id: product_ids)
#products.each do |p|
p.result = results["parsers_matched"][p.id.to_s]
end

Sort array of arrays by array value into hash in ruby

I have an array of arrays
[ [1,23,true], [2,33,false],[3,44,true] ]
I'm trying to sort the array based on the [2] value of each inner array, so the outcome would be
true_values: [ [1,23,true],[3,44,true] ]
false_values: [ [2,33,false] ]
I can select the array objects but am looking for a succinct way of sorting and outputting a hash.
x = [ [1,23,true], [2,33,false],[3,44,true] ]
Since group_by keeps the order you can sort before you group by:
p x.sort_by{|x|x[1]}.group_by(&:last) #=> {true=>[[1, 23, true], [3, 44, true]], false=>[[2, 33, false]]}
and if you do not need to keep the true/false value in the hash:
p x.sort_by{|x|x[1]}.group_by(&:pop) #=> {true=>[[1, 23], [3, 44]], false=>[[2, 33]]}
Here I'm using Enumerable#group_by to group it by it true or false first, then sorting that hash by the contents of the second element of the array found in v. Lastly we call .to_h or to hash to get the format you outlined above.
[10] pry(main)> arr
=> [[1, 23, true], [2, 33, false], [3, 44, true]]
[11] pry(main)> arr.group_by { |x| x[2] }.sort_by { |_, v| v.map { |i| i[1] } }.to_h
=> {true=>[[1, 23, true], [3, 44, true]], false=>[[2, 33, false]]}
Here's one way:
arr = [[1, 44, true], [2, 33, false], [3, 23, true]]
arr.sort_by { |*_,tf| (tf && 0) || 1 }.chunk { |*_,tf| tf }.to_h
#=> {true=>[[1, 44, true], [3, 23, true]], false=>[[2, 33, false]]}
If you want ties broken by, say, arr[i][1], then:
arr.sort_by { |_,e,tf| [(tf && 0) || 1, e] }.chunk { |*_,tf| tf }.to_h
#=> {true=>[[3, 23, true], [1, 44, true]], false=>[[2, 33, false]]}
Notice that I've changed arr given in the question to make the second point.

Column in scatter chart?

this is the code:
$(function () {
// Give the points a 3D feel by adding a radial gradient
Highcharts.getOptions().colors = $.map(Highcharts.getOptions().colors, function (color) {
return {
radialGradient: {
cx: 0.4,
cy: 0.3,
r: 0.5
},
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.2).get('rgb')]
]
};
});
// Set up the chart
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
margin: 100,
type: 'scatter',
options3d: {
enabled: true,
alpha: 10,
beta: 30,
depth: 250,
viewDistance: 5,
frame: {
bottom: { size: 1, color: 'rgba(0,0,0,0.2)' },
back: { size: 1, color: 'rgba(0,0,0,0.4)' },
side: { size: 8, color: 'rgba(0,0,0,0.6)' }
}
}
},
title: {
text: 'Draggable box'
},
subtitle: {
text: 'Click and drag the plot area to rotate in space'
},
plotOptions: {
scatter: {
width: 10,
height: 10,
depth: 10
}
},
yAxis: {
reversed: true,
min: 0,
max: 10,
title: null
},
xAxis: {
min: 0,
max: 10,
gridLineWidth: 1
},
zAxis: {
min: 0,
max: 10
},
legend: {
enabled: false
},
series: [{
name: 'Reading',
colorByPoint: true,
data: [[8, 7, 9], [1, 3, 4], [4, 6, 8], [5, 7, 7], [6, 9, 6], [7, 0, 5], [2, 3, 3], [3, 9, 8], [3, 6, 5], [4, 9, 4], [2, 3, 3], [6, 9, 9], [0, 7, 0], [7, 7, 9], [7, 2, 9], [0, 6, 2], [4, 6, 7], [3, 7, 7], [0, 1, 7], [2, 8, 6], [2, 3, 7], [6, 4, 8], [3, 5, 9], [7, 9, 5], [3, 1, 7], [4, 4, 2], [3, 6, 2], [3, 1, 6], [6, 8, 5], [6, 6, 7], [4, 1, 1], [7, 2, 7], [7, 7, 0], [8, 8, 9], [9, 4, 1], [8, 3, 4], [9, 8, 9], [3, 5, 3], [0, 2, 4], [6, 0, 2], [2, 1, 3], [5, 8, 9], [2, 1, 1], [9, 7, 6], [3, 0, 2], [9, 9, 0], [3, 4, 8], [2, 6, 1], [8, 9, 2], [7, 6, 5], [6, 3, 1], [9, 3, 1], [8, 9, 3], [9, 1, 0], [3, 8, 7], [8, 0, 0], [4, 9, 7], [8, 6, 2], [4, 3, 0], [2, 3, 5], [9, 1, 4], [1, 1, 4], [6, 0, 2], [6, 1, 6], [3, 8, 8], [8, 8, 7], [5, 5, 0], [3, 9, 6], [5, 4, 3], [6, 8, 3], [0, 1, 5], [6, 7, 3], [8, 3, 2], [3, 8, 3], [2, 1, 6], [4, 6, 7], [8, 9, 9], [5, 4, 2], [6, 1, 3], [6, 9, 5], [4, 8, 2], [9, 7, 4], [5, 4, 2], [9, 6, 1], [2, 7, 3], [4, 5, 4], [6, 8, 1], [3, 4, 0], [2, 2, 6], [5, 1, 2], [9, 9, 7], [6, 9, 9], [8, 4, 3], [4, 1, 7], [6, 2, 5], [0, 4, 9], [3, 5, 9], [6, 9, 1], [1, 9, 2]]
},
{ type: "column",
depth: 250,
data: [0]
}],
plotOptions: {
series: {
pointWidth: 200
}
},
});
and this is jsfiddle:
http://jsfiddle.net/Rodrigoson6/2yfebsgn/
I want to have a chart that reminds the very 3d. I can do only with colors and gradients. It would be very important to add another properties at the "frame" (on top), because in that way i coulde use other colors and gradients...
But is impossible.
The yAxis is reversed, because in future on bottom (that with yaxis reversed is at the top) i will add an image...
But on bottom there will be nothing....
So, i would like to add a column in my scatter chart. But i want that the only column must be at the bottom e in the chart (now the column is at the top and half outside of chart)...
Is possible ?
Maybe another solution could be to add a rect ? but the problem is that my chart is scatter type and draggable.
Thanks

Margin in chart with x, y data series

In a stacked chart with a data series in the format of [x, y] there is a larger margin on the left and on the right side. I have created the following jsfiddle to with sample data:
http://jsfiddle.net/ymyrA/4/
The code of the chart is as follows:
$(function () {
var chart = new Highcharts.Chart({
chart: {"renderTo": "container", "type": "column"},
colors: ["#2f7ed8", "#0d233a", "#8bbc21", "#910000", "#1aadce", "#492970", "#f28f43", "#77a1e5", "#c42525", "#a6c96a", "#4572A7", "#AA4643", "#89A54E", "#80699B", "#3D96AE", "#DB843D", "#92A8CD", "#A47D7C", "#B5CA92"],
credits: {"enabled": false},
plotOptions: {"column": {"stacking": "normal"}},
series: [
{"name": "Muster1", "data": [
[11, 1],
[32, 1]
]},
{"name": "Muster2", "data": [
[11, 7],
[12, 4],
[14, 4],
[15, 1],
[16, 1],
[17, 4],
[18, 6],
[19, 2],
[20, 1],
[21, 1],
[22, 1],
[25, 3],
[26, 2],
[28, 1],
[29, 1],
[30, 1]
]},
{"name": "Muster 3", "data": [
[11, 2],
[13, 2],
[15, 1],
[16, 3],
[18, 5],
[19, 11],
[20, 8],
[21, 1],
[23, 3],
[24, 12],
[27, 3],
[28, 4],
[30, 3],
[31, 3],
[33, 3],
[34, 3]
]}
],
title: {"text": null},
xAxis: {"title": {"text": "Week"}},
yAxis: {"title": {"text": "Count"}}
});
});
I have tried with min and max values, categories, etc. and nothing worked out. How can I adjust the chart to be displayed in full size?
I have updated the code and the jsfiddle with the proper sorting. I was using 3.0 and not 3.05 therefore I had no errors in the console. With the latest version I have to issues remaining. One is the above mentioned large margin at the right and left side and the other one is i am unable (even with tickInterval) to have a tick per calendar week. Based on the above mentioned sample the entire chart should have the range from x=11 to x=34 with a tick on each x.
The problem is with calculated pointRange for first series - when you have only two points, for x=11 and x=31 it is assumed that minimum interval between points in that series is 20, so Highcharts will try to set interval something about 20. For such cases you need to use pointRange for example set it to 1, see: http://jsfiddle.net/ymyrA/5/

Resources