From 6583edffb4e324ffbf514f6822b20e21a42a006b Mon Sep 17 00:00:00 2001 From: valentin Date: Sun, 12 Sep 2021 00:19:45 +0900 Subject: [PATCH 1/2] GetContactStats Facebook --- .../Facebook/getters/getContactsStatistics.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts diff --git a/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts b/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts new file mode 100644 index 00000000..652b9107 --- /dev/null +++ b/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts @@ -0,0 +1,50 @@ +import Facebook from '../Facebook' +import { StatisticType } from '../../../../../types/schemas/Statistic' + +// 3 noms et le nombre d'occurence + +function getFirstName(fullName : string) +{ + return fullName.split(' ')[0] +} + +Facebook.prototype.getContactsStatistics = async function getContactsStatistics() { + + const contactsData = await this.getContacts({ + parsingOptions: { + pagination: { + offset: 0, + items: Infinity, + }, + }, + }) + + if (!contactsData) + return null + + var contactMap = new Map() + contactsData.data.forEach((entry) => { + const firstName : string = entry.displayName === undefined ? "" : getFirstName(entry.displayName); + if(contactMap.has(firstName)) + contactMap.set(firstName, contactMap.get(firstName) +1); + else + contactMap.set(firstName, 1); + }) + + const contactMapSorted = new Map([...contactMap.entries()].sort((a, b) => b[1] - a[1])); + + let keys = Array.from(contactMapSorted.keys()).slice(0, 3); + let values = Array.from(contactMapSorted.values()).slice(0, 3); + const mapContactsLimited = keys.map((el, i) => ({ firstName: el, value: values[i] })); + + return { + statistics: [ + { + type: StatisticType.RANKING, + value: mapContactsLimited, + name: 'Contacts with the same first name.', + }, + ], + parsedFiles: contactsData?.parsedFiles ?? [], + } +} From 7c26842a7918d8704f8d3e42af12658eb4a85977 Mon Sep 17 00:00:00 2001 From: valentin Date: Sun, 12 Sep 2021 00:26:52 +0900 Subject: [PATCH 2/2] fix coding style --- .../Facebook/getters/getContactsStatistics.ts | 75 +++++++++---------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts b/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts index 652b9107..ffa496c8 100644 --- a/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts +++ b/src/classes/Standardizer/plugins/Facebook/getters/getContactsStatistics.ts @@ -3,48 +3,43 @@ import { StatisticType } from '../../../../../types/schemas/Statistic' // 3 noms et le nombre d'occurence -function getFirstName(fullName : string) -{ - return fullName.split(' ')[0] +function getFirstName(fullName : string) { + return fullName.split(' ')[0]; } Facebook.prototype.getContactsStatistics = async function getContactsStatistics() { - - const contactsData = await this.getContacts({ - parsingOptions: { - pagination: { - offset: 0, - items: Infinity, - }, + const contactsData = await this.getContacts({ + parsingOptions: { + pagination: { + offset: 0, + items: Infinity, + }, + }, + }) + + if (!contactsData) { return null } + + const contactMap = new Map() + contactsData.data.forEach((entry) => { + const firstName : string = entry.displayName === undefined ? '' : getFirstName(entry.displayName); + if (contactMap.has(firstName)) contactMap.set(firstName, contactMap.get(firstName) + 1); + else contactMap.set(firstName, 1); + }) + + const contactMapSorted = new Map([...contactMap.entries()].sort((a, b) => b[1] - a[1])); + + const keys = Array.from(contactMapSorted.keys()).slice(0, 3); + const values = Array.from(contactMapSorted.values()).slice(0, 3); + const mapContactsLimited = keys.map((el, i) => ({ firstName: el, value: values[i] })); + + return { + statistics: [ + { + type: StatisticType.RANKING, + value: mapContactsLimited, + name: 'Contacts with the same first name.', }, - }) - - if (!contactsData) - return null - - var contactMap = new Map() - contactsData.data.forEach((entry) => { - const firstName : string = entry.displayName === undefined ? "" : getFirstName(entry.displayName); - if(contactMap.has(firstName)) - contactMap.set(firstName, contactMap.get(firstName) +1); - else - contactMap.set(firstName, 1); - }) - - const contactMapSorted = new Map([...contactMap.entries()].sort((a, b) => b[1] - a[1])); - - let keys = Array.from(contactMapSorted.keys()).slice(0, 3); - let values = Array.from(contactMapSorted.values()).slice(0, 3); - const mapContactsLimited = keys.map((el, i) => ({ firstName: el, value: values[i] })); - - return { - statistics: [ - { - type: StatisticType.RANKING, - value: mapContactsLimited, - name: 'Contacts with the same first name.', - }, - ], - parsedFiles: contactsData?.parsedFiles ?? [], - } + ], + parsedFiles: contactsData?.parsedFiles ?? [], + } }