Skip to content

Commit 21dd22e

Browse files
authored
Fixes the addMediaURL method (#18)
* Fixes the addMediaURL() method
1 parent 6d88f4d commit 21dd22e

File tree

7 files changed

+64
-26
lines changed

7 files changed

+64
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 0.4.1
4+
5+
> Mar 25, 2021
6+
7+
- Fixes the `addMediaContent()` method ([#17](https://github.com/joaocarmo/vcard-creator/pull/17) thanks to @Falklian)
8+
- Fixes the `addMediaURL()` method
9+
310
## 0.4.0
411

512
> Mar 21, 2021

README.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ It outputs the vCard text that should be saved as a `*.vcf` file.
1111

1212
## Origin
1313

14-
This is based on _jeroendesloovere_'s
15-
[vCard](https://github.com/jeroendesloovere/vcard) for PHP.
14+
This is based on _jeroendesloovere_'s [vCard][jeroendesloovere] for PHP.
1615

1716
## Installation
1817

@@ -28,15 +27,15 @@ npm install vcard-creator
2827

2928
### As an ESM module (web)
3029

31-
Load **vcard-creator** directly from [skypack](https://skypack.dev) (CDN).
30+
Load **vcard-creator** directly from [skypack][skypack] (CDN).
3231

3332
```html
3433
<script type="module">
3534
import VCard from 'https://cdn.skypack.dev/vcard-creator'
3635
</script>
3736
```
3837

39-
Demo available in [codepen](https://codepen.io/joaocarmo/pen/PozdprL).
38+
Demo available in [codepen][codepen].
4039

4140
### On the web (self-hosted)
4241

@@ -54,33 +53,54 @@ It's exposed through the _window_ global object as explained below.
5453
`main.js`
5554

5655
```js
57-
// define vCard
56+
// Define a new vCard
5857
var VCard = window.vcardcreator.default
5958
var myVCard = new VCard()
6059

61-
// ...
60+
// ...rest of the code
6261
```
6362

6463
### With a bundler / Node.js
6564

6665
With a bundler (e.g. webpack) or in Node.js you can just require / import it.
6766

6867
```js
69-
var VCard = require('vcard-creator').default
68+
const VCard = require('vcard-creator').default
7069

71-
// define a new vCard
72-
var myVCard = new VCard()
70+
// Define a new vCard
71+
const myVCard = new VCard()
7372
```
7473

7574
Or...
7675

7776
```js
7877
import VCard from 'vcard-creator'
7978

80-
// define vCard
79+
// Define a new vCard
8180
const myVCard = new VCard()
8281
```
8382

83+
### Including an image
84+
85+
You need to provide the image already properly encoded (base64). Most software
86+
will probably ignore a photo URL, even if it adheres to the specification.
87+
88+
```js
89+
// Example in Node.js
90+
91+
const fs = require('fs')
92+
const VCard = require('vcard-creator').default
93+
94+
const imagePath = './lib/__tests__/assets/sample.jpg'
95+
const image = fs.readFileSync(imagePath, { encoding: 'base64', flag: 'r' })
96+
97+
const vCard = new VCard()
98+
99+
vCard.addPhoto(image, 'JPEG')
100+
```
101+
102+
Include the proper [MIME type][mime-types] (defaults to `JPEG`).
103+
84104
### iCalendar format
85105

86106
For Apple devices that don't support the `vcf` file format, there is a
@@ -90,11 +110,11 @@ with a `ics` file extension instead.
90110
The trick is to create an iCalendar file with a vCard attached.
91111

92112
```js
93-
// define a new vCard as 'vcalendar'
94-
var myVCalendar = new VCard('vcalendar')
113+
// Define a new vCard as 'vcalendar'
114+
const myVCalendar = new VCard('vcalendar')
95115

96-
// or set it afterwards
97-
var myOtherVCalendar = new VCard()
116+
// ...or set it afterwards
117+
const myOtherVCalendar = new VCard()
98118
myOtherVCalendar.setFormat('vcalendar')
99119
```
100120

@@ -103,20 +123,20 @@ myOtherVCalendar.setFormat('vcalendar')
103123
```js
104124
import VCard from 'vcard-creator'
105125

106-
// define a new vCard
126+
// Define a new vCard
107127
const myVCard = new VCard()
108128

109-
// define variables
129+
// Some variables
110130
const lastname = 'Desloovere'
111131
const firstname = 'Jeroen'
112132
const additional = ''
113133
const prefix = ''
114134
const suffix = ''
115135

116136
myVCard
117-
// add personal data
137+
// Add personal data
118138
.addName(lastname, firstname, additional, prefix, suffix)
119-
// add work data
139+
// Add work data
120140
.addCompany('Siesqo')
121141
.addJobtitle('Web Developer')
122142
.addRole('Data Protection Officer')
@@ -167,3 +187,10 @@ yarn test:web-build
167187

168188
yarn test:web-export
169189
```
190+
191+
<!-- References -->
192+
193+
[codepen]: https://codepen.io/joaocarmo/pen/PozdprL
194+
[jeroendesloovere]: https://github.com/jeroendesloovere/vcard
195+
[mime-types]: https://www.iana.org/assignments/media-types/media-types.xhtml#image
196+
[skypack]: https://skypack.dev

lib/VCard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ ${name};${extended};${street};${city};${region};${zip};${country}\
211211
* @return {this}
212212
*/
213213
private addMediaURL(property: string, url: string, element: string): this {
214-
this.setProperty(element, property, `VALUE=uri:${url}`)
214+
this.setProperty(element, `${property};VALUE=uri`, url)
215215

216216
return this
217217
}

lib/__tests__/VCard.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ describe('Test vCard', () => {
1010
const additional = ''
1111
const prefix = ''
1212
const suffix = ''
13-
const photoURL =
14-
'https://live.staticflickr.com/65535/51059209772_bf41a28c7e_q.jpg'
13+
const photoURL = 'https://example.com/img/photo.jpg'
1514

1615
it("should create and output the proper 'vcard' format", () => {
1716
// Setup a fixed date
@@ -58,8 +57,7 @@ TEL;WORK:123456789\r\n\
5857
ADR;WORK;POSTAL;CHARSET=utf-8:name;extended;street;worktown;state;workpos\r\n\
5958
tcode;Belgium\r\n\
6059
URL:http://www.jeroendesloovere.be\r\n\
61-
PHOTO:VALUE=uri:https://live.staticflickr.com/65535/51059209772_bf41a28c7\r\n\
62-
e_q.jpg\r\n\
60+
PHOTO;VALUE=uri:https://example.com/img/photo.jpg\r\n\
6361
END:VCARD\r\n\
6462
`
6563

test-functional/test-export.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ vcard
2929
'Belgium',
3030
)
3131
.addURL('http://www.jeroendesloovere.be')
32-
.addPhoto('https://live.staticflickr.com/65535/51059209772_bf41a28c7e_q.jpg')
32+
.addPhotoURL(
33+
'https://cdn.jsdelivr.net/gh/joaocarmo/vcard-creator@master/lib/__tests__/assets/sample.jpg',
34+
)
3335

3436
var output = vcard.toString()
3537

test-functional/test-pre-build.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ vcard
3030
'Belgium',
3131
)
3232
.addURL('http://www.jeroendesloovere.be')
33-
.addPhoto('https://live.staticflickr.com/65535/51059209772_bf41a28c7e_q.jpg')
33+
.addPhotoURL(
34+
'https://cdn.jsdelivr.net/gh/joaocarmo/vcard-creator@master/lib/__tests__/assets/sample.jpg',
35+
)
3436

3537
var output = vcard.toString()
3638

test-functional/test-vcalendar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ vCard
3131
'Belgium',
3232
)
3333
.addURL('http://www.jeroendesloovere.be')
34-
.addPhoto('https://live.staticflickr.com/65535/51059209772_bf41a28c7e_q.jpg')
34+
.addPhotoURL(
35+
'https://cdn.jsdelivr.net/gh/joaocarmo/vcard-creator@master/lib/__tests__/assets/sample.jpg',
36+
)
3537

3638
console.log(vCard.toString())

0 commit comments

Comments
 (0)