mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-17 19:21:20 +03:00
feat(*): react render support, move some helper functions to plugin-helper
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
dist
|
||||
test
|
||||
@@ -29,8 +29,9 @@ class TagNode {
|
||||
toString() {
|
||||
const OB = getChar(OPEN_BRAKET);
|
||||
const CB = getChar(CLOSE_BRAKET);
|
||||
const SL = getChar(SLASH);
|
||||
|
||||
return OB + this.tag + CB + this.content.reduce((r, node) => r + node.toString(), '') + OB + getChar(SLASH) + this.tag + CB;
|
||||
return OB + this.tag + CB + this.content.reduce((r, node) => r + node.toString(), '') + OB + SL + this.tag + CB;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const { getChar, N } = require('./char');
|
||||
|
||||
const isTagNode = el => typeof el === 'object' && el.tag;
|
||||
const isTagNode = el => typeof el === 'object' && !!el.tag;
|
||||
const isStringNode = el => typeof el === 'string';
|
||||
|
||||
const EOL = getChar(N);
|
||||
@@ -20,7 +20,23 @@ 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]() : '';
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
attrValue,
|
||||
appendToNode,
|
||||
getNodeLength,
|
||||
isTagNode,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const TagNode = require('../lib/TagNode');
|
||||
|
||||
describe('@bbob/plugin-helper/lib/TagNode', () => {
|
||||
test('create', () => {
|
||||
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);
|
||||
|
||||
expect(tagNode).toBeInstanceOf(TagNode)
|
||||
});
|
||||
|
||||
test('isOf', () => {
|
||||
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);
|
||||
|
||||
expect(TagNode.isOf(tagNode, 'test')).toBe(true);
|
||||
});
|
||||
|
||||
test('toString', () => {
|
||||
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);
|
||||
|
||||
expect(String(tagNode)).toBe('[test]Hello[/test]');
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,68 @@
|
||||
const {
|
||||
attrValue,
|
||||
appendToNode,
|
||||
getNodeLength,
|
||||
isTagNode,
|
||||
isStringNode,
|
||||
} = require('../lib');
|
||||
|
||||
describe('@bbob/plugin-helper', () => {
|
||||
test('one', () => {
|
||||
expect(1 + 2).toBe(3);
|
||||
test('appendToNode', () => {
|
||||
const value = 'test';
|
||||
const node = { content: [] };
|
||||
|
||||
appendToNode(node, value);
|
||||
expect(node.content.pop()).toBe(value);
|
||||
});
|
||||
|
||||
test('getNodeLength', () => {
|
||||
const node = {
|
||||
tag: 'test',
|
||||
content: [
|
||||
'123',
|
||||
{
|
||||
tag: 'test2',
|
||||
content: ['123']
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(getNodeLength(node)).toBe(6)
|
||||
});
|
||||
|
||||
test('isTagNode', () => {
|
||||
const node = {
|
||||
tag: 'test',
|
||||
content: []
|
||||
};
|
||||
|
||||
expect(isTagNode(node)).toBe(true)
|
||||
});
|
||||
|
||||
test('isStringNode', () => {
|
||||
const node = {
|
||||
tag: 'test',
|
||||
content: ['123']
|
||||
};
|
||||
|
||||
expect(isStringNode(node.content[0])).toBe(true);
|
||||
});
|
||||
|
||||
test('attrValue boolean', () => {
|
||||
expect(attrValue('test', true)).toBe('test');
|
||||
});
|
||||
|
||||
test('attrValue number', () => {
|
||||
expect(attrValue('test', 123)).toBe('test="123"');
|
||||
});
|
||||
|
||||
test('attrValue string', () => {
|
||||
expect(attrValue('test', 'hello')).toBe('test="hello"');
|
||||
});
|
||||
|
||||
test('attrValue object', () => {
|
||||
const attrs = { tag: 'test'};
|
||||
|
||||
expect(attrValue('test', attrs)).toBe('test="{"tag":"test"}"');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user