From 7e15df80062b805be9081128be32e6329632b61c Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 20 Dec 2019 00:55:12 -0500 Subject: massively simplified and fixed space.js --- space.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'space.js') diff --git a/space.js b/space.js index 619b47b..f5bf041 100644 --- a/space.js +++ b/space.js @@ -17,10 +17,6 @@ function replace(text, old, repl){ //replaces, in an array `text`, the instances } return text; } -function zeroifnull(obj, prop, alt=0){ //If obj is null, return 0; else obj.prop - if (typeof(obj) == 'undefined') return alt; - else return obj[prop]; -} function Space(){ // CLASS this.data = []; // @@ -80,17 +76,22 @@ function Space(){ // CLASS return this.data.map(row => replace(replace(replace(row,'&','\\&'),'\\','\\\\'),'','&').join('')).join('\n'); } this.comb = function(other, func, offset){ - for (let row = offset[0]; row= this.data.length) this.data.push([]); - for (let chr = offset[1]; chr= this.data[row].length) - this.data[row].push(''); - this.data[row][chr] = func(this.data[row][chr], other.data[row][chr] || ''); - console.log(row,chr,this.data[row][chr]) - }} + // Convert negative offsets of either sort into zero offsets with significant previous whitespace (translation) + for (let i = 0; i < -offset[0]; i++) this.data.unshift([]); + if (offset[0] < 0) offset[0] = 0; + for (let row = 0; row < offset[0]+other.data.length; row++){ // offset[1] < 0 fix + for (let i = 0; i < -offset[1]; i++) this.data[row].unshift(''); + } + if (offset[1] < 0) offset[1] = 0; + + // Iterate over other.data and add to this.data + while (this.data.length < offset[0]+other.data.length) this.data.push([]); // Row padding + for (let row = 0; row < other.data.length; row++){ + while (this.data.length < offset[1]+other.data.length) this.data.push(''); // Character-wise padding + for (let chr = 0; chr < other.data[row].length; chr++){ + this.data[row+offset[0]][chr+offset[1]] = func(this.data[row+offset[0]][chr+offset[1]], other.data[row][chr]); + } + } } this.search = function(other){ //Returns first instance of a subspace (prioritized vertically then horizontally) let loc = []; -- cgit