Skip to content
Permalink
9bfb9ba527
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
66 lines (61 sloc) 1.83 KB
module.exports = str => {
const res = [];
if(!str || typeof str !== 'string') return res;
let sQuoted = false;
let dQuoted = false;
let backSlash = false;
let notEmpty = false;
let buffer = '';
str.split('').forEach((v, i, s) => {
if(sQuoted && v === `'`){
sQuoted = false;
notEmpty = true;
return;
}
if(!sQuoted && !dQuoted && !backSlash){
if(v === `'`){
sQuoted = true;
return;
}
if(v === '"'){
dQuoted = true;
return;
}
if(v === '\\'){
backSlash = true;
return;
}
if(['\b', '\f', '\n', '\r', '\t', ' '].includes(v)){
if(buffer.length > 0 || notEmpty){
res.push(buffer);
notEmpty = false;
}
buffer = '';
return;
}
}
if(!sQuoted && dQuoted && !backSlash && v === '"'){
dQuoted = false;
notEmpty = true;
return;
}
if(!sQuoted && dQuoted && !backSlash && v === '\\'){
backSlash = true;
if(['"', '`', '$', '\\'].includes(s[i + 1])){
return;
}
}
if(backSlash){
backSlash = false;
}
buffer += v;
});
if(buffer.length > 0 || notEmpty){
res.push(buffer);
notEmpty = false;
}
if(dQuoted) throw new SyntaxError('unexpected end of string while looking for matching double quote');
if(sQuoted) throw new SyntaxError('unexpected end of string while looking for matching single quote');
if(backSlash) throw new SyntaxError('unexpected end of string right after slash');
return res;
};