How to combine queries in docpad? - docpad

I need to get only last week posts. I don't find examples in Documentation and for tests I use Query-Engine demo with this code:
models = [
title: 'WRONG: Future'
date: new Date("2015-05-23")
,
title: 'Correct 1'
date: new Date("2015-05-22")
,
title: 'Correct 2'
date: new Date("2015-05-20")
,
title: 'WRONG: Old'
date: new Date("2015-05-15")
]
max_date = min_date = new Date("2015-05-22");
min_date.setDate(min_date.getDate() - 7);
result = queryEngine.createCollection(models)
.findAll({
$and: {
date: {
$lte: max_date
},
date: {
$gt: min_date
}
}
}).toJSON()
return result
How to get posts in this slice?

here is the right answer:
models = [
title: 'WRONG: Future'
date: new Date("2015-05-23")
,
title: 'Correct 1'
date: new Date("2015-05-22")
,
title: 'Correct 2'
date: new Date("2015-05-20")
,
title: 'WRONG: Old'
date: new Date("2015-05-15")
]
max_date = new Date("2015-05-22");
min_date = new Date(max_date.getTime() - 7*24*60*60*1000);
result = queryEngine.createCollection(models)
.findAll({
date: {$lte: max_date, $gt: min_date }
}).toJSON()
return result

Related

How to sort object per date field using lodash? [duplicate]

This question already has answers here:
How to sort an array of timestamps using lodash in desc order
(7 answers)
Closed 3 years ago.
I have this JavaScript object:
var items = [
{
name: "order 2",
date: "2020-02-01T13:50:04.869Z",
},
{
name: "order 3",
date: "2020-03-01T13:50:04.869Z",
},
{
name: "order 1",
date: "2020-01-01T13:50:04.869Z",
},
];
And I want to get the array ["order 1", "order 2", "order 3"] where the names are taken and are ordered by date.
What I tried so far is (using lodash):
var array = _.map(
_.sortBy(items, function(el) { return el.date; }), 
function(el) { return el.name; }
);
Which is sorting the items, but my date is recognized as string and not as date time.
How do I sort this per date?
Parse the date in the _.sortBy() iteratee function, and set the iteratee of _.map() to name:
var items = [{"name":"order 2","date":"2020-02-01T13:50:04.869Z"},{"name":"order 3","date":"2020-03-01T13:50:04.869Z"},{"name":"order 1","date":"2020-01-01T13:50:04.869Z"}];
var array = _.map(
_.sortBy(items, o => Date.parse(o.date)),
'name'
);
console.log(array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
you can try with that:
var items = [
{
name: "order 2",
date: "2020-02-01T13:50:04.869Z",
},
{
name: "order 3",
date: "2020-03-01T13:50:04.869Z",
},
{
name: "order 1",
date: "2020-01-01T13:50:04.869Z",
},
];
var names = items .sort(function compare(a, b) {
var dateA = new Date(a.date);
var dateB = new Date(b.date);
return dateA - dateB;
}).map(v => v.name);
console.log(names);

Select a specific row in a table with single select

I want to implement a method that cancels moving to the next row if data has changed.
My thought was to use the on-current-change event as this provides me with the oldCurrentRow.
What event should I emit with what parameters to achieve staying on the last highlighted row.
You can find a fiddle here https://jsfiddle.net/arjanno/pdazb5kf/28/
Key is what happens here
onCancel: function () {
//Move back to oldCurrentRow
this.$Message.info(`Clicked cancel`);
}
As long as you don't set data dirty you should be able to click on any row.
When you click Dirty it should show a modal asking you if you want to lose your changes.
On cancel I want to stay on the row I was before clicking on another row.
I don't think we can know the index of the row by the data the on-current-change callback gives us. This can/should be changed since its useful, fell free to open a issue for it.
In any case what you can do is to compare the last selected row with the current data set and use the _highlight key to tell i-table which row to highlight.
Example: https://jsfiddle.net/pdazb5kf/40/
The code would be:
function rowToString(row) {
const data = ['name', 'age', 'address'].map(key => row[key]);
return JSON.stringify(data);
}
export default {
data() {
return {
columns3: [{
type: 'index',
width: 60,
align: 'center'
},
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Address',
key: 'address'
}
],
dirty: false,
modal1: false,
data1: [{
name: 'John Brown',
age: 18,
address: 'New York No. 1 Lake Park',
date: '2016-10-03'
},
{
name: 'Jim Green',
age: 24,
address: 'London No. 1 Lake Park',
date: '2016-10-01'
},
{
name: 'Joe Black',
age: 30,
address: 'Sydney No. 1 Lake Park',
date: '2016-10-02',
},
{
name: 'Jon Snow',
age: 26,
address: 'Ottawa No. 2 Lake Park',
date: '2016-10-04'
}
]
}
},
methods: {
onCurrentChange: function(currentRow, oldCurrentRow) {
this.lastIndex = rowToString(oldCurrentRow);
if (this.dirty) {
this.modal1 = true;
}
},
onCancel: function() {
//Move back to oldCurrentRow
this.$Message.info(`Clicked cancel`);
this.data1 = this.data1.map((row, i) => {
return {
...row,
_highlight: rowToString(row) === this.lastIndex
}
});
}
}
}

