author | ymh <ymh.work@gmail.com> |
Sun, 27 Nov 2016 15:12:30 +0100 | |
changeset 447 | 38d5789e30d0 |
parent 392 | 4fbe94af93e8 |
child 490 | 76da86cf9696 |
permissions | -rw-r--r-- |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
1 |
/* eslint-disable no-magic-numbers, no-ternary */ |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
2 |
|
135 | 3 |
import * as constants from 'corpus-common-addon/utils/constants'; |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
4 |
import _ from 'lodash/lodash'; |
135 | 5 |
|
141
c0e8626a271c
literal are now objects if one of lang or datatype is not null
ymh <ymh.work@gmail.com>
parents:
137
diff
changeset
|
6 |
export function isLexvoLink(s) { |
c0e8626a271c
literal are now objects if one of lang or datatype is not null
ymh <ymh.work@gmail.com>
parents:
137
diff
changeset
|
7 |
return s && typeof s === 'string' && s.startsWith(constants.LEXVO_BASE_URL); |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
8 |
} |
141
c0e8626a271c
literal are now objects if one of lang or datatype is not null
ymh <ymh.work@gmail.com>
parents:
137
diff
changeset
|
9 |
|
135 | 10 |
export function isBnfLink(s) { |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
11 |
return s && |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
12 |
typeof s === 'string' && |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
13 |
(s.startsWith(constants.BNF_BASE_URL) || |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
14 |
s.startsWith(constants.BNF_ARK_BASE_URL) || |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
15 |
s.startsWith(constants.BNF_ARK_BASE_ID)); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
16 |
} |
137 | 17 |
|
18 |
export function isArkBnfLink(s) { |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
19 |
return s && |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
20 |
typeof s === 'string' && |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
21 |
(s.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID) || |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
22 |
s.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
23 |
} |
137 | 24 |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
25 |
export function isGeonamesLink(s) { |
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
26 |
return (typeof s === 'string') && (s.match(constants.GEONAMES_BASE_URLS) !== null); |
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
27 |
} |
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
28 |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
29 |
export function getGeonamesCode(s) { |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
30 |
const m = s.match(constants.GEONAMES_BASE_URLS); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
31 |
let code = s; |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
32 |
|
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
33 |
if (m) { |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
34 |
code = s.slice(m[0].length); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
35 |
} |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
36 |
|
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
37 |
return code.replace(/\/+$/, ''); |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
38 |
} |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
39 |
|
447
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
40 |
export function isViafLink(s) { |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
41 |
return (typeof s === 'string') && (s.match(constants.VIAF_URL_REGEXP) !== null); |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
42 |
} |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
43 |
|
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
44 |
export function getViafCode(s) { |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
45 |
const m = s.match(constants.VIAF_URL_REGEXP); |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
46 |
let code = s; |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
47 |
|
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
48 |
if (m) { |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
49 |
code = s.slice(m[0].length); |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
50 |
} |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
51 |
|
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
52 |
return code.replace(/\/+$/, ''); |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
53 |
} |
38d5789e30d0
Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents:
392
diff
changeset
|
54 |
|
137 | 55 |
export function switchArkBnfLink(s) { |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
56 |
if (!s) { |
137 | 57 |
return s; |
58 |
} |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
59 |
if (typeof s === 'string' && s.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { |
137 | 60 |
return constants.BNF_ARK_BASE_URL + s.slice(constants.BNF_BASE_URL.length); |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
61 |
} else if (typeof s === 'string' && s.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { |
137 | 62 |
return constants.BNF_BASE_URL + s.slice(constants.BNF_ARK_BASE_URL.length); |
63 |
} |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
64 |
|
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
65 |
return s; |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
66 |
} |
137 | 67 |
|
68 |
export function calculateBnfArkControlChar(id) { |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
69 |
if (!id) { |
137 | 70 |
return null; |
71 |
} |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
72 |
const sum = _.reduce(id, function (s, c, i) { |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
73 |
return s + (i + 1) * (c in constants.NOID_CHARS_POS ? constants.NOID_CHARS_POS[c] : 0); |
137 | 74 |
}, 0); |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
75 |
|
137 | 76 |
return constants.NOID_CHARS[sum % constants.NOID_CHARS.length]; |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
77 |
} |