diff options
author | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-20 00:55:12 -0500 |
---|---|---|
committer | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-20 00:55:12 -0500 |
commit | 7e15df80062b805be9081128be32e6329632b61c (patch) | |
tree | 439dcbc5e3419104ee994190a27e274246a9a152 | |
parent | 85cb589c3c40c5ae41ea1b547926c3f2daea63ea (diff) |
massively simplified and fixed space.js
-rw-r--r-- | space.js | 31 |
1 files changed, 16 insertions, 15 deletions
@@ -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<Math.max(other.data.length+offset[0],this.data.length); row++){ - if (row < 0) this.data.unshift([]); - if (row >= this.data.length) this.data.push([]); - for (let chr = offset[1]; chr<Math.max(zeroifnull(other.data[row],'length')+offset[1], zeroifnull(this.data[row],'length')); chr++){ - if (chr < 0) - this.data[row].unshift(''); - if (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 = []; |