mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-20 20:00:33 +03:00
refactor(*): move helper fucntions from core and preset-html5 to separate package — plugin-helper
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
dist
|
||||
test
|
||||
rollup.config.js
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
const {
|
||||
getChar, N, CLOSE_BRAKET, OPEN_BRAKET, SLASH,
|
||||
} = require('./char');
|
||||
|
||||
const EOL = getChar(N);
|
||||
const isNode = el => typeof el === 'object' && el.tag;
|
||||
const isStringNode = el => typeof el === 'string';
|
||||
const isEOL = el => el === EOL;
|
||||
|
||||
const getNodeLength = (node) => {
|
||||
if (isNode(node)) {
|
||||
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
|
||||
} else if (isStringNode(node)) {
|
||||
return node.length;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
const appendToNode = (node, value) => {
|
||||
node.content.push(value);
|
||||
};
|
||||
|
||||
class TagNode {
|
||||
constructor(tag, attrs, content) {
|
||||
this.tag = tag;
|
||||
this.attrs = attrs;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
attr(name, value) {
|
||||
if (typeof value !== 'undefined') {
|
||||
this.attrs[name] = value;
|
||||
}
|
||||
|
||||
return this.attrs[name];
|
||||
}
|
||||
|
||||
append(value) {
|
||||
return appendToNode(this, value);
|
||||
}
|
||||
|
||||
get length() {
|
||||
return getNodeLength(this);
|
||||
}
|
||||
|
||||
toString() {
|
||||
const OB = getChar(OPEN_BRAKET);
|
||||
const CB = getChar(CLOSE_BRAKET);
|
||||
|
||||
return OB + this.tag + CB + this.content.reduce((r, node) => r + node.toString(), '') + OB + getChar(SLASH) + this.tag + CB;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TagNode;
|
||||
module.exports.isNode = isNode;
|
||||
module.exports.isStringNode = isStringNode;
|
||||
module.exports.isEOL = isEOL;
|
||||
module.exports.appendToNode = appendToNode;
|
||||
@@ -3,7 +3,7 @@ const {
|
||||
OPEN_BRAKET,
|
||||
CLOSE_BRAKET,
|
||||
SLASH,
|
||||
} = require('./char');
|
||||
} = require('@bbob/plugin-helper/lib/char');
|
||||
|
||||
// type, value, line, row,
|
||||
const TOKEN_TYPE_ID = 'type'; // 0;
|
||||
|
||||
@@ -5,7 +5,7 @@ const {
|
||||
PLACEHOLDER_SPACE, PLACEHOLDER_SPACE_TAB,
|
||||
SLASH,
|
||||
BACKSLASH,
|
||||
} = require('./char');
|
||||
} = require('@bbob/plugin-helper/lib/char');
|
||||
const Token = require('./Token');
|
||||
|
||||
const createTokenOfType = (type, value, line, row) => new Token(type, value, line, row);
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
const N = '\n'.charCodeAt(0);
|
||||
const TAB = '\t'.charCodeAt(0);
|
||||
const F = '\f'.charCodeAt(0);
|
||||
const R = '\r'.charCodeAt(0);
|
||||
|
||||
const EQ = '='.charCodeAt(0);
|
||||
const QUOTEMARK = '"'.charCodeAt(0);
|
||||
const SPACE = ' '.charCodeAt(0);
|
||||
|
||||
const OPEN_BRAKET = '['.charCodeAt(0);
|
||||
const CLOSE_BRAKET = ']'.charCodeAt(0);
|
||||
|
||||
const SLASH = '/'.charCodeAt(0);
|
||||
const BACKSLASH = '\\'.charCodeAt(0);
|
||||
|
||||
const PLACEHOLDER_SPACE_TAB = ' ';
|
||||
const PLACEHOLDER_SPACE = ' ';
|
||||
|
||||
const getChar = String.fromCharCode;
|
||||
|
||||
module.exports = {
|
||||
getChar,
|
||||
N,
|
||||
F,
|
||||
R,
|
||||
TAB,
|
||||
EQ,
|
||||
QUOTEMARK,
|
||||
SPACE,
|
||||
OPEN_BRAKET,
|
||||
CLOSE_BRAKET,
|
||||
SLASH,
|
||||
PLACEHOLDER_SPACE_TAB,
|
||||
PLACEHOLDER_SPACE,
|
||||
BACKSLASH,
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
const Tokenizer = require('./Tokenizer');
|
||||
const TagNode = require('./TagNode');
|
||||
const TagNode = require('@bbob/plugin-helper/lib/TagNode');
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -28,14 +28,6 @@ let tokenizer = null;
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
let tokens = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tag
|
||||
* @param attrs
|
||||
* @param content
|
||||
*/
|
||||
const newTagNode = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
||||
|
||||
const createTokenizer = (input, onToken) => new Tokenizer(input, { onToken });
|
||||
|
||||
/**
|
||||
@@ -56,7 +48,7 @@ const getTagNode = () => (tagNodes.length ? tagNodes[tagNodes.length - 1] : null
|
||||
* @param {Token} token
|
||||
* @return {Array}
|
||||
*/
|
||||
const createTagNode = token => tagNodes.push(newTagNode(token.getValue()));
|
||||
const createTagNode = token => tagNodes.push(TagNode.create(token.getValue()));
|
||||
/**
|
||||
* @private
|
||||
* @param {Token} token
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
"html"
|
||||
],
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"@bbob/plugin-helper": "^1.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/JiLiZART/bbob.git"
|
||||
|
||||
Reference in New Issue
Block a user