Skip to content

Commit

Permalink
added url encoding to urlsnaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanderp committed Dec 31, 2023
1 parent b0d2af2 commit a415d67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/snaks/URLSnak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ export default class URLSnak extends Snak {
}

/**
* the value will be url encoded
*
* @alias url
* @returns {string | undefined} the value of the snak
*/
get value(): string | undefined {
return this.url;
if (!this.url) return undefined;
return encodeURI(this.url);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Snaks/UrlSnak.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ const urlSnak = {
};

describe('URL Snak', () => {
describe('get', () => {
it('should return the value', () => {
const snak = new URLSnak(urlSnak);

expect(snak.value).toBe('http://www.berlin.de/special/immobilien-und-wohnen/stadtteile/');
});

it('should return undefined if there is no value', () => {
const snak = new URLSnak(urlSnak);
snak.value = undefined;

expect(snak.value).toBe(undefined);
});

it('should return the value encoded', () => {
const snak = URLSnak.fromURL('P854', 'https://www.GünterStraßenbahn.AT/');

expect(snak.value).toBe('https://www.G%C3%BCnterStra%C3%9Fenbahn.AT/');
});
});

describe('toJSON', () => {
it('should have the right JSON stringification', () => {
const snak = new URLSnak(urlSnak);
Expand Down

0 comments on commit a415d67

Please sign in to comment.