Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Выборки по всем предикатам, которые представлены в тексте дз #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions ComplexFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Все предстоящие события с моим участием отсортированные по рейтигу
*/
function NewEventWithMeSortByRaiting() {
"use strict";
events
.filter(NewEvent)
.filter(EventWithPerson, {name: "я"})
.sort(SortByRaiting)
.forEach(PrintEvent);
}

/**
* Все события, которые произойдут на этой неделе, отсортированые по рейтингу
*/
function ThisWeekSortedByRaiting() {
"use strict";
var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
var timeToEndOfWeek = (7 - now.getDay()) * 24 * 60 * 60 * 1000;
var endOfWeek = new Date(today + timeToEndOfWeek);
events
.filter(NewEvent)
.filter(BeforeTimeEvent, { time: endOfWeek })
.sort(SortByRaiting)
.forEach(PrintEvent);
};

/**
* Выбрать ближайшее (по времени) событие без моего участия
*/
function FirstEventWithoutMe() {
"use strict";
var filteredEvents = events
.filter(NewEvent)
.filter(EventWithoutPerson, { name: "я" })
.sort(SortByDate);
if (filteredEvents.length != 0)
PrintEvent(filteredEvents[0]);
};
74 changes: 74 additions & 0 deletions Event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Возвращает объект Event
*
* @param {String} [name = "Событие"] Имя события
* @param {String} [address = ""] Адресс события
* @param {Object} time Дата события
* @param {Array} member Участники
* @param {Number} [raiting=3] Важность события (по шкале от 0 до 5)
*
* @example
* Event(
* "Совещание", "Екатеринбург, ул. Тургенева, д. 4, ауд. 150",
* EventTime(new Date(2011, 10, 10, 14, 48, 00), 60),
* ["Жигалов Сергей Алексеевич"], 5)
*
* @return {Object}
* @see EventTime
*/

function Event(name, address, time, member, raiting) {
"use strict";
return {
"name": name || "Событие",
"address": address.toString(),
"time": time,
"member": member || [],
"raiting": +raiting || 3
};
}


/**
* Возвращает объект EventTime
*
* @private
* @param {Number|Date} start Начало события
* @param {Number} [length=0] Длительность события в минутрах
*
* @example
* EventTime(new Date(2011, 10, 10, 14, 48, 00), 60)
*
* @return {Object}
*/
function EventTime(start, length) {
"use strict";
return {
"start": +start,
"length": +length || 0
};
}

var events = [
Event("jsy6on0kz4", "7429kpz7nh", EventTime(new Date(2012, 10, 8), 45), ["Иванов", "я", "Петров"]), // 1
Event("l8zklfh2r5", "1ygb121nee", EventTime(new Date(2012, 11, 22), 45), ["Иванов", "я"]), // 2
Event("weoua5w3pf", "icu620fqpo", EventTime(new Date(2012, 11, 3), 45), ["Иванов"]), // 3
Event("5zs4x4aij9", "4x9pkek4dp", EventTime(new Date(2012, 9, 10), 45), ["Иванов", "я", "Петров"]), // 4
Event("s1m5vkm5mv", "5v6hifv041", EventTime(new Date(2012, 11, 25)), ["Иванов", "я"]), // 5
Event("3bdg49u3ez", "zwvyu91w3b", EventTime(new Date(2012, 10, 10), 45), ["Иванов", "Петров"]), // 6
Event("efc7dr20dx", "u641l89gw7", EventTime(new Date(2012, 9, 9)), ["Иванов", "я"]), // 7
Event("pqlv9jglxq", "nkb24jd7u6", EventTime(new Date(2012, 10, 29)), ["Петров"]), // 8
Event("iei2z6c63b", "kgzdrcs4mk", EventTime(new Date(2012, 11, 9)), ["я", "Петров"]), // 9
Event("kygauxe4ub", "br0vzaikwr", EventTime(new Date(2012, 11, 27), 45), ["Иванов", "я"], 5), // 10
Event("ke4vd03xp8", "9k3bbogzz5", EventTime(new Date(2012, 11, 3), 45), ["Иванов"]), // 11
Event("jt4kew1ayg", "4q51wfulmd", EventTime(new Date(2012, 11, 29), 45), ["Иванов", "Петров"]), // 12
Event("dg9q72wmw9", "c5r5t5z024", EventTime(new Date(2012, 11, 8), 45), ["Иванов"]), // 13
Event("v4873m9tgp", "l0rdqy22d3", EventTime(new Date(2012, 10, 25), 45), ["Иванов", "я"]), // 14
Event("rrb6bjyubc", "0oxvmk59p4", EventTime(new Date(2012, 10, 23)), ["Иванов", "я"]), // 15
Event("0oqqd4j655", "rb1gc65t8d", EventTime(new Date(2012, 10, 11), 45), ["Иванов", "Петров"]), // 16
Event("f7b16x9cmy", "ncxmyqnhp1", EventTime(new Date(2012, 11, 4)), ["Иванов", "я"]), // 17
Event("ik3xyzqoky", "3cguxe6l6n", EventTime(new Date(2012, 11, 16)), ["Петров"]), // 18
Event("u4mfqatngu", "a7vnsmnpb0", EventTime(new Date(2012, 9, 24)), ["я"]), // 19
Event("s53m0j6u9j", "bk9y4szr2s", EventTime(new Date(2012, 9, 7), 45), ["Иванов", "я"], 5), // 20
Event("lkajbmx7y2", "vwzpu1xl0o", EventTime(new Date(2012, 9, 20), 45), ["Иванов", "я", "Петров"], 1)//21
];
127 changes: 127 additions & 0 deletions SelectionFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

