All files / models Subjects.js

0% Statements 0/13
0% Branches 0/2
0% Functions 0/5
0% Lines 0/11
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                                                                 
import { isArrayLike as isArray, mapObjIndexed } from 'ramda';
 
export class Subject {
  constructor(obj) {
    /**
     * Because you can't directly assign a value to `this` using ES6 destructuring I'm mapping the
     * properties of the `subjects` object to the `this` object dynamically.
     */
    Object.keys(obj).map((key) => this[key] = obj[key]);
  }
}
 
export default class Subjects {
 
  constructor(obj) {
    let subjects = {};
    let length = 0;
 
    if (isArray(obj.ssrGetCoursesResp.courseSearchResult.subjects.subject)) {
      subjects = mapObjIndexed((subject, index) => {
        ++length;
        return new Subject(subject);
      }, obj.ssrGetCoursesResp.courseSearchResult.subjects.subject);
    } else {
      ++length;
      subjects['0'] = new Subject(obj.ssrGetCoursesResp.courseSearchResult.subjects.subject);
    }
 
    subjects.length = length;
    Object.keys(subjects).map((key) => this[key] = subjects[key]);
  }
}