Reputation: 1273
I am working with Node.Js and have the below block of code to pull a string from a Database location, strips off certain junk and sets it up to be uniform, so I can update another table with this information later.
jsUpdateCon.query('SELECT FileContent FROM codeFile WHERE ID = ?',[msg[1]], function(err, result){
if (err) throw err;
str = result[0].FileContent;
var firstInstance = str.search(/=/) + 2;
var lastInstance = str.lastIndexOf('\\') + 1;
str = str.substring(firstInstance, lastInstance);
str = str.replace(/\\n\\/g, "");
str = str.replace(/\#(.*)/g, "");
str = str.replace(/\n\s*\n/g, "\n");
str = str.replace(/snippet /g, "\nsnippet ");
str = str.replace(/^(?:\t)/gm, "");
console.log(str);
});
Here is a sample of the output from console.log(str);
snippet cfabort
<cfabort>
snippet cfargument
<cfargument name=\"${1:ArgName}\" type=\"${2:any}\" default=\"${3:DefaultValue}\" hint=\"${4:hint about this argument}\">
snippet cfbreak
<cfbreak>
snippet cfcase
<cfcase value=\"${1:${SELECTED_TEXT:<!--- code --->}}\">
${0}
</cfcase>
snippet cfcatch
<cfcatch>
${0}
</cfcatch>
snippet cfcatch:type
<cfcatch type=\"${1:any}\">
${0}
</cfcatch>
snippet cfcomponent
<cfcomponent>
${0:<!--- code --->}
</cfcomponent>
snippet cfcontent
<cfcontent deleteFile=\"${1:no}\" file=\"${2:filename}\" reset=\"${3:yes}\" type=\"${4:fileType}\" variable=\"${5:variableName}\">
snippet cfcontinue
<cfcontinue>
snippet cfdefaultcase
<cfdefaultcase>
snippet cfdirectory:c
<cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:copy}\" destination=\"${3:destinationPath}\">
snippet cfdirectory:cr
<cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:create}\" >
snippet cfdirectory:d
<cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:delete}\" recurse=\"${3:yes|no}\">
snippet cfdirectory:l
<cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:list}\" name=\"${3:nameOfOutputSet}\" recurse=\"${4:yes|no}\" sort=\"${3:asc|desc}\" >
snippet cfdirectory:r
<cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:rename}\" newDirectory=\"${3:newNameforDirectory}\">
snippet cfdump
<cfdump var=\"
snippet cfelse
<cfelse>
${0:<!--- code --->}
I am now needing to parse this string to insert into a database table.
Any line beginning with snippet[space] (such as "snippet cfabort") -> insert into keyboardShortcuts.keyShort such as "cfabort"(removing the snippet[space]).
Every line after (excluding the blank line between groups) -> insert into keyboardShortcuts.snippet such as "<cfabort>" keeping formatting of the group.
And then breaking on a blank line to start the next row in the DB table with the next snippet.
Do I use the readline from NodeJs? This appears to only use a file stream not a string in memory.
Do I do use something else? What is the best way to accomplish this?
Current Working Code with Abdullah Shahin's answer:
jsUpdateCon.query('SELECT FileContent FROM codeFile WHERE ID = ?',[msg[1]], function(err, result){
if (err) throw err;
str = result[0].FileContent;
var firstInstance = str.search(/=/) + 2;
var lastInstance = str.lastIndexOf('\\') + 1;
str = str.substring(firstInstance, lastInstance);
str = str.replace(/\\"/g, "\"");
str = str.replace(/\</g, "<");
str = str.replace(/\>/g, ">");
str = str.replace(/\\n\\/g, "");
str = str.replace(/^\#(.*)/gm, "");
str = str.replace(/\n\s*\n/g, "\n");
str = str.replace(/^(?:\t)/gm, "");
str = str.trim();
str = str.replace(/^\s*[\r\n]/gm,"");
var object = {};
var tempArray = str.split("\n");
var currentObj = "";
for(var i=0; i<tempArray.length;i++){
if(/snippet /g.test(tempArray[i])){
currentObj = tempArray[i].replace(/snippet /g, "");
object[currentObj] = "";
} else {
object[currentObj] += tempArray[i];
}
}
});
Upvotes: 0
Views: 5411
Reputation: 1052
hope this helps, this code will parse it into an object, all snippets are the keys and you can find all values for a specific snippet match for its key
var object = {};
text = text.replace(/^\s*[\r\n]/gm,"");
var array = text.split("\n");
var currentObj = "";
for(var i=0;i<array.length;i++)
{
if(/snippet /g.test(array[i]))
{
currentObj = array[i].replace(/snippet /g,"");
object[currentObj] = "";
}
else
{
object[currentObj] += array[i];
}
}
console.log(object)
this is the output
{
cfabort: '<cfabort>',
cfargument: '<cfargument name=\\"${1:ArgName}\\" type=\\"${2:any}\\" default=\\"${3:DefaultValue}\\" hint=\\"${4:hint about this argument}\\">',
cfbreak: '<cfbreak>',
cfcase: '<cfcase value=\\"${1:${SELECTED_TEXT:<!--- code --->}}\\"> ${0}</cfcase>',
cfcatch: '<cfcatch> ${0}</cfcatch>',
'cfcatch:type': '<cfcatch type=\\"${1:any}\\"> ${0}</cfcatch>',
cfcomponent: '<cfcomponent> ${0:<!--- code --->}</cfcomponent>',
cfcontent: '<cfcontent deleteFile=\\"${1:no}\\" file=\\"${2:filename}\\" reset=\\"${3:yes}\\" type=\\"${4:fileType}\\" variable=\\"${5:variableName}\\">',
cfcontinue: '<cfcontinue>',
cfdefaultcase: '<cfdefaultcase>'
}
Upvotes: 1