All files / src/query/schema arrayField.js

60.87% Statements 14/23
36.36% Branches 4/11
75% Functions 6/8
60.87% Lines 14/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                  99x 99x 99x 99x       45x       189x       130x                                               99x 26x 26x 26x   73x 146x 146x        
// @flow
 
import forEach from 'lodash/forEach';
import {createField, isCompositeType} from './utils';
import {types} from './types';
import NullField from './nullField';
 
import type {Field} from './types';
 
 
export default class ArrayField implements Field {
  schema: any;
  rootSchema: any;
  key: string;
  isEntity: boolean;
 
  constructor({rootSchema, schema, key, isEntity}: {rootSchema: any, schema: any, key: string, isEntity?: ?boolean}) {
    this.key = key;
    this.rootSchema = rootSchema;
    this.schema = schema;
    this.isEntity = isEntity || false;
  }
 
  getAttr(name: string) {
    return this.schema[name];
  }
 
  getKey() {
    return this.key;
  }
 
  getType() {
    return types.ARRAY;
  }
 
  exists() {
    return true;
  }
 
  getChild(fieldName: string) {
    if (isCompositeType(this.schema.items.type)) {
      const child = createField(fieldName, this.rootSchema, this.schema.items);
      const childField = child.getChild(fieldName);
      return childField;
    }
 
    if (!this.schema.items || !this.schema.items.items || !this.schema.items.items[fieldName]) {
      return new NullField({key: fieldName});
    }
 
    // array of object
    const field = createField(fieldName, this.rootSchema, this.schema.items.items[fieldName]);
    return field;
  }
 
  forEach(visitor: Function) {
    if (isCompositeType(this.schema.items.type)) {
      const child = createField('', this.rootSchema, this.schema.items);
      child.forEach(visitor);
      return;
    }
    forEach(this.schema.items.items, (item, key) => {
      const field = createField(key, this.rootSchema, this.schema.items.items[key]);
      visitor(field);
    });
  }
}