2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-02 16:04:04 +03:00

refactor(parser): better jsdoc, some behavior fixes, more tests

— all operations on nodes moved to `createList` function
- fixed problem with single tags with value only like `[url=value]` fixes #6
- write tests for `Token` class
- moved all node arrays to parse func, now parser supports many instances
- add jsdoc to critical parts of the parser to better understanding how it works
This commit is contained in:
Nikolay Kostyurin
2019-03-04 02:24:12 +02:00
parent ef6a778f45
commit 8cb1d495dd
7 changed files with 379 additions and 220 deletions
+52
View File
@@ -76,10 +76,12 @@ export const createCharGrabber = (source, { onSkip } = {}) => {
*/
export const trimChar = (str, charToRemove) => {
while (str.charAt(0) === charToRemove) {
// eslint-disable-next-line no-param-reassign
str = str.substring(1);
}
while (str.charAt(str.length - 1) === charToRemove) {
// eslint-disable-next-line no-param-reassign
str = str.substring(0, str.length - 1);
}
@@ -92,3 +94,53 @@ export const trimChar = (str, charToRemove) => {
* @return {String}
*/
export const unquote = str => str.replace(BACKSLASH + QUOTEMARK, QUOTEMARK);
/**
* @typedef {Object} ItemList
* @type {Object}
* @property {getLastCb} getLast
* @property {flushLastCb} flushLast
* @property {pushCb} push
* @property {toArrayCb} toArray
*/
/**
*
* @param values
* @return {ItemList}
*/
export const createList = (values = []) => {
const nodes = values;
/**
* @callback getLastCb
*/
const getLast = () => (nodes.length ? nodes[nodes.length - 1] : null);
/**
* @callback flushLastCb
* @return {*}
*/
const flushLast = () => {
if (nodes.length) {
return nodes.pop();
}
return false;
};
/**
* @callback pushCb
* @param value
*/
const push = value => nodes.push(value);
/**
* @callback toArrayCb
* @return {Array}
*/
return {
getLast,
flushLast,
push,
toArray: () => nodes,
};
};