Skip to content

Commit 808d44b

Browse files
committed
Move parsing functions for CC licenses to a utility file
1 parent a593113 commit 808d44b

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { TranslateModule } from '@ngx-translate/core';
1111
import { Item } from 'src/app/core/shared/item.model';
1212
import { MetadataFieldWrapperComponent } from 'src/app/shared/metadata-field-wrapper/metadata-field-wrapper.component';
13+
import { parseCcCode } from 'src/app/shared/utils/license.utils';
1314

1415
@Component({
1516
selector: 'ds-item-page-cc-license-field',
@@ -59,23 +60,12 @@ export class ItemPageCcLicenseFieldComponent implements OnInit {
5960
showImage = true;
6061
imgSrc: string;
6162

62-
/**
63-
* Parse a URI an return its CC code. URIs pointing to non-CC licenses will return null.
64-
* @param uri
65-
* @returns the CC code or null if uri is not a valid CC URI
66-
*/
67-
public static parseCcCode(uri: string): string {
68-
const regex = /.*creativecommons.org\/(licenses|publicdomain)\/([^/]+)/gm;
69-
const matches = regex.exec(uri ?? '') ?? [];
70-
return matches.length > 2 ? matches[2] : null;
71-
}
72-
7363
ngOnInit() {
7464
this.uri = this.item.firstMetadataValue(this.ccLicenseUriField);
7565
this.name = this.item.firstMetadataValue(this.ccLicenseNameField);
7666

7767
// Extracts the CC license code from the URI
78-
const ccCode = ItemPageCcLicenseFieldComponent.parseCcCode(this.uri);
68+
const ccCode = parseCcCode(this.uri);
7969
this.imgSrc = ccCode ? `assets/images/cc-licenses/${ccCode}.png` : null;
8070
}
8171
}

src/app/item-page/simple/field-components/specific-field/license/item-page-license-field.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from 'src/app/core/shared/operators';
2727
import { ItemPageCcLicenseFieldComponent } from 'src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component';
2828
import { MetadataFieldWrapperComponent } from 'src/app/shared/metadata-field-wrapper/metadata-field-wrapper.component';
29+
import { isCcLicense } from 'src/app/shared/utils/license.utils';
2930

3031
@Component({
3132
selector: 'ds-item-page-license-field',
@@ -77,7 +78,7 @@ export class ItemPageLicenseFieldComponent implements OnInit, OnDestroy {
7778
getRemoteDataPayload(),
7879
).subscribe((remoteData: ConfigurationProperty) => {
7980
const ccLicenseUriField = remoteData?.values && remoteData?.values?.length > 0 ? remoteData.values[0] : 'dc.rights.uri';
80-
this.hasCcLicenseUri$ = of(!!ItemPageCcLicenseFieldComponent.parseCcCode(this.item.firstMetadataValue(ccLicenseUriField)));
81+
this.hasCcLicenseUri$ = of(isCcLicense(this.item.firstMetadataValue(ccLicenseUriField)));
8182
}),
8283
);
8384

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Parse a URI an return its CC code. URIs pointing to non-CC licenses will return null.
3+
* @param uri
4+
* @returns the CC code or null if uri is not a valid CC URI
5+
*/
6+
export function parseCcCode(uri: string): string {
7+
const regex = /.*creativecommons.org\/(licenses|publicdomain)\/([^/]+)/gm;
8+
const matches = regex.exec(uri ?? '') ?? [];
9+
return matches.length > 2 ? matches[2] : null;
10+
}
11+
12+
/**
13+
* Returns whether a URI denotes a valid URI for CC licenses.
14+
* @param uri
15+
* @returns true if the URI corresponds to a reconigzable CC license URI or false otherwise
16+
*/
17+
export function isCcLicense(uri: string): boolean {
18+
return !!parseCcCode(uri);
19+
}

0 commit comments

Comments
 (0)