Dynamic Kendo grid columns from response data

I have been trying to create a Kendo grid with dynamic column values based on a date item as part of the response data.
The data I have looks like this:
[
{ Date: '01-01-2018', Name: 'Foo', Value: 1000},
{ Date: '02-01-2018', Name: 'Foo', Value: 2000},
{ Date: '03-01-2018', Name: 'Foo', Value: 3000},
{ Date: '01-01-2018', Name: 'Bar', Value: 1400},
{ Date: '02-01-2018', Name: 'Bar', Value: 2000},
{ Date: '03-01-2018', Name: 'Bar', Value: 5000}
]
My intended structure for the grid is the following:
| Name | Jan | Feb | Mar |
|------|------|------|------|
| Foo | 1000 | 2000 | 3000 |
| Bar | 1400 | 2000 | 5000 |
I had a look at https://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/various/create-with-dynamic-columns-and-data-types but it was not quite what I was trying to do and it required that I have the columns sent as part of the response.
I am working with a wrapper for GridOptions that populates the columns through a staticly defined json. Since my columns are dynamic I am having an issue with defining them there.
On top of that, I am unable to pick out the values for the date besides brute forcing through the values and storing all the unique date entries as columns. And if I have them, then how do I match them up with the correct data entry to display the correct value in the grid?
You could use the kendo ui PivotGrid component. It is built for dealing with categorical data. However, you probably would find that takes up to much real estate.
That leaves the task of manually pivoting the data yourself. A task that is relatively simple if you make the assumptions that the Date values over all the data never have a month from two different years (If there was a 01-01-2018 and 01-01-2017 they are both Jan) and the is only one row for each date/name combo. (If there were two values for a date/name you would have to decide what is done with the value? min, max, first, last, mean ?)
data =
[
{ Date: '01-01-2018', Name: 'Foo', Value: 1000},
{ Date: '02-01-2018', Name: 'Foo', Value: 2000},
{ Date: '03-01-2018', Name: 'Foo', Value: 3000},
{ Date: '01-01-2018', Name: 'Bar', Value: 1400},
{ Date: '02-01-2018', Name: 'Bar', Value: 2000},
{ Date: '03-01-2018', Name: 'Bar', Value: 5000}
];
// distinct month nums over all data
months = [];
data.forEach(function(item) {
var parts = item.Date.split('-');
var month = parts[0] - 1;
if (months.indexOf(month) == -1) months.push(month);
});
// sort and convert month num to month name (for columns)
months.sort();
months.forEach(function(monthNum,index,arr) {
arr[index] = new Date(2018,monthNum,1).toLocaleString("en-US", { month: "short" });
});
function mmddyyyyToMon(mmddyyyy) {
var parts = mmddyyyy.split("-");
var jsMonth = parts[0] - 1;
var jsDay = parts[1];
var jsYear = parts[2];
return new Date(jsYear,jsMonth,jsDay).toLocaleString("en-US", { month: "short" });
}
// helper to make sure pivot item has one field for every month observed over all data
function newPivotItem () {
var result = { Name: '' };
months.forEach(function(month) {
result[month] = undefined;
})
return result;
}
// presume data grouped by Name and ordered by month within
var pivotData = [];
var pivotItem = {};
data.forEach (function (item) {
var parts = item.Date.split("-");
var jsMonth = parts[0] - 1;
var jsDay = parts[1];
var jsYear = parts[2];
var month = new Date(jsYear,jsMonth,jsDay).toLocaleString("en-US", { month: "short" });
if (pivotItem.Name != item.Name) {
// start next group of data for a name
pivotItem = newPivotItem();
pivotData.push(pivotItem);
pivotItem.Name = item.Name;
}
// set value for month for the name
pivotItem[month] = item.Value;
})
console.log (pivotData);
I hope this helps you.
I made a dojo for you and pasted the code bellow for the future.
I used the possibility of having a callback transport for the read.
https://dojo.telerik.com/imeNOdUh/2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.221/styles/kendo.common.min.css">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.221/styles/kendo.rtl.min.css">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.221/styles/kendo.default.min.css">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.221/styles/kendo.mobile.all.min.css">
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2018.1.221/js/angular.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2018.1.221/js/jszip.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2018.1.221/js/kendo.all.min.js"></script></head>
<body>
<div id="my-grid"></div>
<script>
function fetchData(success, fail) {
success([
{ Date: '01-01-2018', Name: 'Foo', Value: 1000},
{ Date: '02-01-2018', Name: 'Foo', Value: 2000},
{ Date: '03-01-2018', Name: 'Foo', Value: 3000},
{ Date: '01-01-2018', Name: 'Bar', Value: 1400},
{ Date: '02-01-2018', Name: 'Bar', Value: 2000},
{ Date: '03-01-2018', Name: 'Bar', Value: 5000}
]);
}
var $gridElement = $('#my-grid');
$gridElement.kendoGrid({
dataSource: {
transport: {
read: function(options) {
fetchData(function(data) {
// get month names
var monthNames = data
.map(function(t) {
var monthName = kendo.format("{0:MMM}", kendo.parseDate(t.Date, 'MM-dd-yyyy'));
return monthName;
})
.reduce(function(p, t) {
if (p.indexOf(t) == -1)
p.push(t);
return p;
}, []);
// transform
var result = data.reduce(function(p, t) {
var monthName = kendo.format("{0:MMM}", kendo.parseDate(t.Date, 'MM-dd-yyyy'));
var existing = p.filter(function(t2) {
return t2.Name == t.Name;
});
if (existing.length) {
existing[0][monthName] = t.Value;
} else {
var n = {
Name: t.Name
};
monthNames.forEach(function(m) {
n[m] = 0;
});
n[monthName] = t.Value;
p.push(n);
}
return p;
}, []);
options.success(result);
});
}
}
}
});
</script>
</body>
</html>

