-
Notifications
You must be signed in to change notification settings - Fork 8
Выборки по предикатам из задания 3 при помощи наследования #12
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
Open
Zhigalov
wants to merge
4
commits into
cripi-javascript:master
Choose a base branch
from
Zhigalov:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * Возвращает объект Collection | ||
| * | ||
| * @param {Array} items Элементы списка | ||
| * | ||
| * @example Collection([]); | ||
| */ | ||
| var Collection = function (items) { | ||
| "use strict"; | ||
|
|
||
| var i; | ||
| this.items = [] | ||
| for (i = 0; i < items.length; i++) { | ||
| this.items[i] = items[i].clone(); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Добавление элемента в коллекцию | ||
| * | ||
| * @return {Collection} | ||
| */ | ||
| Collection.prototype.add = function (model) { | ||
| "use strict"; | ||
|
|
||
| var result = new this.constructor(this.items); | ||
| result.items.push(model); | ||
| return result; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {Function} selector | ||
| * | ||
| * @see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter | ||
| * | ||
| * @example | ||
| * new Collection([]).filter(function (item) { | ||
| * return item.get('attendee').indexOf("me") !== -1; | ||
| * }); | ||
| * @return {Collection} | ||
| */ | ||
| Collection.prototype.filter = function (selector) { | ||
| "use strict"; | ||
|
|
||
| return new this.constructor(this.items.filter(selector)); | ||
| }; | ||
|
|
||
| /** | ||
| * @param {String} fieldName | ||
| * | ||
| * @see http://javascript.ru/Array/sort | ||
| * | ||
| * @example | ||
| * new Collection([]).sortBy("raiting"); | ||
| * @return {Collection} | ||
| */ | ||
| Collection.prototype.sortBy = function (fieldName) { | ||
| "use strict"; | ||
|
|
||
| var result = new this.constructor(this.items); | ||
| result.items.sort(function (first, second) { | ||
| if (first.get(fieldName) < second.get(fieldName)) { | ||
| return -1; | ||
| } | ||
| if (first.get(fieldName) > second.get(fieldName)) { | ||
| return 1; | ||
| } | ||
| return 0; | ||
| }); | ||
| return result; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| var Model = function (data) { | ||
| "use strict"; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {Object} attributes | ||
| * | ||
| * @example | ||
| * item.set({title: "March 20", content: "In his eyes she eclipses..."}); | ||
| */ | ||
| Model.prototype.set = function (attributes) { | ||
| "use strict"; | ||
| var keyName; | ||
| for (keyName in attributes) { | ||
| this[keyName] = attributes[keyName]; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @param {String} attribute | ||
| */ | ||
| Model.prototype.get = function (attribute) { | ||
| "use strict"; | ||
| return this[attribute]; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {Object} attributes | ||
| */ | ||
| Model.prototype.validate = function (attributes) { | ||
| "use strict"; | ||
| throw new Error('this is Abstract method'); | ||
| }; | ||
|
|
||
| Model.prototype.clone = function () { | ||
| var attr, temp = new this.constructor(); | ||
| for (attr in this) { | ||
| temp[attr] = clone(this.get(attr)); | ||
| } | ||
| return temp; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| function clone(obj) { | ||
| if (null == obj || "object" != typeof obj) { | ||
| return obj; | ||
| } | ||
|
|
||
| if (obj instanceof Date) { | ||
| var copy = new Date(); | ||
| copy.setTime(obj.getTime()); | ||
| return copy; | ||
| } | ||
|
|
||
| if (obj instanceof Array) { | ||
| var i, copy = []; | ||
| for (i = 0; i < obj.length; ++i) { | ||
| copy[i] = clone(obj[i]); | ||
| } | ||
| return copy; | ||
| } | ||
|
|
||
| if (obj instanceof Object) { | ||
| var copy = {}; | ||
| for (var attr in obj) { | ||
| if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); | ||
| } | ||
| return copy; | ||
| } | ||
|
|
||
| throw new Error("Unable to copy obj! Its type isn't supported."); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /** | ||
| * Возвращает объект 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) | ||
| * | ||
| * @see EventTime | ||
| */ | ||
|
|
||
| var Event = function (name, address, time, member, raiting) { | ||
| "use strict"; | ||
|
|
||
| Model.call(this); | ||
| var myTime = time || new EventTime(new Date(), 60); | ||
|
|
||
| this.set({ | ||
| name: name || "Событие", | ||
| address: address || "", | ||
| timeStart: myTime.start, | ||
| timeLength: myTime.length, | ||
| member: member || [], | ||
| raiting: +raiting || 3 | ||
| }); | ||
| } | ||
|
|
||
| inherits(Event, Model); | ||
| Event.prototype.constructor = Event; | ||
|
|
||
|
|
||
| /** | ||
| * Возвращает объект 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 | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Валидация собития | ||
| * | ||
| * @return {Array} Список ошибок | ||
| */ | ||
| Event.prototype.validate = function () { | ||
| "use strict"; | ||
|
|
||
| var errors = []; | ||
| if (this.get("timeLength") < 0) { | ||
| errors.push("Продолжительность события меньше допустимой величины"); | ||
| } | ||
| if (this.get("raiting") < 0) { | ||
| errors.puch("Рэйтинг собития меньше допустимой величины"); | ||
| } | ||
| else if (this.get("raiting") > 5) { | ||
| errors.push("Рэйтинг события больше допустимой величины"); | ||
| } | ||
| return errors; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /** | ||
| * @return {Object} Список событий | ||
| */ | ||
| var Events = function (items) { | ||
| "use strict"; | ||
|
|
||
| Collection.call(this, items); | ||
| }; | ||
|
|
||
| inherits(Events, Collection); | ||
| Events.prototype.constructor = Events; | ||
|
|
||
| /** | ||
| * Прошедшие события | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.findPastEvents = function () { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (event) { | ||
| return event.get("timeStart") < new Date().getTime(); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Предстоящие события | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.findFutureEvents = function () { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (event) { | ||
| return event.get("timeStart") > new Date().getTime(); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * События с участием персоны с именем "name" | ||
| * | ||
| * @param personName Имя персоны | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.findEventsWithPerson = function (personName) { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (event) { | ||
| return event.get("member").some(function (member) { | ||
| return member === personName; | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * События без участия персоны с именем "name" | ||
| * | ||
| * @param personName Имя персоны | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.findEventsWithoutPerson = function (personName) { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (event) { | ||
| return event.get("member").every(function (member) { | ||
| return member != personName; | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * События, которые произойдут после указанного времени | ||
| * | ||
| * @param time Временной отсчет | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.findEventsHappendLaterTime = function (time) { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (event) { | ||
| return event.get("timeStart") > time; | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * События, которые произойдут до указанного времени | ||
| * | ||
| * @param time Временной отсчет | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.findEventsHappendBeforeTime = function (time) { | ||
| "use strict"; | ||
|
|
||
| return this.filter(function (event) { | ||
| return event.get("timeStart") < time; | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Сортировка по времени начала события | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.sortEventsByDate = function () { | ||
| "use strict"; | ||
|
|
||
| return this.sortBy("timeStart"); | ||
| }; | ||
|
|
||
| /** | ||
| * Сортировка по рэтингу события | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.sortEventsByRaiting = function () { | ||
| "use strict"; | ||
|
|
||
| return this.sortBy("raiting"); | ||
| }; | ||
|
|
||
| /** | ||
| * Сортировка по имени события | ||
| * | ||
| * @return {Events} | ||
| */ | ||
| Events.prototype.sortEventsByName = function () { | ||
| "use strict"; | ||
|
|
||
| return this.sortBy("name"); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Табы + пробелы?