JSFiddle - React, Tailwind, and code Playground

by tonytlwu

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

JavaScript

var sampleData = {
  apps: [
    { id: 1, title: 'Lorem', published: true, userId: 123 },
    { id: 2, title: 'Ipsum', published: false, userId: 123 },
    { id: 3, title: 'Dolor', published: true, userId: 456 },
    { id: 4, title: 'Sit', published: true, userId: 789 },
    { id: 5, title: 'Amet', published: false, userId: 123 },
    { id: 6, title: 'Et', published: true, userId: 123 }
  ],
  organizations: [
    { id: 1, name: 'Google', suspended: true, userId: 123 },
    { id: 2, name: 'Apple', suspended: false, userId: 456 },
    { id: 3, name: 'Fliplet', suspended: false, userId: 123 }
  ]
}

// @TODO: This is the model/class you should work out
var User = function (options) {
  options = options || {};
  if (!options.id) {
    throw new Error('User ID is required.');
  }

  this.id = options.id

  return this;
};

User.prototype.select = function (model) {
  if (!model) {
    throw new Error('A valid model is required.')
  }

  if (!sampleData.hasOwnProperty(model)) {
    throw new Error('Model not found: ' + model);
  }

  delete this.attr;
  this.model = _.cloneDeep(sampleData[model]);

  return this;
};

User.prototype.attributes = function (attr) {
  if (!attr) {
    return this;
  }

  if (typeof attr === 'string') {
    attr = [attr];
  }

  this.attr = attr;

  return this;
};

User.prototype.where = function (where) {
  var result = _.cloneDeep(this.model);

  if (typeof where === 'undefined') {
    this.result = result;
    return this;
  }

  if (typeof where !== 'object') {
    throw new Error('An object must be passed to User.where as a query');
  }

  this.result = _.filter(result, function (o) {
    var match = false;
    _.some(Object.keys(where), function (cond) {
      if (!o.hasOwnProperty(cond) || o[cond] !== where[cond]) {
        match = true
        return true;
      }
    });
    return match;
  }); 

  return this;
};

User.prototype.order = function (order) {
  if (typeof order === 'undefined') {
    throw new Error('A sorting...