storeObjectArray.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Types } from './../../ast/types';
  2. import { StoreObject } from './storeObject';
  3. export class StoreObjectArray extends StoreObject {
  4. constructor (subtype, lines, columns, value, readOnly) {
  5. super(Types.ARRAY, value, readOnly);
  6. this._lines = lines;
  7. this._columns = columns;
  8. this._subtype = subtype;
  9. }
  10. get lines () {
  11. return this._lines;
  12. }
  13. get columns () {
  14. return this._columns;
  15. }
  16. get subtype () {
  17. return this._subtype;
  18. }
  19. isCompatible (another) {
  20. if(another instanceof StoreObjectArray) {
  21. if(this.lines === another.lines &&
  22. this.columns === another.columns &&
  23. this.subtype === another.subtype) {
  24. return super.isCompatible(another);
  25. }
  26. }
  27. return false;
  28. }
  29. get isVector () {
  30. return this.columns === null;
  31. }
  32. get isValid () {
  33. if (this.value !== null) {
  34. if( this.isVector) {
  35. if(this.value.length !== this.lines) {
  36. return false;
  37. }
  38. const mustBeNull = this.value.find(v => v.type !== this.subtype);
  39. if(!!mustBeNull) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. } else {
  45. if(this.lines !== this.value.length) {
  46. return false;
  47. }
  48. for (let i = 0; i < this.lines; i++) {
  49. for (let j = 0; j < this.columns; j++) {
  50. const arr = this.value[i];
  51. if(arr.length !== this.columns) {
  52. return false;
  53. }
  54. const mustBeNull = arr.find(v => v.type !== this.subtype);
  55. if(!!mustBeNull) {
  56. return false;
  57. }
  58. }
  59. }
  60. return true;
  61. }
  62. }
  63. }