{"action":"created","comment":{"url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/comments/42946330","id":42946330,"diff_hunk":"@@ -1,52 +1,194 @@\n 'use strict';\n \n-var phoneBook; // Здесь вы храните записи как хотите\n+String.prototype.repeat = function (num) {\n+    return new Array(isNaN(num) ? 1 : ++num).join(this);\n+};\n \n-/*\n-   Функция добавления записи в телефонную книгу.\n-   На вход может прийти что угодно, будьте осторожны.\n-*/\n-module.exports.add = function add(name, phone, email) {\n+var phoneBook = {};\n \n-    // Ваша невероятная магия здесь\n+/**\n+ * Очистка от пробелов и приведение в нижний регистр\n+ * поля email\n+ * @param email\n+ * @returns {string}\n+ */\n+function cleanName(email) {\n+    if (typeof (String.prototype.trim) === undefined) {\n+        String.prototype.trim = function () {\n+            return String(this).replace(/^\\s+|\\s+$/g, '');\n+        };\n+    }\n+    return email.toLowerCase().trim();\n+}\n \n-};\n+/**\n+ * Очистка поля телефон от скобок, + и пробелов\n+ * @param phone\n+ * @returns {string|XML}\n+ */\n+function cleanPhone(phone) {\n+    phone = phone.replace(/\\s+/g, '').replace(/\\-+/g, '');\n+    return phone.replace(/\\++/g, '').replace(/\\(+/g, '').replace(/\\)+/g, '');\n+}\n \n-/*\n-   Функция поиска записи в телефонную книгу.\n-   Поиск ведется по всем полям.\n-*/\n-module.exports.find = function find(query) {\n+/**\n+ * Добавление новой записи в книгу\n+ * @param name Имя\n+ * @param phone Телефон\n+ * @param email Email\n+ * @returns {*} Результат операции\n+ */\n+module.exports.add = function add(name, phone, email) {\n+    var emailRe = /^(([^<>()[\\]\\.,;:\\s@\\\"]+(\\.[^<>()[\\]\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))@(([^<>()[\\]\\.,;:\\s@\\\"]+\\.)+[^<>()[\\]\\.,;:\\s@\\\"]{2,})$/i;\n+    var phoneRe = /^\\+?\\d*?[- ]?(\\({1}\\d{3}\\){1}|\\d{3})[- ]?\\d{3}[- ]?\\d?[- ]?\\d{3}$/;\n+    var cleanedEamil = cleanName(email);\n+    var toAdd = {name: name, phone: phone, clean_phone: cleanPhone(phone), email: cleanedEamil};\n+    if (emailRe.test(email) && phoneRe.test(phone)) {\n+        /* Раз в примере у людей повторяются email (что старнно),\n+         то за ключ в словаре берем и email и имя\n+         */\n+        phoneBook[[cleanedEamil, name].join('_')] = toAdd;\n+        return {status: 200, descr: 'added', data: toAdd};\n+    } else {\n+        return {status: 500, descr: 'error with data validation', data: toAdd};\n+    }\n+};\n \n-    // Ваша удивительная магия здесь\n+/**\n+ * Поиск по телефонной книге\n+ * @param query\n+ * @returns {{}} часть объекта phoneBook\n+ */\n+var search = function (query) {\n+    var pbKeys = Object.keys(phoneBook);\n+    var searchResult = {};\n+    for (var pbKey in phoneBook) {\n+        if (phoneBook.hasOwnProperty(pbKey)) {\n+            var toSearchIn = '';\n+            var toSearchInDict = phoneBook[pbKey];\n+            var toSearchInKeys = Object.keys(toSearchInDict);\n+            for (var field in toSearchInDict) {\n+                toSearchIn = toSearchIn.concat(toSearchInDict[field]);\n+            }\n+            if (toSearchIn.indexOf(query) != -1) {\n+                searchResult[pbKey] = phoneBook[pbKey];\n+            }\n+        }\n+    }\n+    return searchResult;\n+};\n \n+/**\n+ * Функция поиска записи в телефонной книге.\n+ * Поиск ведется по всем полям.\n+ * @param query\n+ * @returns {{}} словарь с результатами\n+ */\n+module.exports.find = function find(query) {\n+    if (query == '') {\n+        return phoneBook;\n+    }\n+    var searchResult = search(query);\n+    /* Странно, конечно, в этой функции что-то выводить в консоль,\n+     но в задании описанно именно так\n+     */\n+    for (var key in searchResult) {\n+        console.log([searchResult[key]['name'],\n+                     searchResult[key]['phone'],\n+                     searchResult[key]['email']].join(', '));\n+    }\n+    return searchResult;\n };\n \n-/*\n-   Функция удаления записи в телефонной книге.\n-*/\n+/**\n+ * Функция удаления записи в телефонной книге.\n+ * @param query\n+ * @returns {{deleted: Number, deleted_data}}\n+ */\n module.exports.remove = function remove(query) {\n-\n-    // Ваша необьяснимая магия здесь\n-\n+    var searchResult = search(query);\n+    for (var key in searchResult) {\n+        delete phoneBook[key];\n+    }\n+    // И опять выводим в консоль\n+    console.log(['Deleted', Object.keys(searchResult).length, 'contacts'].join(' '));\n+    return {deleted: Object.keys(searchResult).length, deleted_data: searchResult};\n };\n \n-/*\n-   Функция импорта записей из файла (задача со звёздочкой!).\n-*/\n+/**\n+ * Функция импорта записей из файла (задача со звёздочкой!).\n+ * @param filename\n+ * @returns {Array} Все добавленные записи\n+ */\n module.exports.importFromCsv = function importFromCsv(filename) {\n     var data = require('fs').readFileSync(filename, 'utf-8');\n+    data = data.replace(/\\n+$/g, '').split('\\n');\n+    var addedData = [];\n+    data.forEach(function (record, i, data) {","path":"phoneBook.js","position":150,"original_position":150,"commit_id":"cfeb2855e499705075a80804d23182c29c00abf1","original_commit_id":"cfeb2855e499705075a80804d23182c29c00abf1","user":{"login":"danmir","id":1074336,"avatar_url":"https://avatars.githubusercontent.com/u/1074336?v=3","gravatar_id":"","url":"https://api.github.com/users/danmir","html_url":"https://github.com/danmir","followers_url":"https://api.github.com/users/danmir/followers","following_url":"https://api.github.com/users/danmir/following{/other_user}","gists_url":"https://api.github.com/users/danmir/gists{/gist_id}","starred_url":"https://api.github.com/users/danmir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danmir/subscriptions","organizations_url":"https://api.github.com/users/danmir/orgs","repos_url":"https://api.github.com/users/danmir/repos","events_url":"https://api.github.com/users/danmir/events{/privacy}","received_events_url":"https://api.github.com/users/danmir/received_events","type":"User","site_admin":false},"body":"Почистил","created_at":"2015-10-25T15:57:37Z","updated_at":"2015-10-25T15:57:37Z","html_url":"https://github.com/urfu-2015/javascript-tasks-2/pull/24#discussion_r42946330","pull_request_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24","_links":{"self":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/comments/42946330"},"html":{"href":"https://github.com/urfu-2015/javascript-tasks-2/pull/24#discussion_r42946330"},"pull_request":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24"}}},"pull_request":{"url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24","id":47352911,"html_url":"https://github.com/urfu-2015/javascript-tasks-2/pull/24","diff_url":"https://github.com/urfu-2015/javascript-tasks-2/pull/24.diff","patch_url":"https://github.com/urfu-2015/javascript-tasks-2/pull/24.patch","issue_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues/24","number":24,"state":"open","locked":false,"title":"Миронов Данил","user":{"login":"danmir","id":1074336,"avatar_url":"https://avatars.githubusercontent.com/u/1074336?v=3","gravatar_id":"","url":"https://api.github.com/users/danmir","html_url":"https://github.com/danmir","followers_url":"https://api.github.com/users/danmir/followers","following_url":"https://api.github.com/users/danmir/following{/other_user}","gists_url":"https://api.github.com/users/danmir/gists{/gist_id}","starred_url":"https://api.github.com/users/danmir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danmir/subscriptions","organizations_url":"https://api.github.com/users/danmir/orgs","repos_url":"https://api.github.com/users/danmir/repos","events_url":"https://api.github.com/users/danmir/events{/privacy}","received_events_url":"https://api.github.com/users/danmir/received_events","type":"User","site_admin":false},"body":"Как разбить regexp на несколько строк пока не придумал. Если оборачивать в строку, тогда становится не regexp.","created_at":"2015-10-10T20:07:28Z","updated_at":"2015-10-25T15:57:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5e98e2d38def670f021df17b6f2a6241ad377a0d","assignee":{"login":"evilj0e","id":914429,"avatar_url":"https://avatars.githubusercontent.com/u/914429?v=3","gravatar_id":"","url":"https://api.github.com/users/evilj0e","html_url":"https://github.com/evilj0e","followers_url":"https://api.github.com/users/evilj0e/followers","following_url":"https://api.github.com/users/evilj0e/following{/other_user}","gists_url":"https://api.github.com/users/evilj0e/gists{/gist_id}","starred_url":"https://api.github.com/users/evilj0e/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evilj0e/subscriptions","organizations_url":"https://api.github.com/users/evilj0e/orgs","repos_url":"https://api.github.com/users/evilj0e/repos","events_url":"https://api.github.com/users/evilj0e/events{/privacy}","received_events_url":"https://api.github.com/users/evilj0e/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24/commits","review_comments_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24/comments","review_comment_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues/24/comments","statuses_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/statuses/cfeb2855e499705075a80804d23182c29c00abf1","head":{"label":"danmir:master","ref":"master","sha":"cfeb2855e499705075a80804d23182c29c00abf1","user":{"login":"danmir","id":1074336,"avatar_url":"https://avatars.githubusercontent.com/u/1074336?v=3","gravatar_id":"","url":"https://api.github.com/users/danmir","html_url":"https://github.com/danmir","followers_url":"https://api.github.com/users/danmir/followers","following_url":"https://api.github.com/users/danmir/following{/other_user}","gists_url":"https://api.github.com/users/danmir/gists{/gist_id}","starred_url":"https://api.github.com/users/danmir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danmir/subscriptions","organizations_url":"https://api.github.com/users/danmir/orgs","repos_url":"https://api.github.com/users/danmir/repos","events_url":"https://api.github.com/users/danmir/events{/privacy}","received_events_url":"https://api.github.com/users/danmir/received_events","type":"User","site_admin":false},"repo":{"id":43954686,"name":"javascript-tasks-2","full_name":"danmir/javascript-tasks-2","owner":{"login":"danmir","id":1074336,"avatar_url":"https://avatars.githubusercontent.com/u/1074336?v=3","gravatar_id":"","url":"https://api.github.com/users/danmir","html_url":"https://github.com/danmir","followers_url":"https://api.github.com/users/danmir/followers","following_url":"https://api.github.com/users/danmir/following{/other_user}","gists_url":"https://api.github.com/users/danmir/gists{/gist_id}","starred_url":"https://api.github.com/users/danmir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danmir/subscriptions","organizations_url":"https://api.github.com/users/danmir/orgs","repos_url":"https://api.github.com/users/danmir/repos","events_url":"https://api.github.com/users/danmir/events{/privacy}","received_events_url":"https://api.github.com/users/danmir/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/danmir/javascript-tasks-2","description":"Задача к лекции «Основы Javascript» – «Телефонная книга»","fork":true,"url":"https://api.github.com/repos/danmir/javascript-tasks-2","forks_url":"https://api.github.com/repos/danmir/javascript-tasks-2/forks","keys_url":"https://api.github.com/repos/danmir/javascript-tasks-2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/danmir/javascript-tasks-2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/danmir/javascript-tasks-2/teams","hooks_url":"https://api.github.com/repos/danmir/javascript-tasks-2/hooks","issue_events_url":"https://api.github.com/repos/danmir/javascript-tasks-2/issues/events{/number}","events_url":"https://api.github.com/repos/danmir/javascript-tasks-2/events","assignees_url":"https://api.github.com/repos/danmir/javascript-tasks-2/assignees{/user}","branches_url":"https://api.github.com/repos/danmir/javascript-tasks-2/branches{/branch}","tags_url":"https://api.github.com/repos/danmir/javascript-tasks-2/tags","blobs_url":"https://api.github.com/repos/danmir/javascript-tasks-2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/danmir/javascript-tasks-2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/danmir/javascript-tasks-2/git/refs{/sha}","trees_url":"https://api.github.com/repos/danmir/javascript-tasks-2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/danmir/javascript-tasks-2/statuses/{sha}","languages_url":"https://api.github.com/repos/danmir/javascript-tasks-2/languages","stargazers_url":"https://api.github.com/repos/danmir/javascript-tasks-2/stargazers","contributors_url":"https://api.github.com/repos/danmir/javascript-tasks-2/contributors","subscribers_url":"https://api.github.com/repos/danmir/javascript-tasks-2/subscribers","subscription_url":"https://api.github.com/repos/danmir/javascript-tasks-2/subscription","commits_url":"https://api.github.com/repos/danmir/javascript-tasks-2/commits{/sha}","git_commits_url":"https://api.github.com/repos/danmir/javascript-tasks-2/git/commits{/sha}","comments_url":"https://api.github.com/repos/danmir/javascript-tasks-2/comments{/number}","issue_comment_url":"https://api.github.com/repos/danmir/javascript-tasks-2/issues/comments{/number}","contents_url":"https://api.github.com/repos/danmir/javascript-tasks-2/contents/{+path}","compare_url":"https://api.github.com/repos/danmir/javascript-tasks-2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/danmir/javascript-tasks-2/merges","archive_url":"https://api.github.com/repos/danmir/javascript-tasks-2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/danmir/javascript-tasks-2/downloads","issues_url":"https://api.github.com/repos/danmir/javascript-tasks-2/issues{/number}","pulls_url":"https://api.github.com/repos/danmir/javascript-tasks-2/pulls{/number}","milestones_url":"https://api.github.com/repos/danmir/javascript-tasks-2/milestones{/number}","notifications_url":"https://api.github.com/repos/danmir/javascript-tasks-2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/danmir/javascript-tasks-2/labels{/name}","releases_url":"https://api.github.com/repos/danmir/javascript-tasks-2/releases{/id}","created_at":"2015-10-09T12:41:59Z","updated_at":"2015-10-09T12:42:00Z","pushed_at":"2015-10-10T20:05:53Z","git_url":"git://github.com/danmir/javascript-tasks-2.git","ssh_url":"git@github.com:danmir/javascript-tasks-2.git","clone_url":"https://github.com/danmir/javascript-tasks-2.git","svn_url":"https://github.com/danmir/javascript-tasks-2","homepage":"","size":95,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"urfu-2015:master","ref":"master","sha":"8964185d6a3c6c2b9c54b4480137e874d6fbd374","user":{"login":"urfu-2015","id":14800971,"avatar_url":"https://avatars.githubusercontent.com/u/14800971?v=3","gravatar_id":"","url":"https://api.github.com/users/urfu-2015","html_url":"https://github.com/urfu-2015","followers_url":"https://api.github.com/users/urfu-2015/followers","following_url":"https://api.github.com/users/urfu-2015/following{/other_user}","gists_url":"https://api.github.com/users/urfu-2015/gists{/gist_id}","starred_url":"https://api.github.com/users/urfu-2015/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/urfu-2015/subscriptions","organizations_url":"https://api.github.com/users/urfu-2015/orgs","repos_url":"https://api.github.com/users/urfu-2015/repos","events_url":"https://api.github.com/users/urfu-2015/events{/privacy}","received_events_url":"https://api.github.com/users/urfu-2015/received_events","type":"Organization","site_admin":false},"repo":{"id":43743710,"name":"javascript-tasks-2","full_name":"urfu-2015/javascript-tasks-2","owner":{"login":"urfu-2015","id":14800971,"avatar_url":"https://avatars.githubusercontent.com/u/14800971?v=3","gravatar_id":"","url":"https://api.github.com/users/urfu-2015","html_url":"https://github.com/urfu-2015","followers_url":"https://api.github.com/users/urfu-2015/followers","following_url":"https://api.github.com/users/urfu-2015/following{/other_user}","gists_url":"https://api.github.com/users/urfu-2015/gists{/gist_id}","starred_url":"https://api.github.com/users/urfu-2015/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/urfu-2015/subscriptions","organizations_url":"https://api.github.com/users/urfu-2015/orgs","repos_url":"https://api.github.com/users/urfu-2015/repos","events_url":"https://api.github.com/users/urfu-2015/events{/privacy}","received_events_url":"https://api.github.com/users/urfu-2015/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/urfu-2015/javascript-tasks-2","description":"Задача к лекции «Основы Javascript» – «Телефонная книга»","fork":false,"url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2","forks_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/forks","keys_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/teams","hooks_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/hooks","issue_events_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues/events{/number}","events_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/events","assignees_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/assignees{/user}","branches_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/branches{/branch}","tags_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/tags","blobs_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/git/refs{/sha}","trees_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/statuses/{sha}","languages_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/languages","stargazers_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/stargazers","contributors_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/contributors","subscribers_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/subscribers","subscription_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/subscription","commits_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/commits{/sha}","git_commits_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/git/commits{/sha}","comments_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/comments{/number}","issue_comment_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues/comments{/number}","contents_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/contents/{+path}","compare_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/merges","archive_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/downloads","issues_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues{/number}","pulls_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls{/number}","milestones_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/milestones{/number}","notifications_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/labels{/name}","releases_url":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/releases{/id}","created_at":"2015-10-06T10:22:05Z","updated_at":"2015-10-10T17:56:49Z","pushed_at":"2015-10-25T12:12:59Z","git_url":"git://github.com/urfu-2015/javascript-tasks-2.git","ssh_url":"git@github.com:urfu-2015/javascript-tasks-2.git","clone_url":"https://github.com/urfu-2015/javascript-tasks-2.git","svn_url":"https://github.com/urfu-2015/javascript-tasks-2","homepage":"","size":1613,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":98,"mirror_url":null,"open_issues_count":25,"forks":98,"open_issues":25,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24"},"html":{"href":"https://github.com/urfu-2015/javascript-tasks-2/pull/24"},"issue":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues/24"},"comments":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/issues/24/comments"},"review_comments":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24/comments"},"review_comment":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/pulls/24/commits"},"statuses":{"href":"https://api.github.com/repos/urfu-2015/javascript-tasks-2/statuses/cfeb2855e499705075a80804d23182c29c00abf1"}}}}