Xamarin UI Test Android Date Picker (On Hold)

I'm working with Xamarin UI Test (1.0.0). I'm trying to write an extension method that will update the selected date.
Here is what I wrote so far. It works with iOS and Android 4.x. it does not work with Android 5.x.
public static void EnterDate(this IApp app, string datePickerStyleId, DateTime date)
{
var array = app.Query(datePickerStyleId);
if (array.Length == 0)
return;
foreach (var picker in array)
{
var newMonth = date.ToString("MMM");
var newDay = date.Day.ToString();
var newYear = date.Year.ToString();
if (app.IsAndroid())
{
app.Tap(picker.Label);
//if device OS > 5 try scrolll up
app.Repl();
app.Screenshot("Date Picker");
app.Tap("month");
app.EnterText(newMonth);
app.PressEnter();
app.Tap("day");
app.EnterText(newDay);
app.PressEnter();
app.Tap("year");
app.EnterText(newYear);
app.PressEnter();
app.ClickDone();
app.Screenshot("Page After Changing Date");
app.ClickButton("Ok");
}
else if (app.IsIOS())
{
app.Tap(picker.Id);
app.Screenshot("Date Picker");
var currentDate = picker.Text;
var split = currentDate.Split(' ');
var currentMonth = split[0];
var currentDay = split[1].Remove(split[1].Length - 1);
var currentYear = split[2];
if (!newMonth.Equals(currentMonth))
{
app.Tap(newMonth);
}
if (!newDay.Equals(currentDay))
{
app.Tap(newDay);
}
if (!newYear.Equals(currentYear))
{
app.Tap(newYear);
}
app.ClickButton("Done");
}
}
}
The problem that I'm running into is that I can't figure out how to select a certain date. App.ScrollUp()/.ScrollDown() do work to navigate to different months. Although, I haven't found to determine the current month and then pick a day. Here is the output from the tree command of the Repl() tool.
tree [[object CalabashRootView] > ... > FrameLayout] [FrameLayout] id: "content" [LinearLayout] id: "parentPanel" [FrameLayout] id: "customPanel" [FrameLayout] id: "custom" [DatePicker > LinearLayout] id: "datePicker" [LinearLayout] id: "day_picker_selector_layout"
[TextView] id: "date_picker_header" text: "Thursday"
[LinearLayout] id: "date_picker_month_day_year_layout"
[LinearLayout] id: "date_picker_month_and_day_layout", label: "August 6"
[TextView] id: "date_picker_month" text: "AUG"
[TextView] id: "date_picker_day" text: "6"
[TextView] id: "date_picker_year" text: "2015"
[AccessibleDateAnimator > ... > SimpleMonthView] id: "animator", label: "Month grid of days: August 6"
[LinearLayout] id: "buttonPanel"
[Button] id: "button2" text: "Cancel"
[Button] id: "button1" text: "OK"
Does anybody know or have a method to select the date for Xamarin UI test that will work on iOS and Android (4.x and 5.x)?

Entity Framework bug with query?

I have an array with this data (ID, Time, and Name):
var array = new[]
{
new { ID = 1, Time = DateTime.ParseExact("12:01", "HH:mm", null), Name = "Peter" },
new { ID = 2, Time = DateTime.ParseExact("12:06", "HH:mm", null), Name = "James" },
new { ID = 3, Time = DateTime.ParseExact("12:03", "HH:mm", null), Name = "Jackie" },
new { ID = 4, Time = DateTime.ParseExact("12:08", "HH:mm", null), Name = "Peter" },
new { ID = 5, Time = DateTime.ParseExact("12:05", "HH:mm", null), Name = "James" },
new { ID = 6, Time = DateTime.ParseExact("12:07", "HH:mm", null), Name = "Peter" },
};
The following statement on the array produces the correct result:
var result = array.OrderBy(x => x.Time).GroupBy(x => x.Name)
.SelectMany(x => x).ToArray();
The result:
Time: 2013/3/6 12:01:00, Name: Peter
Time: 2013/3/6 12:07:00, Name: Peter
Time: 2013/3/6 12:08:00, Name: Peter
Time: 2013/3/6 12:03:00, Name: Jackie
Time: 2013/3/6 12:05:00, Name: James
Time: 2013/3/6 12:06:00, Name: James
But when I use the same statement with EF and SQL Server, the order is wrong:
Time: 2013/3/6 12:03:00, Name: Jackie
Time: 2013/3/6 12:06:00, Name: James
Time: 2013/3/6 12:05:00, Name: James
Time: 2013/3/6 12:07:00, Name: Peter
Time: 2013/3/6 12:01:00, Name: Peter
Time: 2013/3/6 12:08:00, Name: Peter
Here is the SQL EF generates:
SELECT
[Extent2].[Id] AS [Id],
[Extent2].[Time] AS [Time],
[Extent2].[Name] AS [Name]
FROM (SELECT DISTINCT
[Extent1].[Name] AS [Name]
FROM [dbo].[testt1] AS [Extent1] ) AS [Distinct1]
INNER JOIN [dbo].[testt1] AS [Extent2]
ON ([Distinct1].[Name] = [Extent2].[Name]) OR
(([Distinct1].[Name] IS NULL) AND ([Extent2].[Name] IS NULL))
There is no order by clause.
What did I forget? Is this a bug of EF?
How to get the same result from EF as from the array?
Just like in SQL:
First group, then order:
var result = array.GroupBy(x => x.Name)
.Select(x => x.OrderBy(y => y.Time))
.SelectMany(x => x)
.ToArray();
But in your case I don't see why you would need the group at all. Every time seems to be different, so a simpler version that yields the same result (at least with your test data) would be:
var result = array.OrderBy(x => x.Name)
.ThenBy(y => y.Time)
.ToArray();

Resources