arrayLiteral.js 1.6 KB

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