From 386d719626c0c958d167be24468594f20d7f2753 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 8 Nov 2018 01:27:36 +0100 Subject: linter respect --- dist/index.js | 103 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 40 deletions(-) (limited to 'dist/index.js') diff --git a/dist/index.js b/dist/index.js index f081bed..8ae7b4f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,27 +1,29 @@ -'use strict'; - -// A valid output which means nothing has been parsed. +'use strict'; // A valid output which means nothing has been parsed. // Used as error return / invalid output +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + var nothingHappend = { prop: {}, eaten: '' }; - var defaultConfig = { defaultValue: function defaultValue() { return undefined; } // Its a function -}; -// Main function +}; // Main function + function parse(value, indexNext, userConfig) { var letsEat = ''; var stopOnBrace = false; var errorDetected = false; - var config = Object.assign({}, defaultConfig, userConfig); - // Make defaultValue a function if it isn't + var config = _objectSpread({}, defaultConfig, userConfig); // Make defaultValue a function if it isn't + + if (typeof config.defaultValue !== 'function') { var defaultValue = config.defaultValue; @@ -31,17 +33,17 @@ function parse(value, indexNext, userConfig) { } var prop = {}; - /* They are at least one label and at best two */ + /* ekqsdf <- one label * qsdfqsfd=qsdfqsdf <- two */ + var labelFirst = ''; - var labelSecond = void 0; + var labelSecond; if (indexNext === undefined) { indexNext = 0; } - /* 3 types : * .azcv <- class * #poi <- id @@ -49,23 +51,26 @@ function parse(value, indexNext, userConfig) { * jkj <- this is also a key but with a user defined value (default is undefined) * jkj= <- this is also a key but with a empty value */ - var type = void 0; - var forbidenCharacters = '\n\r{}'; - // A function that detect if it's time to end the parsing + + var type; + var forbidenCharacters = '\n\r{}'; // A function that detect if it's time to end the parsing + var shouldStop = function shouldStop() { if (indexNext >= value.length || forbidenCharacters.indexOf(value[indexNext]) > -1) { if (stopOnBrace && value[indexNext] !== '}') { errorDetected = true; } + return true; } + return value[indexNext] === '}' && stopOnBrace; }; - var eaten = ''; - // Couple of functions that parse same kinds of characters + var eaten = ''; // Couple of functions that parse same kinds of characters // Used to parse spaces or identifiers + var eat = function eat(chars) { eaten = ''; @@ -77,6 +82,7 @@ function parse(value, indexNext, userConfig) { return shouldStop(); }; + var eatUntil = function eatUntil(chars) { eaten = ''; @@ -84,10 +90,10 @@ function parse(value, indexNext, userConfig) { letsEat += value.charAt(indexNext); eaten += value.charAt(indexNext); indexNext++; - } - - // Ugly but keep the main loop readable + } // Ugly but keep the main loop readable // Set the label it should set + + if (labelFirst) { labelSecond = eaten; } else { @@ -95,13 +101,13 @@ function parse(value, indexNext, userConfig) { } return shouldStop(); - }; - - // In quote, every character is valid except the unescaped quotes and CR or LF + }; // In quote, every character is valid except the unescaped quotes and CR or LF // Same function for single and double quote + + var eatInQuote = function eatInQuote(quote) { - eaten = ''; - // First check so value[indexNext-1] will always be valid + eaten = ''; // First check so value[indexNext-1] will always be valid + if (value[indexNext] === quote) { return; } @@ -110,15 +116,16 @@ function parse(value, indexNext, userConfig) { letsEat += value.charAt(indexNext); eaten += value.charAt(indexNext); indexNext++; - } - // If we encounter an EOL, there is an error + } // If we encounter an EOL, there is an error // We are waiting for a quote + + if (value[indexNext] === '\n' || value[indexNext] === '\r' || indexNext >= value.length) { errorDetected = true; return true; - } + } // Ugly but keep the main loop readable + - // Ugly but keep the main loop readable if (labelFirst) { labelSecond = eaten.replace(/\\"/g, '"'); } else { @@ -126,18 +133,17 @@ function parse(value, indexNext, userConfig) { } return shouldStop(); - }; + }; // It's really common to eat only one character so let's make it a function + - // It's really common to eat only one character so let's make it a function var eatOne = function eatOne(c, skipStopCheck) { // Miam ! letsEat += c; indexNext++; - return skipStopCheck ? false : shouldStop(); - }; + }; // Common parsing of quotes + - // Common parsing of quotes var eatQuote = function eatQuote(q) { eatOne(q, true); eatInQuote(q, true); @@ -145,6 +151,7 @@ function parse(value, indexNext, userConfig) { if (value.charAt(indexNext) !== q) { return nothingHappend; } + if (eatOne(q)) { return -1; } @@ -156,6 +163,7 @@ function parse(value, indexNext, userConfig) { // ID prop.id = prop.id || labelFirst; break; + case 'class': if (!prop.class) { prop.class = []; @@ -166,10 +174,12 @@ function parse(value, indexNext, userConfig) { } break; + case 'key': if (!labelFirst) { return nothingHappend; } + if (!(labelFirst in prop)) { if (labelSecond === undefined) { // Here, we have an attribute without value @@ -179,17 +189,20 @@ function parse(value, indexNext, userConfig) { prop[labelFirst] = labelFirst === 'class' ? [labelSecond] : labelSecond; } } + break; + default: } + type = undefined; labelFirst = ''; labelSecond = undefined; }; - /** *********************** Start parsing ************************ */ - // Let's check for leading spaces first + + eat(' \t\v'); if (value[indexNext] === '{') { @@ -206,6 +219,7 @@ function parse(value, indexNext, userConfig) { if (value.charAt(indexNext) === '.') { // Classes type = 'class'; + if (eatOne('.')) { errorDetected = true; break; @@ -213,6 +227,7 @@ function parse(value, indexNext, userConfig) { } else if (value.charAt(indexNext) === '#') { // ID type = 'id'; + if (eatOne('#')) { errorDetected = true; break; @@ -220,12 +235,13 @@ function parse(value, indexNext, userConfig) { } else { // Key type = 'key'; - } + } // Extract name + - // Extract name if (eatUntil('=\t\b\v  ') || !labelFirst) { break; } + if (value.charAt(indexNext) === '=' && type === 'key') { // Set labelSecond if (eatOne('=')) { @@ -234,6 +250,7 @@ function parse(value, indexNext, userConfig) { if (value.charAt(indexNext) === '"') { var ret = eatQuote('"'); + if (ret === -1) { break; } else if (ret === nothingHappend) { @@ -241,6 +258,7 @@ function parse(value, indexNext, userConfig) { } } else if (value.charAt(indexNext) === '\'') { var _ret = eatQuote('\''); + if (_ret === -1) { break; } else if (_ret === nothingHappend) { @@ -249,12 +267,14 @@ function parse(value, indexNext, userConfig) { } else if (eatUntil(' \t\n\r\v=}')) { break; } - } + } // Add the parsed attribute to the output prop with the ad hoc type + - // Add the parsed attribute to the output prop with the ad hoc type addAttribute(); } + addAttribute(); + if (stopOnBrace) { if (indexNext < value.length && value[indexNext] === '}') { stopOnBrace = false; @@ -264,7 +284,10 @@ function parse(value, indexNext, userConfig) { } } - return errorDetected ? nothingHappend : { prop: prop, eaten: letsEat }; + return errorDetected ? nothingHappend : { + prop: prop, + eaten: letsEat + }; } module.exports = parse; \ No newline at end of file -- cgit v1.2.3