Skip to content

Commit

Permalink
feat(plugin-helper): move getUniqAttr from preset to plugin helper (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART authored Apr 12, 2020
1 parent 2cfe729 commit f28f19e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/bbob-plugin-helper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,23 @@ const attrsToString = (values) => {
.join(' ');
};

/**
* Gets value from
* @example
* getUniqAttr({ 'foo': true, 'bar': bar' }) => 'bar'
* @param attrs
* @returns {string}
*/
const getUniqAttr = (attrs) => Object
.keys(attrs)
.reduce((res, key) => (attrs[key] === key ? attrs[key] : null), null);

export {
attrsToString,
attrValue,
appendToNode,
getNodeLength,
getUniqAttr,
isTagNode,
isStringNode,
isEOL,
Expand Down
13 changes: 11 additions & 2 deletions packages/bbob-plugin-helper/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
attrValue,
appendToNode,
getNodeLength,
getUniqAttr,
isTagNode,
isStringNode,
isEOL,
Expand Down Expand Up @@ -78,9 +79,17 @@ describe('@bbob/plugin-helper', () => {
foo: 'bar',
disabled: true
})).toBe(` tag="test" foo="bar" disabled`)
})
});

test('attrsToString undefined', () => {
expect(attrsToString(undefined)).toBe('')
})
});

test('getUniqAttr with unq attr', () => {
expect(getUniqAttr({foo: true, 'http://bar.com': 'http://bar.com'})).toBe('http://bar.com')
});

test('getUniqAttr without unq attr', () => {
expect(getUniqAttr({foo: true})).toBe(null)
})
});
6 changes: 1 addition & 5 deletions packages/bbob-preset-html5/src/defaultTags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-plusplus,no-lonely-if */
import { isStringNode, isTagNode } from '@bbob/plugin-helper';
import { isStringNode, isTagNode, getUniqAttr } from '@bbob/plugin-helper';
import TagNode from '@bbob/plugin-helper/lib/TagNode';

const isStartsWith = (node, type) => (node[0] === type);
Expand Down Expand Up @@ -55,10 +55,6 @@ const asListItems = (content) => {
return [].concat(listItems);
};

const getUniqAttr = (attrs) => Object
.keys(attrs)
.reduce((res, key) => (attrs[key] === key ? attrs[key] : null), null);

export default {
b: (node) => ({
tag: 'span',
Expand Down

0 comments on commit f28f19e

Please sign in to comment.