From 74781992fb10684408e628afca89f072182a5cf5 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 30 Oct 2020 14:12:27 +0100 Subject: [PATCH] Refactor to improve bundle size --- index.js | 71 +++++++++++++++++++++++----------------------------- package.json | 12 +++++++-- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index ac40175..459e1d0 100644 --- a/index.js +++ b/index.js @@ -2,65 +2,56 @@ module.exports = x -var slice = [].slice - // Creating xast elements. function x(name, attributes) { - var attrs = {} - var childrenIndex = 2 - var attribute - var value - var node + var node = {type: 'element', name: name, attributes: {}, children: []} + var index = 1 + var key - if (typeof name !== 'string' || name === '') { + if (typeof name !== 'string' || !name) { throw new Error('Expected element name, got `' + name + '`') } - node = {type: 'element', name: name, attributes: attrs, children: []} - - // Note that we do not accept a node instead of attributes. - if ( - typeof attributes === 'number' || - typeof attributes === 'string' || - (attributes && 'length' in attributes) - ) { - childrenIndex = 1 - attributes = null - } - - if (attributes !== null && attributes !== undefined) { - for (attribute in attributes) { - value = attributes[attribute] - - // Ignore nullish and NaN values. - if (value !== null && value !== undefined && value === value) { - attrs[attribute] = String(value) + // Handle props. + if (attributes) { + if ( + typeof attributes === 'string' || + typeof attributes === 'number' || + 'length' in attributes + ) { + // Nope, it’s something for `children`. + index-- + } else { + for (key in attributes) { + // Ignore nullish and NaN values. + if (attributes[key] != null && attributes[key] === attributes[key]) { + node.attributes[key] = String(attributes[key]) + } } } } - add(node.children, slice.call(arguments, childrenIndex)) + // Handle children. + while (++index < arguments.length) { + addChild(node.children, arguments[index]) + } return node } -function add(siblings, value) { - var index - var length +function addChild(nodes, value) { + var index = -1 - if (value === null || value === undefined) { + if (value == null) { // Empty. } else if (typeof value === 'string' || typeof value === 'number') { - siblings.push({type: 'text', value: String(value)}) + nodes.push({type: 'text', value: String(value)}) } else if (typeof value === 'object' && 'length' in value) { - index = -1 - length = value.length - - while (++index < length) { - add(siblings, value[index]) + while (++index < value.length) { + addChild(nodes, value[index]) } - } else if (typeof value === 'object' && typeof value.type === 'string') { - siblings.push(value) + } else if (typeof value === 'object' && value.type) { + nodes.push(value) } else { throw new TypeError('Expected node, nodes, string, got `' + value + '`') } diff --git a/package.json b/package.json index 2682573..aa818de 100644 --- a/package.json +++ b/package.json @@ -67,9 +67,17 @@ "prettier": true, "esnext": false, "rules": { - "unicorn/prefer-number-properties": "off", + "eqeqeq": [ + "error", + "always", + { + "null": "ignore" + } + ], + "guard-for-in": "off", + "no-eq-null": "off", "no-self-compare": "off", - "guard-for-in": "off" + "unicorn/prefer-number-properties": "off" } }, "remarkConfig": {