mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-23 20:40:34 +03:00
refactor(*): move helper fucntions from core and preset-html5 to separate package — plugin-helper
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
const {
|
||||
getChar, OPEN_BRAKET, CLOSE_BRAKET, SLASH,
|
||||
} = require('./char');
|
||||
const { getNodeLength, appendToNode } = require('./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 = 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.create = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
||||
module.exports.isOf = (node, type) => (node.tag === type);
|
||||
@@ -0,0 +1,36 @@
|
||||
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,
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
const { getChar, N } = require('./char');
|
||||
|
||||
const isTagNode = el => typeof el === 'object' && el.tag;
|
||||
const isStringNode = el => typeof el === 'string';
|
||||
|
||||
const EOL = getChar(N);
|
||||
const isEOL = el => el === EOL;
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
appendToNode,
|
||||
getNodeLength,
|
||||
isTagNode,
|
||||
isStringNode,
|
||||
isEOL,
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "@bbob/plugin-helper",
|
||||
"version": "1.0.0",
|
||||
"description": "Set of utils to help write plugins",
|
||||
"main": "lib/index.js",
|
||||
"directories": {
|
||||
"lib": "lib"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/JiLiZART/bbob.git"
|
||||
},
|
||||
"keywords": [
|
||||
"bbob",
|
||||
"plugin",
|
||||
"helper"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://github.com/JiLiZART/bbob/issues"
|
||||
},
|
||||
"homepage": "https://github.com/JiLiZART/bbob#readme",
|
||||
"scripts": {
|
||||
"test": "../../node_modules/.bin/jest --",
|
||||
"cover": "../../node_modules/.bin/jest --coverage",
|
||||
"lint": "../../node_modules/.bin/eslint ."
|
||||
},
|
||||
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
describe('@bbob/plugin-helper', () => {
|
||||
test('one', () => {
|
||||
expect(1 + 2).toBe(3)
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user