mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-11 18:02:26 +03:00
refactor(*): convert to babel and generation to lib, es, dist folders (#2)
* refactor(*): convert to babel and generation to lib, es, dist * chore(*): remove generated files * fix(*): lint run command
This commit is contained in:
committed by
GitHub
parent
d22a2895a4
commit
32a7fb51da
@@ -0,0 +1,39 @@
|
||||
import { OPEN_BRAKET, CLOSE_BRAKET, SLASH } from './char';
|
||||
import { getNodeLength, appendToNode } from './index';
|
||||
|
||||
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 = OPEN_BRAKET;
|
||||
const CB = CLOSE_BRAKET;
|
||||
|
||||
return OB + this.tag + CB + this.content.reduce((r, node) => r + node.toString(), '') + OB + SLASH + this.tag + CB;
|
||||
}
|
||||
}
|
||||
|
||||
TagNode.create = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
||||
TagNode.isOf = (node, type) => (node.tag === type);
|
||||
|
||||
export { TagNode };
|
||||
export default TagNode;
|
||||
@@ -0,0 +1,35 @@
|
||||
const N = '\n';
|
||||
const TAB = '\t';
|
||||
const F = '\f';
|
||||
const R = '\r';
|
||||
|
||||
const EQ = '=';
|
||||
const QUOTEMARK = '"';
|
||||
const SPACE = ' ';
|
||||
|
||||
const OPEN_BRAKET = '[';
|
||||
const CLOSE_BRAKET = ']';
|
||||
|
||||
const SLASH = '/';
|
||||
const BACKSLASH = '\\';
|
||||
|
||||
const PLACEHOLDER_SPACE_TAB = ' ';
|
||||
const PLACEHOLDER_SPACE = ' ';
|
||||
|
||||
// const getChar = String.fromCharCode;
|
||||
|
||||
export {
|
||||
N,
|
||||
F,
|
||||
R,
|
||||
TAB,
|
||||
EQ,
|
||||
QUOTEMARK,
|
||||
SPACE,
|
||||
OPEN_BRAKET,
|
||||
CLOSE_BRAKET,
|
||||
SLASH,
|
||||
PLACEHOLDER_SPACE_TAB,
|
||||
PLACEHOLDER_SPACE,
|
||||
BACKSLASH,
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
import { N } from './char';
|
||||
|
||||
const isTagNode = el => typeof el === 'object' && !!el.tag;
|
||||
const isStringNode = el => typeof el === 'string';
|
||||
const isEOL = el => el === N;
|
||||
|
||||
const getNodeLength = (node) => {
|
||||
if (isTagNode(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);
|
||||
};
|
||||
|
||||
const escapeQuote = value => value.replace(/"/g, '"');
|
||||
|
||||
const attrValue = (name, value) => {
|
||||
const type = typeof value;
|
||||
|
||||
const types = {
|
||||
boolean: () => (value ? `${name}` : ''),
|
||||
number: () => `${name}="${value}"`,
|
||||
string: () => `${name}="${escapeQuote(value)}"`,
|
||||
object: () => `${name}="${escapeQuote(JSON.stringify(value))}"`,
|
||||
};
|
||||
|
||||
return types[type] ? types[type]() : '';
|
||||
};
|
||||
|
||||
export {
|
||||
attrValue,
|
||||
appendToNode,
|
||||
getNodeLength,
|
||||
isTagNode,
|
||||
isStringNode,
|
||||
isEOL,
|
||||
};
|
||||
Reference in New Issue
Block a user