arrayLiteral.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Literal } from './literal';
  2. import { Types } from './../types';
  3. export class ArrayLiteral extends Literal {
  4. constructor(value) {
  5. super(Types.ARRAY);
  6. this.value = value;
  7. }
  8. get subtype () {
  9. let element = this.value[0];
  10. if (element instanceof ArrayLiteral) {
  11. return element.value[0].type;
  12. } else {
  13. return element.type;
  14. }
  15. }
  16. get lines () {
  17. return this.value.length;
  18. }
  19. get columns () {
  20. let element = this.value[0];
  21. if (!(element instanceof ArrayLiteral)){
  22. return null;
  23. } else {
  24. return element.value[0].value.length;
  25. }
  26. }
  27. get isVector () {
  28. return this.columns === null;
  29. }
  30. get isValid () {
  31. return true;//this.validateType() && this.validateSize();
  32. }
  33. validateType () {
  34. // let valid = true;
  35. // if(this.columns !== null) {
  36. // const len = this.columns;
  37. // const len2 = this.lines;
  38. // for (let i = len - 1; i >= 0; i--) {
  39. // for (let j = len2 - 1; j >= 0; j--) {
  40. // if(this.value[i].value[j].type !== this.subtype) {
  41. // valid = false;
  42. // break;
  43. // }
  44. // }
  45. // }
  46. // } else {
  47. // const len = this.lines;
  48. // for (var i = len - 1; i >= 0; i--) {
  49. // if(this.value[i].type !== this.subtype) {
  50. // valid = false;
  51. // break;
  52. // }
  53. // }
  54. // }
  55. return true;//valid;
  56. }
  57. validateSize () {
  58. let valid = true;
  59. if(this.columns !== null) {
  60. const equalityTest = data.value.map(v => v.length)
  61. .reduce((old, next) => {
  62. if (old === null) {
  63. return next;
  64. } else if (old === next) {
  65. return old
  66. } else {
  67. return -1;
  68. }
  69. }, null);
  70. valid = equalityTest !== -1;
  71. }
  72. return valid;
  73. }
  74. }