From f2a6ad43e080724d4a1580486c7e7d21b35e5233 Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Thu, 9 Feb 2017 17:20:22 +0200 Subject: [PATCH 1/2] smaller size --- index.js | 50 +++++++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 8059642..9affcc1 100644 --- a/index.js +++ b/index.js @@ -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; 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) { node.properties.className = className; } From e2efaaf4478cfd83b5d92d2fb27841842be5ed3f Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Thu, 9 Feb 2017 17:31:56 +0200 Subject: [PATCH 2/2] fix linting --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9affcc1..a75044e 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ function parse(selector) { children: [] }; - selector = selector || '' + selector = selector || ''; while (index <= selector.length) { var ch = selector[index++]; @@ -41,7 +41,7 @@ function parse(selector) { } } - if (className.length) { + if (className.length) { // eslint-disable-line unicorn/explicit-length-check node.properties.className = className; }