Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Added processing support for V-Card files GH-50
Browse files Browse the repository at this point in the history
#vcard
  • Loading branch information
mrbrianevans committed Feb 2, 2022
1 parent 396a7c3 commit 0809c23
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 7 deletions.
16 changes: 15 additions & 1 deletion lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"papaparse": "^5.3.1",
"pretty": "^2.0.0",
"snake-case": "^3.0.4",
"ua-parser-js": "^1.0.2"
"ua-parser-js": "^1.0.2",
"vcard4-ts": "^0.4.1"
},
"devDependencies": {
"@types/node": "^17.0.12",
Expand Down
6 changes: 5 additions & 1 deletion lib/postProcessing/postProcessingCategoriser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {
TwitterTweetsPostProcess
} from './postProcessors/twitter'
import { TextPostProcess } from './postProcessors/genericFallbacks/text'
import { ContactsCsvPostProcess } from './postProcessors/contacts'
import {
ContactsCsvPostProcess,
ContactsVcfPostProcess
} from './postProcessors/contacts'
import {
KeyValuePostProcess,
NestedArrayPostProcess,
Expand Down Expand Up @@ -57,6 +60,7 @@ const postProcessors = {
TwitterFallbackPostProcess,
TwitterManifestPostProcess,
ContactsCsvPostProcess,
ContactsVcfPostProcess,
YouTubeWatchHistoryPostProcess,
YouTubeSearchHistoryPostProcess,
DefaultHtmlPostProcess,
Expand Down
14 changes: 14 additions & 0 deletions lib/postProcessing/postProcessors/contacts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PostProcess } from '../../typedefs/PostProcess'
import { processContacts } from '../../vendors/google/Contacts'
import { processVCardContacts } from '../../vendors/google/VCardContacts'

export const ContactsCsvPostProcess: PostProcess = {
name: 'Contacts',
Expand All @@ -15,3 +16,16 @@ export const ContactsCsvPostProcess: PostProcess = {
component: 'VaadinGrid',
postProcessingFunction: processContacts
}

export const ContactsVcfPostProcess: PostProcess = {
name: 'Contacts',
code: 'contacts-vcf',
classifier: {
topLevelIsArray: true,
filenameRegex: /^.+\.vcf$/,
preProcessingCategory: 'vcard',
itemCriteria: {}
},
component: 'VaadinGrid',
postProcessingFunction: processVCardContacts
}
5 changes: 5 additions & 0 deletions lib/preProcessing/preProcessingCategoriser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const preProcessingTesters: PreProcessingTester[] = [
filenameRegex: /\.html$/,
fileTypes: ['text/html'],
preProcessingCategory: 'html'
},
{
filenameRegex: /\.vcf$/,
fileTypes: ['text/x-vcard'],
preProcessingCategory: 'vcard'
}
]

Expand Down
4 changes: 3 additions & 1 deletion lib/preProcessing/preProcessorMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { twitterJsPreProcessor } from './preProcessors/twitterJs'
import { videoPreProcessor } from './preProcessors/video'
import { imagePreProcessor } from './preProcessors/image'
import { htmlPreProcessor } from './preProcessors/html'
import { vcardPreProcessor } from './preProcessors/vcard'

export const preProcessorMap: { [key in PreProcessingCategory]: PreProcessor } =
{
Expand All @@ -17,5 +18,6 @@ export const preProcessorMap: { [key in PreProcessingCategory]: PreProcessor } =
twitterJs: twitterJsPreProcessor,
image: imagePreProcessor,
video: videoPreProcessor,
html: htmlPreProcessor
html: htmlPreProcessor,
vcard: vcardPreProcessor
}
16 changes: 16 additions & 0 deletions lib/preProcessing/preProcessors/vcard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PreProcessor } from '../../typedefs/PreProcess'
import { ParsedVCards, parseVCards, sortByPREF } from 'vcard4-ts'

export const vcardPreProcessor: PreProcessor<ParsedVCards['vCards']> = ({
filename,
fileType,
fileContent
}) => {
const { nags, vCards } = parseVCards(fileContent, true)
const { length } = vCards
return {
data: vCards,
title: filename,
metadata: { length, nags }
}
}
1 change: 1 addition & 0 deletions lib/typedefs/PreProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type PreProcessingCategory =
| 'image'
| 'video'
| 'html'
| 'vcard'
export type PreProcessingTester = {
// tests
filenameRegex?: RegExp
Expand Down
5 changes: 2 additions & 3 deletions lib/vendors/google/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const processContacts: PostProcessor<CsvContacts, Contact[]> = ({
preProcessedOutput: { data, metadata }
}) => {
const { fields } = metadata
console.log(data)
const contacts: Contact[] = []
for (const contact of data) {
contacts.push({
Expand Down Expand Up @@ -122,7 +121,7 @@ export const processContacts: PostProcessor<CsvContacts, Contact[]> = ({
}
}

function generateContacts(qty = 10): CsvContacts {
function generateCsvContacts(qty = 10): CsvContacts {
const contacts = new Set<CsvContacts[number]>()
for (let i = 0; i < qty; i++) {
const Name = getRandomFullName()
Expand All @@ -141,6 +140,6 @@ function generateContacts(qty = 10): CsvContacts {
}

export function generateContactsFile(qty = 10): string {
const contacts = generateContacts(qty)
const contacts = generateCsvContacts(qty)
return Papa.unparse(contacts)
}
35 changes: 35 additions & 0 deletions lib/vendors/google/VCardContacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ParsedVCards } from 'vcard4-ts'
import { PostProcessor } from '../../typedefs/PostProcess'
import { Contact, CsvContacts } from './Contacts'

export const processVCardContacts: PostProcessor<
ParsedVCards['vCards'],
Contact[]
> = ({ preProcessedOutput: { data, metadata, title } }) => {
const contacts: Contact[] = []
for (const contact of data) {
contacts.push({
address: contact.ADR?.[0]
? {
postCode: contact.ADR[0].value.postalCode?.[0],
streetAddress: contact.ADR[0].value.streetAddress?.[0]
}
: null,
emailAddresses: contact.EMAIL?.map((t) => ({
label: t.parameters?.TYPE?.at(0),
emailAddress: t.value
})),
firstName: contact.N?.value.givenNames.join(' '),
fullName: contact.FN[0]?.value,
lastName: contact.N?.value.familyNames.join(' '),
organisation: contact.ORG?.[0]?.value[0],
otherDetails: contact.x,
phoneNumbers: contact.TEL?.map((t) => ({
label: t.parameters?.TYPE?.[0],
phoneNumber: t.value
})),
profilePictureUrl: contact.PHOTO?.[0]?.value
})
}
return { data: contacts, metadata, title }
}

0 comments on commit 0809c23

Please sign in to comment.