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.

utils.js 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @fileoverview Define utility functions for token store.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Gets `token.range[0]` from the given token.
  11. * @param {Node|Token|Comment} token The token to get.
  12. * @returns {number} The start location.
  13. * @private
  14. */
  15. function getStartLocation(token) {
  16. return token.range[0];
  17. }
  18. //------------------------------------------------------------------------------
  19. // Exports
  20. //------------------------------------------------------------------------------
  21. /**
  22. * Finds the index of the first token which is after the given location.
  23. * If it was not found, this returns `tokens.length`.
  24. * @param {(Token|Comment)[]} tokens It searches the token in this list.
  25. * @param {number} location The location to search.
  26. * @returns {number} The found index or `tokens.length`.
  27. */
  28. exports.search = function search(tokens, location) {
  29. const index = tokens.findIndex(el => location <= getStartLocation(el));
  30. return index === -1 ? tokens.length : index;
  31. };
  32. /**
  33. * Gets the index of the `startLoc` in `tokens`.
  34. * `startLoc` can be the value of `node.range[1]`, so this checks about `startLoc - 1` as well.
  35. * @param {(Token|Comment)[]} tokens The tokens to find an index.
  36. * @param {Object} indexMap The map from locations to indices.
  37. * @param {number} startLoc The location to get an index.
  38. * @returns {number} The index.
  39. */
  40. exports.getFirstIndex = function getFirstIndex(tokens, indexMap, startLoc) {
  41. if (startLoc in indexMap) {
  42. return indexMap[startLoc];
  43. }
  44. if ((startLoc - 1) in indexMap) {
  45. const index = indexMap[startLoc - 1];
  46. const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
  47. /*
  48. * For the map of "comment's location -> token's index", it points the next token of a comment.
  49. * In that case, +1 is unnecessary.
  50. */
  51. if (token && token.range[0] >= startLoc) {
  52. return index;
  53. }
  54. return index + 1;
  55. }
  56. return 0;
  57. };
  58. /**
  59. * Gets the index of the `endLoc` in `tokens`.
  60. * The information of end locations are recorded at `endLoc - 1` in `indexMap`, so this checks about `endLoc - 1` as well.
  61. * @param {(Token|Comment)[]} tokens The tokens to find an index.
  62. * @param {Object} indexMap The map from locations to indices.
  63. * @param {number} endLoc The location to get an index.
  64. * @returns {number} The index.
  65. */
  66. exports.getLastIndex = function getLastIndex(tokens, indexMap, endLoc) {
  67. if (endLoc in indexMap) {
  68. return indexMap[endLoc] - 1;
  69. }
  70. if ((endLoc - 1) in indexMap) {
  71. const index = indexMap[endLoc - 1];
  72. const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
  73. /*
  74. * For the map of "comment's location -> token's index", it points the next token of a comment.
  75. * In that case, -1 is necessary.
  76. */
  77. if (token && token.range[1] > endLoc) {
  78. return index - 1;
  79. }
  80. return index;
  81. }
  82. return tokens.length - 1;
  83. };