Skip to content

Commit

Permalink
Add types/vcfJS.d.ts, update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLauu committed Jun 4, 2023
1 parent 31445b8 commit 4d98524
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/AutoTOC.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: AutoToc
on:
push:
branches: [main]
branches: [master]
paths: ['README.md', '.github/workflows/AutoTOC.yml']

jobs:
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vcfJS
# js.vcf
[![npm](https://img.shields.io/npm/v/js.vcf.svg?style=flat-square)](https://npmjs.com/package/js.vcf)
[![npm license](https://img.shields.io/npm/l/js.vcf.svg?style=flat-square)](https://npmjs.com/package/js.vcf)
***
Expand All @@ -8,23 +8,24 @@

## Introduction

This is a simple javascript lib which parses vCard file (AKA \*.vcf) and generate vcf object.
This is a simple javascript lib which parses vCard file (\*.vcf) and generate vcf object.

Only support vCard 2.1 version.
- Only support vCard 2.1 version.
- Only support parse, generating is not currently (or permanently) supported.

## USE

### Install

`npm install vcfJS`
`npm install js.vcf`

### parse one vCard

```javascript
const fs = require('fs')
const {parseVCard} = require('vcfJS')
const {parseVCard} = require('js.vcf')

const vcfText = fs.readFileSync('/path/to/vcffile.vcf')
const vcfText = fs.readFileSync('/path/to/vcf.vcf')
var vCard = parseVCard(vcfText)
console.log(vCard.NF[0].VALUE)
console.log(vCard)
Expand All @@ -50,9 +51,9 @@ console.log(vCard)

```javascript
const fs = require('fs')
const {parseVCards} = require('vcfJS')
const {parseVCards} = require('js.vcf')

const vcfText = fs.readFileSync('/path/to/vcffile.vcf')
const vcfText = fs.readFileSync('/path/to/vcf.vcf')
var vCards = parseVCard(vcfText)

console.log(vCards)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-promise": "^6.1.1"
}
},
"types": "./types/vcfJS.d.ts"
}
93 changes: 41 additions & 52 deletions src/vcfJS.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
/*
* param: string : vCard(s) text
* return: object : vCardObj
*/
const EOL = '\r\n'

function parseVCards(vCardsText) {
const vCards = []

const EOL='\r\n'

function parseVCards(vCardsText){
const vCards=[]

vCardsText+=EOL
var vCardsText=vCardsText
vCardsText += EOL
var vCardsText = vCardsText
.trim()
.replace(/\r\n\r\n/g,EOL)
.replace(/\r\n{2,}/g, EOL)


var vCardsList=vCardsText.split('END:VCARD')
var vCardsList = vCardsText.split('END:VCARD')

vCardsList.forEach(c=>{
vCardsList.forEach(c => {
if (!c) return
vCards.push(parseVCard(c))
})
Expand All @@ -26,7 +20,7 @@ function parseVCards(vCardsText){
}


function parseVCard(vCardText){
function parseVCard(vCardText) {

// vCard={
// N:{
Expand All @@ -48,18 +42,18 @@ function parseVCard(vCardText){
// }...
// }

const vCard={}
const vCard = {}
//contentline = [group "."] name *(";" param) ":" value CRLF
// const propName=["SOURCE","KIND","FN","N","NICKNAME","PHOTO","BDAY","ANNIVERSARY","GENDER","ADR","TEL","EMAIL","IMPP","LANG","TZ","GEO","TITLE","ROLE","LOGO","ORG","MEMBER","RELATED","CATEGORIES","NOTE","PRODID","REV","SOUND","UID","CLIENTPIDMAP","URL","KEY","FBURL","CALADRURI","CALURI","XML"]

const vCardList=vCardText
.replace(/BEGIN:VCARD/g,'')
.replace(/\r\n[\x20\x09]/g,'')//unfold
.replace(/(.)\r\n\1/g,'=')//unfold
.replace(/\\:/g,'<%escapedCol%>')
.replace(/\\;/g,'<%escapedSemiCol%>')
.replace(/\\,/g,'<%escapedSemiCom%>')
.replace(/\r\n\r\n/g,EOL)
const vCardList = vCardText
.replace(/BEGIN:VCARD/g, '')
.replace(/\r\n[\x20\x09]/g, '')//unfold
.replace(/(.)\r\n\1/g, '=')//unfold
.replace(/\\:/g, '<%escapedCol%>')
.replace(/\\;/g, '<%escapedSemiCol%>')
.replace(/\\,/g, '<%escapedSemiCom%>')
.replace(/\r\n{2,}/g, EOL)
.trim()
.split(EOL)

Expand All @@ -68,8 +62,8 @@ function parseVCard(vCardText){
// .map(e=>"("+e+")")
// .join("|")+':|;.*?'+EOL+'$','gi')

for (var i=0;i<vCardList.length;i++){
var procLine=vCardList[i].trim()
for (var i = 0; i < vCardList.length; i++) {
var procLine = vCardList[i].trim()
//unfold line
// while(i<vCardList.length-1 && !tagExp.test(vCardList[i+1]))
// {
Expand All @@ -78,40 +72,35 @@ function parseVCard(vCardText){
// }

//contentline = [group "."] name *(";" param) ":" value CRLF
var [pnameParam,...value]=procLine.split(':')
var [pname,...params]=pnameParam.split(';')
var [pnameParam, ...value] = procLine.split(':')
var [pname, ...params] = pnameParam.split(';')

const paramsValue={}
const paramsValue = {}

params.forEach(p=>{
var [k,v=null]=p.split('=')
paramsValue[k]=v
params.forEach(p => {
var [k, v = null] = p.split('=')
paramsValue[k] = v
})

value=value.join(':')
if (paramsValue['CHARSET'] && paramsValue['CHARSET'].toUpperCase()=="UTF-8"){
value=value
value = value.join(':')
if (paramsValue['CHARSET'] && paramsValue['CHARSET'].toUpperCase() == "UTF-8") {
value = value
.split(';').map(
np=>np.split(',').map(
npp=>Buffer.from(
npp.replace(/=/g,''),'hex')
.toString()
).join(',')
).join(';')
np => np.split(',').map(
npp => Buffer.from(
npp.replace(/=/g, ''), 'hex')
.toString()
).join(',')
).join(';')
}
paramsValue.VALUE=value
.replace(/<%escapedCol%>/g,':')
.replace(/<%escapedSemiCol%>/g,';')
.replace(/<%escapedSemiCom%>/g,',')
paramsValue.VALUE = value
.replace(/<%escapedCol%>/g, ':')
.replace(/<%escapedSemiCol%>/g, ';')
.replace(/<%escapedSemiCom%>/g, ',')

if (!vCard[pname]) vCard[pname]=[]
if (!vCard[pname]) vCard[pname] = []
vCard[pname].push(paramsValue)
}
delete vCard.END
return vCard
}

module.exports ={
parseVCard:parseVCard,
parseVCards:parseVCards
}
}
4 changes: 4 additions & 0 deletions types/vcfJS.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'js.vcf' {
export default function parseVCards(vCardsText: string): object
export function parseVCard(vCardText: string): object
}

0 comments on commit 4d98524

Please sign in to comment.