common/corpus-common-addon/addon/utils/utils.js
changeset 137 1baa7c6bd370
parent 135 66c1d31e2f97
child 141 c0e8626a271c
--- a/common/corpus-common-addon/addon/utils/utils.js	Wed Mar 02 13:47:07 2016 +0100
+++ b/common/corpus-common-addon/addon/utils/utils.js	Thu Mar 03 17:34:12 2016 +0100
@@ -1,5 +1,33 @@
 import * as constants from 'corpus-common-addon/utils/constants';
+import _ from "lodash/lodash";
 
 export function isBnfLink(s) {
-  return s.startsWith(constants.BNF_BASE_URL) || s.startsWith(constants.BNF_ARK_BASE_URL) || s.startsWith(constants.BNF_ARK_BASE_ID);
+  return s && (s.startsWith(constants.BNF_BASE_URL) || s.startsWith(constants.BNF_ARK_BASE_URL) || s.startsWith(constants.BNF_ARK_BASE_ID));
+};
+
+export function isArkBnfLink(s) {
+  return s && (s.startsWith(constants.BNF_BASE_URL+constants.BNF_ARK_BASE_ID) || s.startsWith(constants.BNF_ARK_BASE_URL+constants.BNF_ARK_BASE_ID));
 };
+
+export function switchArkBnfLink(s) {
+  if(!s) {
+    return s;
+  }
+  if(s.startsWith(constants.BNF_BASE_URL+constants.BNF_ARK_BASE_ID)) {
+    return constants.BNF_ARK_BASE_URL + s.slice(constants.BNF_BASE_URL.length);
+  } else if (s.startsWith(constants.BNF_ARK_BASE_URL+constants.BNF_ARK_BASE_ID)) {
+    return constants.BNF_BASE_URL + s.slice(constants.BNF_ARK_BASE_URL.length);
+  } else {
+    return s;
+  }
+};
+
+export function calculateBnfArkControlChar(id) {
+  if(!id) {
+    return null;
+  }
+  var sum = _.reduce(id, function(s, c, i) {
+    return s + (i+1) * (c in constants.NOID_CHARS_POS ? constants.NOID_CHARS_POS[c] : 0);
+  }, 0);
+  return constants.NOID_CHARS[sum % constants.NOID_CHARS.length];
+};