author | ymh <ymh.work@gmail.com> |
Fri, 04 Nov 2016 19:03:25 +0100 | |
changeset 392 | 4fbe94af93e8 |
parent 304 | 20071981ba2a |
child 447 | 38d5789e30d0 |
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 |
|
137 | 40 |
export function switchArkBnfLink(s) { |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
41 |
if (!s) { |
137 | 42 |
return s; |
43 |
} |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
44 |
if (typeof s === 'string' && s.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { |
137 | 45 |
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
|
46 |
} else if (typeof s === 'string' && s.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { |
137 | 47 |
return constants.BNF_BASE_URL + s.slice(constants.BNF_ARK_BASE_URL.length); |
48 |
} |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
49 |
|
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
50 |
return s; |
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
51 |
} |
137 | 52 |
|
53 |
export function calculateBnfArkControlChar(id) { |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
141
diff
changeset
|
54 |
if (!id) { |
137 | 55 |
return null; |
56 |
} |
|
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
57 |
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
|
58 |
return s + (i + 1) * (c in constants.NOID_CHARS_POS ? constants.NOID_CHARS_POS[c] : 0); |
137 | 59 |
}, 0); |
392
4fbe94af93e8
Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents:
304
diff
changeset
|
60 |
|
137 | 61 |
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
|
62 |
} |