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.

whitespaceChecker.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. 'use strict';
  2. const configurationError = require('./configurationError');
  3. const isSingleLineString = require('./isSingleLineString');
  4. const isWhitespace = require('./isWhitespace');
  5. /**
  6. * @typedef {object} Messages
  7. * @property {function} [expectedBefore]
  8. * @property {function} [rejectedBefore]
  9. * @property {function} [expectedAfter]
  10. * @property {function} [rejectedAfter]
  11. * @property {function} [expectedBeforeSingleLine]
  12. * @property {function} [rejectedBeforeSingleLine]
  13. * @property {function} [expectedBeforeMultiLine]
  14. * @property {function} [rejectedBeforeMultiLine]
  15. * @property {function} [expectedAfterSingleLine]
  16. * @property {function} [rejectedAfterSingleLine]
  17. * @property {function} [expectedAfterMultiLine]
  18. * @property {function} [rejectedAfterMultiLine]
  19. */
  20. /**
  21. * @typedef {object} WhitespaceCheckerArgs
  22. * @property {string} source - The source string
  23. * @property {number} index - The index of the character to check before
  24. * @property {function} err - If a violation is found, this callback
  25. * will be invoked with the relevant warning message.
  26. * Typically this callback will report() the violation.
  27. * @property {function} errTarget - If a violation is found, this string
  28. * will be sent to the relevant warning message.
  29. * @property {string} [lineCheckStr] - Single- and multi-line checkers
  30. * will use this string to determine whether they should proceed,
  31. * i.e. if this string is one line only, single-line checkers will check,
  32. * multi-line checkers will ignore.
  33. * If none is passed, they will use `source`.
  34. * @property {boolean} [onlyOneChar=false] - Only check *one* character before.
  35. * By default, "always-*" checks will look for the `targetWhitespace` one
  36. * before and then ensure there is no whitespace two before. This option
  37. * bypasses that second check.
  38. * @property {boolean} [allowIndentation=false] - Allow arbitrary indentation
  39. * between the `targetWhitespace` (almost definitely a newline) and the `index`.
  40. * With this option, the checker will see if a newline *begins* the whitespace before
  41. * the `index`.
  42. */
  43. /**
  44. * @callback WhitespaceChecker
  45. * @param {WhitespaceCheckerArgs} args
  46. */
  47. /**
  48. * Create a whitespaceChecker, which exposes the following functions:
  49. * - `before()`
  50. * - `beforeAllowingIndentation()`
  51. * - `after()`
  52. * - `afterOneOnly()`
  53. *
  54. * @param {"space" | "newline"} targetWhitespace - This is a keyword instead
  55. * of the actual character (e.g. " ") in order to accommodate
  56. * different styles of newline ("\n" vs "\r\n")
  57. * @param { "always" | "never" | "always-single-line" | "always-multi-line" | "never-single-line" | "never-multi-line" } expectation
  58. * @param {Messages} messages - An object of message functions;
  59. * calling `before*()` or `after*()` and the `expectation` that is passed
  60. * determines which message functions are required
  61. *
  62. * @returns {object} The checker, with its exposed checking functions
  63. */
  64. module.exports = function (targetWhitespace, expectation, messages) {
  65. // Keep track of active arguments in order to avoid passing
  66. // too much stuff around, making signatures long and confusing.
  67. // This variable gets reset anytime a checking function is called.
  68. /**
  69. * @type {{
  70. source?: any,
  71. index?: any,
  72. err: any,
  73. errTarget: any,
  74. onlyOneChar: any,
  75. allowIndentation?: any,
  76. }}
  77. */
  78. let activeArgs;
  79. /**
  80. * Check for whitespace *before* a character.
  81. * @type {WhitespaceChecker}
  82. */
  83. function before({
  84. source,
  85. index,
  86. err,
  87. errTarget,
  88. lineCheckStr,
  89. onlyOneChar = false,
  90. allowIndentation = false,
  91. }) {
  92. activeArgs = {
  93. source,
  94. index,
  95. err,
  96. errTarget,
  97. onlyOneChar,
  98. allowIndentation,
  99. };
  100. switch (expectation) {
  101. case 'always':
  102. expectBefore();
  103. break;
  104. case 'never':
  105. rejectBefore();
  106. break;
  107. case 'always-single-line':
  108. if (!isSingleLineString(lineCheckStr || source)) {
  109. return;
  110. }
  111. expectBefore(messages.expectedBeforeSingleLine);
  112. break;
  113. case 'never-single-line':
  114. if (!isSingleLineString(lineCheckStr || source)) {
  115. return;
  116. }
  117. rejectBefore(messages.rejectedBeforeSingleLine);
  118. break;
  119. case 'always-multi-line':
  120. if (isSingleLineString(lineCheckStr || source)) {
  121. return;
  122. }
  123. expectBefore(messages.expectedBeforeMultiLine);
  124. break;
  125. case 'never-multi-line':
  126. if (isSingleLineString(lineCheckStr || source)) {
  127. return;
  128. }
  129. rejectBefore(messages.rejectedBeforeMultiLine);
  130. break;
  131. default:
  132. throw configurationError(`Unknown expectation "${expectation}"`);
  133. }
  134. }
  135. /**
  136. * Check for whitespace *after* a character.
  137. * @type {WhitespaceChecker}
  138. */
  139. function after({ source, index, err, errTarget, lineCheckStr, onlyOneChar = false }) {
  140. activeArgs = { source, index, err, errTarget, onlyOneChar };
  141. switch (expectation) {
  142. case 'always':
  143. expectAfter();
  144. break;
  145. case 'never':
  146. rejectAfter();
  147. break;
  148. case 'always-single-line':
  149. if (!isSingleLineString(lineCheckStr || source)) {
  150. return;
  151. }
  152. expectAfter(messages.expectedAfterSingleLine);
  153. break;
  154. case 'never-single-line':
  155. if (!isSingleLineString(lineCheckStr || source)) {
  156. return;
  157. }
  158. rejectAfter(messages.rejectedAfterSingleLine);
  159. break;
  160. case 'always-multi-line':
  161. if (isSingleLineString(lineCheckStr || source)) {
  162. return;
  163. }
  164. expectAfter(messages.expectedAfterMultiLine);
  165. break;
  166. case 'never-multi-line':
  167. if (isSingleLineString(lineCheckStr || source)) {
  168. return;
  169. }
  170. rejectAfter(messages.rejectedAfterMultiLine);
  171. break;
  172. default:
  173. throw configurationError(`Unknown expectation "${expectation}"`);
  174. }
  175. }
  176. /**
  177. * @param {WhitespaceCheckerArgs} obj
  178. */
  179. function beforeAllowingIndentation(obj) {
  180. before({ ...obj, allowIndentation: true });
  181. }
  182. /**
  183. * @param {Function} [messageFunc]
  184. */
  185. function expectBefore(messageFunc = messages.expectedBefore) {
  186. if (activeArgs.allowIndentation) {
  187. expectBeforeAllowingIndentation(messageFunc);
  188. return;
  189. }
  190. const _activeArgs = activeArgs;
  191. const source = _activeArgs.source;
  192. const index = _activeArgs.index;
  193. const oneCharBefore = source[index - 1];
  194. const twoCharsBefore = source[index - 2];
  195. if (!isValue(oneCharBefore)) {
  196. return;
  197. }
  198. if (targetWhitespace === 'space' && oneCharBefore === ' ') {
  199. if (activeArgs.onlyOneChar || !isWhitespace(twoCharsBefore)) {
  200. return;
  201. }
  202. }
  203. const msgFunc = assertFunction(messageFunc);
  204. activeArgs.err(msgFunc(activeArgs.errTarget ? activeArgs.errTarget : source[index]));
  205. }
  206. /**
  207. * @param {Function} [messageFunc]
  208. */
  209. function expectBeforeAllowingIndentation(messageFunc = messages.expectedBefore) {
  210. const _activeArgs2 = activeArgs;
  211. const source = _activeArgs2.source;
  212. const index = _activeArgs2.index;
  213. const err = _activeArgs2.err;
  214. const expectedChar = (function () {
  215. if (targetWhitespace === 'newline') {
  216. return '\n';
  217. }
  218. })();
  219. let i = index - 1;
  220. while (source[i] !== expectedChar) {
  221. if (source[i] === '\t' || source[i] === ' ') {
  222. i--;
  223. continue;
  224. }
  225. const msgFunc = assertFunction(messageFunc);
  226. err(msgFunc(activeArgs.errTarget ? activeArgs.errTarget : source[index]));
  227. return;
  228. }
  229. }
  230. /**
  231. * @param {Function} [messageFunc]
  232. */
  233. function rejectBefore(messageFunc = messages.rejectedBefore) {
  234. const _activeArgs3 = activeArgs;
  235. const source = _activeArgs3.source;
  236. const index = _activeArgs3.index;
  237. const oneCharBefore = source[index - 1];
  238. if (isValue(oneCharBefore) && isWhitespace(oneCharBefore)) {
  239. const msgFunc = assertFunction(messageFunc);
  240. activeArgs.err(msgFunc(activeArgs.errTarget ? activeArgs.errTarget : source[index]));
  241. }
  242. }
  243. /**
  244. * @param {WhitespaceCheckerArgs} obj
  245. */
  246. function afterOneOnly(obj) {
  247. after({ ...obj, onlyOneChar: true });
  248. }
  249. /**
  250. * @param {Function} [messageFunc]
  251. */
  252. function expectAfter(messageFunc = messages.expectedAfter) {
  253. const _activeArgs4 = activeArgs;
  254. const source = _activeArgs4.source;
  255. const index = _activeArgs4.index;
  256. const oneCharAfter = source[index + 1];
  257. const twoCharsAfter = source[index + 2];
  258. if (!isValue(oneCharAfter)) {
  259. return;
  260. }
  261. if (targetWhitespace === 'newline') {
  262. // If index is followed by a Windows CR-LF ...
  263. if (oneCharAfter === '\r' && twoCharsAfter === '\n') {
  264. if (activeArgs.onlyOneChar || !isWhitespace(source[index + 3])) {
  265. return;
  266. }
  267. }
  268. // If index is followed by a Unix LF ...
  269. if (oneCharAfter === '\n') {
  270. if (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter)) {
  271. return;
  272. }
  273. }
  274. }
  275. if (targetWhitespace === 'space' && oneCharAfter === ' ') {
  276. if (activeArgs.onlyOneChar || !isWhitespace(twoCharsAfter)) {
  277. return;
  278. }
  279. }
  280. const msgFunc = assertFunction(messageFunc);
  281. activeArgs.err(msgFunc(activeArgs.errTarget ? activeArgs.errTarget : source[index]));
  282. }
  283. /**
  284. * @param {Function} [messageFunc]
  285. */
  286. function rejectAfter(messageFunc = messages.rejectedAfter) {
  287. const _activeArgs5 = activeArgs;
  288. const source = _activeArgs5.source;
  289. const index = _activeArgs5.index;
  290. const oneCharAfter = source[index + 1];
  291. if (isValue(oneCharAfter) && isWhitespace(oneCharAfter)) {
  292. const msgFunc = assertFunction(messageFunc);
  293. activeArgs.err(msgFunc(activeArgs.errTarget ? activeArgs.errTarget : source[index]));
  294. }
  295. }
  296. return {
  297. before,
  298. beforeAllowingIndentation,
  299. after,
  300. afterOneOnly,
  301. };
  302. };
  303. /**
  304. * @param {any} x
  305. */
  306. function isValue(x) {
  307. return x !== undefined && x !== null;
  308. }
  309. /**
  310. * @param {unknown} x
  311. */
  312. function assertFunction(x) {
  313. if (typeof x === 'function') {
  314. return x;
  315. }
  316. throw new Error(`\`${x}\` must be a function`);
  317. }