Skip to content
Snippets Groups Projects
Commit 534dea34 authored by mcmlxxix's avatar mcmlxxix
Browse files

add combine() function for combining two objects. support objects other than...

add combine() function for combining two objects. support objects other than console in pushMessage(). add regExp to removeSpaces(), remove stupid.
parent 4e17ecf4
No related branches found
No related tags found
No related merge requests found
...@@ -150,8 +150,7 @@ function removeFirstWord(text) ...@@ -150,8 +150,7 @@ function removeFirstWord(text)
} }
function removeSpaces(text) function removeSpaces(text)
{ {
while(text[0]==" ") text=text.substr(1); return text.replace(/^\s*/,"").replace(/\s*$/,"");
return truncsp(text);
} }
function splitPadded(string1,string2,length,padding) function splitPadded(string1,string2,length,padding)
{ {
...@@ -174,8 +173,7 @@ function printPadded(string,length,padding,justification) ...@@ -174,8 +173,7 @@ function printPadded(string,length,padding,justification)
if(!padding) padding=" "; if(!padding) padding=" ";
if(!justification) justification="left"; if(!justification) justification="left";
var strlength=console.strlen(string); var strlength=console.strlen(string);
if(strlength>length) if(strlength>length) {
{
string=string.substring(0,length); string=string.substring(0,length);
} }
var padlength=length-strlength; var padlength=length-strlength;
...@@ -190,14 +188,12 @@ function centerString(string,length,padding) ...@@ -190,14 +188,12 @@ function centerString(string,length,padding)
{ {
if(!padding) padding=" "; if(!padding) padding=" ";
var strlength=console.strlen(string); var strlength=console.strlen(string);
if(strlength>length) if(strlength>length) {
{
string=string.substring(0,length); string=string.substring(0,length);
} }
var padlength=length-strlength; var padlength=length-strlength;
var newstring=string; var newstring=string;
for(p=1;p<=padlength;p++) for(p=1;p<=padlength;p++) {
{
if(p%2==0) newstring+=padding; if(p%2==0) newstring+=padding;
else newstring=padding+newstring; else newstring=padding+newstring;
} }
...@@ -224,13 +220,11 @@ function strlen(str) ...@@ -224,13 +220,11 @@ function strlen(str)
} }
function drawLine(x,y,length,color) function drawLine(x,y,length,color)
{ {
if(x && y) if(x && y) {
{
console.gotoxy(x,y); console.gotoxy(x,y);
if(y==24 && x+length>80) length-=(length-80); if(y==24 && x+length>80) length-=(length-80);
} }
for(i=0;i<length;i++) for(i=0;i<length;i++) {
{
if(color) console.attributes=color; if(color) console.attributes=color;
console.putmsg("\xc4",P_SAVEATR); console.putmsg("\xc4",P_SAVEATR);
} }
...@@ -239,11 +233,10 @@ function clearBlock(x,y,w,h,bg) ...@@ -239,11 +233,10 @@ function clearBlock(x,y,w,h,bg)
{ {
console.attributes=bg?bg:ANSI_NORMAL; console.attributes=bg?bg:ANSI_NORMAL;
console.gotoxy(x,y); console.gotoxy(x,y);
for(line=0;line<h;line++) for(var l=0;l<h;l++) {
{
console.pushxy(); console.pushxy();
clearLine(w); clearLine(w);
if(line<h-1) { if(l<h-1) {
console.popxy(); console.popxy();
console.down(); console.down();
} }
...@@ -325,15 +318,100 @@ function setPosition(x,y) ...@@ -325,15 +318,100 @@ function setPosition(x,y)
console.gotoxy(x,y); console.gotoxy(x,y);
console.pushxy(); console.pushxy();
} }
function pushMessage(txt) function pushMessage(txt,container)
{ {
console.popxy(); if(!container)
console.putmsg(txt,P_SAVEATR); container = console;
console.popxy(); container.popxy();
console.down(); container.putmsg(txt,P_SAVEATR);
console.pushxy(); container.popxy();
container.down();
container.pushxy();
} }
function pushxy(name)
{
if(!js.global.saved_coords)
js.global.saved_coords=[];
if(name)
js.global.saved_coords[name]=console.getxy();
else
console.pushxy();
}
function popxy(name)
{
if(name)
if(js.global.saved_coords[name])
console.gotoxy(js.global.saved_coords[name]);
else
console.popxy();
}
function combine(obj1,obj2,concat_str)
{
var newobj = {};
if(obj2 instanceof Array)
newobj=[];
else
newobj={};
for(var i in obj1) {
switch(typeof obj1[i]) {
case "object":
newobj[i] = combine(newobj[i],obj1[i],concat_str);
break;
default:
newobj[i] = obj1[i];
break;
}
}
for(var i in obj2) {
switch(typeof obj2[i]) {
case "function":
if(!newobj[i])
newobj[i]=obj2[i];
else
newobj["_"+i]=obj2[i];
break;
case "string":
if(!newobj[i])
newobj[i]=obj2[i];
else if(newobj instanceof Array) {
if(concat_str)
newobj[i] += obj2[i];
else
newobj.push(obj2[i]);
}
else {
if(concat_str && typeof newobj[i] == "string")
newobj[i] += obj2[i];
else
newobj["_"+i]=obj2[i];
}
break;
case "number":
if(newobj[i]) {
if(typeof newobj[i] == "number" || !isNaN(obj[i]))
newobj[i] += obj2[i];
else if(newobj instanceof Array)
newobj.push(obj2[i]);
else
newobj["_"+i]=obj2[i];
}
else
newobj[i] = obj2[i];
break;
case "object":
if(typeof newobj[i] == "object")
newobj[i] = combine(newobj[i],obj2[i],concat_str);
else if(newobj[i]) {
newobj["_"+i] = {};
newobj = combine(newobj["_"+i],obj2[i],concat_str);
}
else
newobj[i] = obj2[i];
break;
}
}
return newobj;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment