-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathlicense-utilities.js
104 lines (98 loc) · 3.25 KB
/
license-utilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const CC0Attributes = { BY: false, NC: false, ND: false, SA: false }
const CCBYAttributes = { BY: true, NC: false, ND: false, SA: false }
const visibleSetters = {
FS: {
true: ['FS', 'DD', 'AD'],
false: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
},
BY: {
// Decide if NC/ND/SA should be disabled or removed on CC0
true: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD'],
false: ['FS', 'BY', 'CW', 'NC', 'ND', 'SA', 'AD']
},
ND: {
true: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
}
}
const disabledSetters = {
// Steps that should be disabled if other steps are selected/not selected
BY: {
false: ['NC', 'ND', 'SA']
},
ND: {
true: ['SA']
}
}
function shortToAttr(shortLicenseName) {
const short = shortLicenseName
if (short.includes('CC0')) {
return { ...CC0Attributes }
}
const nc = short.includes('NC')
const nd = short.includes('ND')
const sa = short.includes('SA')
return { ...CCBYAttributes, NC: nc, ND: nd, SA: sa }
}
function attrToShort(attr) {
if (!attr.BY) { return 'CC0 1.0' }
let base = 'CC BY'
if (attr.NC) { base += '-NC' }
if (!attr.ND && attr.SA) {
base += '-SA'
} else if (attr.ND) {
base += '-ND'
}
base += ' 4.0'
return base
}
function attrToFull(attr) {
if (!attr.BY) { return 'CC0 1.0 Universal' }
let base = 'Attribution'
if (attr.NC) { base += '-NonCommercial' }
if (!attr.ND && attr.SA) {
base += '-ShareAlike'
} else if (attr.ND) {
base += '-NoDerivatives'
}
base += ' 4.0 International'
return base
}
// function licenseUrl(shortLicenseName) {
// Returns url to license from short license name with version number (eg. 'CC BY 4.0')
// TODO: check how it works: it doesn't use '-' to join elements ?
// if (shortLicenseName.includes('CC0')) {
// return 'https://creativecommons.org/publicdomain/zero/1.0/?ref=ccchooser'
// }
// const short = shortLicenseName.toLowerCase().slice(3, shortLicenseName.length - 4)
// return 'https://creativecommons.org/licenses/' + short + '/4.0/?ref=ccchooser'
// }
function licenseUrl(attr) {
// Returns url to license from short license name with version number (eg. 'CC BY 4.0')
if (!attr.BY) {
return 'https://creativecommons.org/publicdomain/zero/1.0/?ref=ccchooser'
}
let short = attrToShort(attr).toLowerCase().slice(3)
short = short.slice(0, short.length - 4)
return 'https://creativecommons.org/licenses/' + short + '/4.0/?ref=ccchooser'
}
function licenseSlug(shortLicenseName) {
// Returns lower case slugified string of license name without the version number
// 'CC BY 4.0' -> 'cc-by'
return shortLicenseName
.toLowerCase()
.replace(' ', '-')
.slice(0, shortLicenseName.length - 4)
}
function licenseIconsArr(licenseAttributes) {
if (!licenseAttributes.BY) {
return ['zero']
}
const iconsArray = []
for (const key in licenseAttributes) {
if (licenseAttributes[key]) {
iconsArray.push(key.toLowerCase())
}
}
return iconsArray
}
export { CC0Attributes, CCBYAttributes, visibleSetters, disabledSetters, shortToAttr, attrToShort, attrToFull, licenseUrl, licenseSlug, licenseIconsArr }