Пример #1
0
 deepDiff(one: Object, two: Object, path: string = ''): IDeepDiff[] {
     let result: IDeepDiff[] = [];
     for (var key of _.keys(one)) {
         let concatPath: string = path ? path + '.' + key : key;
         if (_.isPlainObject(one[key])) {
             if (!_.has(two, key)) {
                 result.push(new DeepDiff('deleted', concatPath, one[key], null));
             } else {
                 result = _.concat(result, this.deepDiff(one[key], two[key], path ? path + '.' + key : key));
             }
         } else if (_.isBoolean(one[key]) || _.isDate(one[key]) || _.isNumber(one[key])
             || _.isNull(one[key]) || _.isRegExp(one[key]) || _.isString(one[key])) {
             if (!_.has(two, key)) {
                 result.push(new DeepDiff('deleted', concatPath, one[key], null));
             } else if (_.get(one, key) !== _.get(two, key)) {
                 result.push(new DeepDiff('edited', concatPath, one[key], two[key]));
             }
         } else if (_.isArray(one[key]) && _.isArray(two[key]) && !_.isEqual(one[key], two[key])) {
             result.push(new DeepDiff('array', concatPath, one[key], two[key]));
         } else if (!_.has(two, key)) {
             result.push(new DeepDiff('deleted', concatPath, one[key], null));
         }
     }
     for (var key of _.keys(two)) {
         let concatPath: string = path ? path + '.' + key : key;
         if (!_.has(one, key)) {
             if (_.isPlainObject(two[key]) || _.isBoolean(two[key]) || _.isDate(two[key]) || _.isNumber(two[key])
                 || _.isNull(two[key]) || _.isRegExp(two[key]) || _.isString(two[key]) || _.isArray(two[key])) {
                 result.push(new DeepDiff('created', concatPath, null, two[key]));
             }
         }
     }
     return result;
 }
Пример #2
0
  private moveAssociationReferences(object: any): void {
    if (!object.$associations) object.$associations = {}

    var key
    for (key in object) {
      if (!object.hasOwnProperty(key)) continue

      var value = object[key]

      if (key === '$associations') continue

      if (isArray(value)) {
        var el = first(value)
        if (isPlainObject(el) && el['@id']) {
          // HABTM
          object.$associations[key] = clone(value)
          delete object[key]
        }
      }
      else if (isPlainObject(value) && value['@id']) {
        object.$associations[key] = clone(value)
        delete object[key]
      }
    }
  }
export function typeOfSchema(schema: JSONSchema): SCHEMA_TYPE {
  if (schema.tsType) return 'CUSTOM_TYPE'
  if (schema.allOf) return 'ALL_OF'
  if (schema.anyOf) return 'ANY_OF'
  if (schema.oneOf) return 'ONE_OF'
  if (schema.items) return 'TYPED_ARRAY'
  if (schema.enum && schema.tsEnumNames) return 'NAMED_ENUM'
  if (schema.enum) return 'UNNAMED_ENUM'
  if (schema.$ref) return 'REFERENCE'
  if (Array.isArray(schema.type)) return 'UNION'
  switch (schema.type) {
    case 'string': return 'STRING'
    case 'number': return 'NUMBER'
    case 'integer': return 'NUMBER'
    case 'boolean': return 'BOOLEAN'
    case 'object':
      if (!schema.properties && !isPlainObject(schema)) {
        return 'OBJECT'
      }
      break
    case 'array': return 'UNTYPED_ARRAY'
    case 'null': return 'NULL'
    case 'any': return 'ANY'
  }

  switch (typeof schema.default) {
    case 'boolean': return 'BOOLEAN'
    case 'number': return 'NUMBER'
    case 'string': return 'STRING'
  }
  if (schema.id) return 'NAMED_SCHEMA'
  if (isPlainObject(schema) && Object.keys(schema).length) return 'UNNAMED_SCHEMA'
  return 'ANY'
}
Пример #4
0
    function normalize(value, key) {
        if (!value) return value

        if (isPlainObject(value)) {
            return mapValues(value, normalize)
        }

        if (isArray(value) && isPlainObject(value[0])) {
            return map(value, normalize)
        }

        if (key.includes('_at') || key.includes('_on')) {
            return '2017-10-10T16:00:00Z'
        }

        if (key.includes('_count')) {
            return 42
        }

        if (key.includes('id')) {
            return 1000 + id++
        }

        if (key.includes('node_id')) {
            return 'MDA6RW50aXR5MQ=='
        }

        if (key.includes('url')) {
            return value.replace(/[1-9][0-9]{2,10}/, '000000001')
        }

        return value
    }
Пример #5
0
export function createDOMElement(tagName, style, attributes) {
  /* eslint complexity:0 */
  assert(_.isString(tagName), 'createDOMElement expects the first argument, tagName, to be a DOM element');
  if (style) assert(_.isPlainObject(style), 'createDOMElement expects the second argument, style, to be a hash');
  if (attributes) assert(_.isPlainObject(attributes), 'createDOMElement expects the third argument, attributes, to be a hash');

  var domElement = document.createElement(tagName);

  if (style) addStyle(domElement, style);
  if (attributes) _.each(attributes, setAttribute(domElement));

  return domElement;
}
Пример #6
0
export default function init(options: Options = {}): Init {
  if (!isPlainObject(options)) {
    throw new TypeError("@cachemap/reaper expected options to be a plain object.");
  }

  return (callbacks: Callbacks) => new Reaper({ ...options, ...callbacks });
}
Пример #7
0
 let unsetIgnoredSubProperties = (obj: any) => {
     if (_.isPlainObject(obj)) {
         for (var prop of this.options.ignoreSubProperties) {
             _.unset(obj, prop);
         }
     }
 };
Пример #8
0
 public static from (value, args = []) {
   if (value instanceof Criteria) return value
   
   if (isPlainObject(value)) return this.fromObject(value)
   
   return new Criteria(Literal.from(value, args))
 }
Пример #9
0
  return function(optionText) {
    var item = _.isPlainObject(optionText) ?
      createOptgroup(optionText) :
      createOption(optionText);

    domElement.appendChild(item);
  };
Пример #10
0
export function dft<T, U>(object: { [k: string]: any }, cb: (value: U, key: string) => T): void {
  for (let key in object) {
    if (!object.hasOwnProperty(key)) continue
    if (isPlainObject(object[key])) dft(object[key], cb)
    cb(object[key], key)
  }
}