Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ip6.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Created by elgs on 3/5/16.
  3. */
  4. ;(function () {
  5. 'use strict';
  6. var normalize = function (a) {
  7. if (!_validate(a)) {
  8. return false;
  9. }
  10. var nh = a.split(/\:\:/g);
  11. if (nh.length > 2) {
  12. return false;
  13. }
  14. var sections = [];
  15. if (nh.length == 1) {
  16. // full mode
  17. sections = a.split(/\:/g);
  18. if (sections.length !== 8) {
  19. return false;
  20. }
  21. } else if (nh.length == 2) {
  22. // compact mode
  23. var n = nh[0];
  24. var h = nh[1];
  25. var ns = n.split(/\:/g);
  26. var hs = h.split(/\:/g);
  27. for (var i in ns) {
  28. sections[i] = ns[i];
  29. }
  30. for (var i = hs.length; i > 0; --i) {
  31. sections[7 - (hs.length - i)] = hs[i - 1];
  32. }
  33. }
  34. for (var i = 0; i < 8; ++i) {
  35. if (sections[i] === undefined) {
  36. sections[i] = '0000';
  37. }
  38. if (sections[i].length < 4) {
  39. sections[i] = '0000'.substring(0, 4 - sections[i].length) + sections[i];
  40. }
  41. }
  42. return sections.join(':');
  43. };
  44. var abbreviate = function (a) {
  45. if (!_validate(a)) {
  46. return false;
  47. }
  48. a = normalize(a);
  49. a = a.replace(/0000/g, 'g');
  50. a = a.replace(/\:000/g, ':');
  51. a = a.replace(/\:00/g, ':');
  52. a = a.replace(/\:0/g, ':');
  53. a = a.replace(/g/g, '0');
  54. var sections = a.split(/\:/g);
  55. var zPreviousFlag = false;
  56. var zeroStartIndex = -1;
  57. var zeroLength = 0;
  58. var zStartIndex = -1;
  59. var zLength = 0;
  60. for (var i = 0; i < 8; ++i) {
  61. var section = sections[i];
  62. var zFlag = (section === '0');
  63. if (zFlag && !zPreviousFlag) {
  64. zStartIndex = i;
  65. }
  66. if (!zFlag && zPreviousFlag) {
  67. zLength = i - zStartIndex;
  68. }
  69. if (zLength > 1 && zLength > zeroLength) {
  70. zeroStartIndex = zStartIndex;
  71. zeroLength = zLength;
  72. }
  73. zPreviousFlag = (section === '0');
  74. }
  75. if (zPreviousFlag) {
  76. zLength = 8 - zStartIndex;
  77. }
  78. if (zLength > 1 && zLength > zeroLength) {
  79. zeroStartIndex = zStartIndex;
  80. zeroLength = zLength;
  81. }
  82. //console.log(zeroStartIndex, zeroLength);
  83. //console.log(sections);
  84. if (zeroStartIndex >= 0 && zeroLength > 1) {
  85. sections.splice(zeroStartIndex, zeroLength, 'g');
  86. }
  87. //console.log(sections);
  88. a = sections.join(':');
  89. //console.log(a);
  90. a = a.replace(/\:g\:/g, '::');
  91. a = a.replace(/\:g/g, '::');
  92. a = a.replace(/g\:/g, '::');
  93. a = a.replace(/g/g, '::');
  94. //console.log(a);
  95. return a;
  96. };
  97. // Basic validation
  98. var _validate = function (a) {
  99. return /^[a-f0-9\\:]+$/ig.test(a);
  100. };
  101. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  102. exports.normalize = normalize;
  103. exports.abbreviate = abbreviate;
  104. exports._validate = _validate;
  105. } else {
  106. window.normalize = normalize;
  107. window.abbreviate = abbreviate;
  108. window._validate = _validate;
  109. }
  110. })();