aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-07-17 17:48:10 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-07-17 17:48:10 -0400
commit0a815292b98f54741e809ca97043ac898fa559b9 (patch)
tree1352a5a665b2f4171bf8d2543edfd6d6d6fef98a
parent5bb7455d5e16b38ab03504c2ef79d4d74b2dbb1b (diff)
Space made into a real class
-rw-r--r--space.js37
1 files changed, 19 insertions, 18 deletions
diff --git a/space.js b/space.js
index 9125c54..b0ffce9 100644
--- a/space.js
+++ b/space.js
@@ -20,10 +20,12 @@ function replace(text, old, repl){ //replaces, in an array `text`, the instances
return text;
}
-function Space(){
- this.data = [];
- this.loc = []; // Coordinates to represent topleft location of spaces, and aid with .comb
- this.fromfetch = function(tiles, dimension, conform=true){ //tiles is straight from fetch/tileUpdate function, dimension is a min/max pair of y/x coordinates; conform is false for tileUpdate because the cell_props don't actually mean anything; all data is still included
+class Space{
+ constructor(){
+ this.data = [];
+ this.loc = [];
+ }
+ fromfetch(tiles, dimension, conform=true){ //tiles is straight from fetch/tileUpdate function, dimension is a min/max pair of y/x coordinates; conform is false for tileUpdate because the cell_props don't actually mean anything; all data is still included
// destroys tiles, I think. might cause bugs
this.loc = vec.tileToChar(dimension[0]);
for (let y=dimension[0][0]; y<=dimension[1][0]; y++){
@@ -51,8 +53,7 @@ function Space(){
}
return this;
}
-
- this.towrite = function(){ // Does no splitting or anything like that. Just returns a list of triplets for the write function
+ towrite(){ // Does no splitting or anything like that. Just returns a list of triplets for the write function
if (this.loc[0] === undefined) throw "Space.towrite() requires valid loc";
let writes = [];
for (let line = 0; line < this.data.length; line++) for (let chr = 0; chr < this.data[line].length; chr++){
@@ -62,20 +63,20 @@ function Space(){
return writes;
}
- this.tofile = function(filename){
+ tofile(filename){
fs.writeFileSync(filename, this.print());
};
- this.print = function(){
+ print(){
return this.data.map(row => replace(replace(replace(row.slice(),'&','\\&'),'\\','\\\\'),'','&').join('')).join('\n');
}
- this.fromfile = function(filename){ //Reads an external file into internal data
+ fromfile(filename){ //Reads an external file into internal data
this.adhoc(fs.readFileSync(filename,'utf8'));
return this;
}
- this.adhoc = function(text){
+ adhoc(text){
text = text.split('\n')
text = text.map(row => row.split(''));
this.data = text.map(row => {
@@ -88,7 +89,7 @@ function Space(){
return this;
}
- this.comb = function(other, func){ // other must have a valid .loc. If this.loc null, treated as offset
+ comb(other, func){ // other must have a valid .loc. If this.loc null, treated as offset
if (! other.loc) throw "The secondary space must have a valid .loc"
let offset;
if (this.loc.length == 0) {
@@ -119,7 +120,7 @@ function Space(){
return this;
}
- this.search = function(other){ //Returns first instance of `other` subspace (prioritized vertically then horizontally)
+ search(other){ //Returns first instance of `other` subspace (prioritized vertically then horizontally)
// Non-standard exclusion of this.loc
let loc = [];
for (let line=0; line<=this.data.length-other.data.length; line++){ for (let chr=0; chr<=this.data[line].length-other.data[0].length; chr++){
@@ -138,11 +139,11 @@ function Space(){
return loc;
}
- this.nullprint = function(){
+ nullprint(){
let data = [];
for (let ln of this.data){
let newln = "";
- for (c of ln){
+ for (let c of ln){
if (c === '') newln += '\0';
else newln += c;
}
@@ -151,7 +152,7 @@ function Space(){
return data;
}
- this.regex = function(strlines){
+ regex(strlines){
if (typeof strlines[0] !== 'string') throw ".regex needs arr of strings";
let lines = [];
for (let i = 0; i < strlines.length; i++) {
@@ -180,7 +181,7 @@ function Space(){
return loc;
}
- this.subsection = function(range){ // range is a coordinate pair
+ subsection(range){ // range is a coordinate pair
if (range.length !== 2 || range[0].length !== 2) throw "not subsecting a coord pair";
// Similarly excludes this.loc
newspace = new Space();
@@ -195,12 +196,12 @@ function Space(){
return newspace;
}
- this.copy = function(){ // deeply copies `array` and `loc` to a new space
+ copy(){ // deeply copies `array` and `loc` to a new space
let newspace = new Space();
newspace.data = this.data.map(row => row.slice()); // Deep copy
newspace.loc = this.loc.slice();
return newspace;
}
}
-
+
module.exports = Space