/**
* Прошедшее событие
*
* @param {Object} event Событие
*
* @return {Boolean}
*/
function OldEvent(event) {
"use strict";
return event.time.start < new Date().getTime();
}

/**
* Предстоящее событие
*
* @param {Object} event Событие
*
* @return {Boolean}
*/
function NewEvent(event) {
"use strict";
return event.time.start >= new Date().getTime();
}

/**
* Событие, в котором участвует человек с именем "name"
*
* @param {Object} event Событие
*
* @return {Boolean}
*/
function EventWithPerson(event) {
"use strict";
var name = this.name;
return event.member.some(function (member) {
return member === name;
});
}

/**
* Событие, в котором не участвует человек с именем "name"
*
* @param {Object} event Событие
*
* @return {Boolean}
*/
function EventWithoutPerson(event) {
"use strict";
var name = this.name;
return event.member.every(function (member) {
return member !== name;
});
}

/**
* События, которые произойдут через время, указанное в time
*
* @param {Object} event Событие
*
* @return {Boolean}
*/
function LaterTimeEvent(event) {
"use strict";
return event.time.start >= this.time;
}

/**
* События, которые произойдут до времени, указанного в time
*
* @param {Object} event Событие
*
* @return {Boolean}
*/
function BeforeTimeEvent(event) {
"use strict";
return event.time.start < this.time;
}

/**
* Сортировка по времени события
*
* @param {Object} first Событие
* @param {Object} second Событие
*
* @return {Int}
*/
function SortByDate(first, second) {
"use strict";
if (first.time.start < second.time.start) {
return -1;
}
if (first.time.start > second.time.start) {
return 1;
}
return 0;
}

/**
* Сортировка по рейтингу события
*
* @param {Object} first Событие
* @param {Object} second Событие
*
* @return {Int}
*/
function SortByRaiting(first, second) {
"use strict";
if (first.raiting < second.raiting) {
return 1;
}
if (first.raiting > second.raiting) {
return -1;
}
return 0;
}

/**
* Напечатать событие
*
* @param {Object} event Событие
*
*/
function PrintEvent(event) {
"use strict";
console.log(event.name + " " + event.address + "; Begin in \"" + new Date(event.time.start) + "\". " + event.member.length + " member(s). Raiting: " + event.raiting);
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Выборки</title>
<script language="javascript" type="text/jscript" src="Event.js"></script>
<script language="javascript" type="text/jscript" src="SelectionFunctions.js"></script>
<script language="javascript" type="text/jscript" src="ComplexFunctions.js"></script>
</head>
<body>
<p>Выбрать все предстоящие события с моим участием отсортированные по рейтигу<button onclick="NewEventWithMeSortByRaiting();">Go</button></p>
<p>Выбрать все события, которые произойдут на этой неделе, отсортированые по рейтингу<button onclick="ThisWeekSortedByRaiting();">Go</button></p>
<p>Выбрать ближайшее (по времени) событие без моего участия<button onclick="FirstEventWithoutMe();">Go</button></p>
</body>
</html>