Skip to content

Improve bundle size #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,45 @@
/* Expose. */
module.exports = parse;

/* Characters */
var dot = '.'.charCodeAt(0);
var hash = '#'.charCodeAt(0);

/* Parse a simple CSS selector into a HAST node. */
function parse(selector) {
var id = null;
var index = 0;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any value? is it so important? it add bytes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using charCodeAt is faster than charCode or bracket notation.
And this is code that could be run a lot, so it could matter a little.

var className = [];
var value = selector || '';
var name = 'div';
var node;
var type = null;
var index = -1;
var code;
var length = value.length;
var subvalue;

var type;
var lastIndex;

node = {
var node = {
type: 'element',
tagName: null,
tagName: 'div',
properties: {},
children: []
};

type = null;
selector = selector || '';

while (++index <= length) {
code = value.charCodeAt(index);
while (index <= selector.length) {
var ch = selector[index++];

if (!code || code === dot || code === hash) {
subvalue = value.slice(lastIndex, index);
if (!ch || ch === '.' || ch === '#') {
var subvalue = selector.slice(lastIndex, index - 1);

if (subvalue) {
if (type === dot) {
if (type === '.') {
className.push(subvalue);
} else if (type === hash) {
id = subvalue;
} else if (type === '#') {
node.properties.id = subvalue;
} else {
name = subvalue;
node.tagName = subvalue;
}
}

lastIndex = index + 1;
type = code;
lastIndex = index;
type = ch;
}
}

node.tagName = name;

if (id) {
node.properties.id = id;
}

if (className.length !== 0) {
if (className.length) { // eslint-disable-line unicorn/explicit-length-check
node.properties.className = className;
}

Expand Down