Example #1
0
function updateInKeypath (map: any, keyPath: Array<String | number>, v: any): any {
  const frontKey = keyPath[0]
  var copy: any
  if (keyPath.length === 1) {
    if (map.constructor === Array) {
      copy = [].concat(map)
      if (typeof frontKey === 'number') {
        copy[frontKey] = v
      } else {
        console.error('Accessing an array without a number')
      }
      return copy
    } else if (Immutable.Iterable.isIterable(map)) {
      return map.set(frontKey, v)
    }

    return {...map, [frontKey]: v}
  }

  if (map.constructor === Array) {
    copy = [].concat(map)
    if (typeof frontKey === 'number') {
      copy[frontKey] = updateInKeypath(copy[frontKey], keyPath.slice(1), v)
    } else {
      console.error('Accessing an array without a number')
    }
    return copy
  } else if (Immutable.Iterable.isIterable(map)) {
    return map.setIn(keyPath, v)
  }

  return {...map, [frontKey]: updateInKeypath(map[frontKey], keyPath.slice(1), v)}
}
Example #2
0
File: ref.js Project: JaapRood/vry
internals.Ref.replaceIn = function(source, subject, ...paths) {
	Invariant(Immutable.Iterable.isIterable(source), 'Root source is required to replace references in subject');
	Invariant(Immutable.Iterable.isIterable(subject), 'Subject source is required to replace references in subject');

	paths = Immutable.List(paths).map(KeyPath.parse);

	Invariant(paths.every(KeyPath.isKeyPath), 'Path(s) required to replace references in a subject')

	return paths.reduce((subj, path) => {
		const nextKey = path.first();
		const restPath = path.rest();

		return subj.update(nextKey, (maybeRef) => {
			var value;

			if (internals.Ref.instanceOf(maybeRef)) {
				value = internals.Ref.resolve(maybeRef, source);
			} else if (internals.Ref.collectionOf(maybeRef)) {
				value = internals.Ref.resolveCollection(maybeRef, source);
			} else {
				value = maybeRef;
			}

			if (restPath.count() && Immutable.Iterable.isIterable(value)) {
				return internals.Ref.replaceIn(source, value, restPath);
			} else {
				return value;
			}
		});
	}, subject);

}
Example #3
0
var deepEqualValues = function deepEqualValues(a, b) {
  if (_immutable.Iterable.isIterable(a)) {
    return _immutable.Iterable.isIterable(b) && a.count() === b.count() && a.every(function (value, key) {
      return deepEqualValues(value, b.get(key));
    });
  }
  return (0, _deepEqual2.default)(a, b); // neither are immutable iterables
};
Example #4
0
const deepEqualValues = (a, b) => {
  if (Iterable.isIterable(a)) {
    return Iterable.isIterable(b) &&
      a.count() === b.count() &&
      a.every((value, key) => deepEqualValues(value, b.get(key)))
  }
  return deepEqual(a, b) // neither are immutable iterables
}
Example #5
0
module.exports = function(prevState, currState, keyPaths) {
  if (!Iterable.isIterable(prevState) || !Iterable.isIterable(currState)) {
    // prev or current state is some primitive
    return prevState !== currState
  }

  return keyPaths.some(function(keyPath) {
    return prevState.getIn(keyPath) !== currState.getIn(keyPath)
  })
}
Example #6
0
merge = function(preserve, newValue, value) {
  if (Util.undefinedOrNull(value)) {
    return newValue;
  } else {
    if (Imm.Iterable.isIterable(value) && Imm.Iterable.isIterable(value)) {
      return preserve ? newValue.mergeDeep(value) : value.mergeDeep(newValue);
    } else {
      return preserve ? value : newValue;
    }
  }
};
Example #7
0
var customizer = function customizer(obj, other) {
  if (obj == other) return true;
  if ((obj == null || obj === '' || obj === false) && (other == null || other === '' || other === false)) return true;

  if (_immutable.Iterable.isIterable(obj) && _immutable.Iterable.isIterable(other)) {
    return obj.count() === other.count() && obj.every(function (value, key) {
      return (0, _isEqualWith3.default)(value, other.get(key), customizer);
    });
  }

  return void 0;
};
 Object.keys(state).forEach(i => {
   if (Immutable.Iterable.isIterable(state[i])) {
     newState[i] = state[i].toJS()
   } else {
     newState[i] = state[i]
   }
 })
//
// Extra functions for dealing with semi-immutable objects
// or non-immutable objects with immutable semantics
//

/**
 * Split a stateIndex path into two parts:
 * 1. Up to where the first immutable object is
 * 2. Everything after that
 * @param obj    object to look in
 * @param index  array of keys to look in obj
 * @note  this must be called with "this" bound correctly
 * @note  this assumes the mutable part comes first
 *        (e.g., with passing a React state object)
 */
function splitMutableImmutable(obj, index) {
  if (typeof index === 'string') {
    index = index.split('.');
  } else if (typeof index === 'number') {
    index = [index];
  }

  let cur = obj;
  let immutableParts = [];
  const mutableParts = [];

  for (let i = 0; i !== index.length; i++) {
    if (Immutable.Iterable.isIterable(cur)) {
      // Everything after the current is mutable
      immutableParts = index.slice(i);
      break;
    }
    const part = index[i];
    mutableParts.push(part);
    cur = obj[part];
  }

  return {
    mutable: mutableParts,
    immutable: immutableParts,
  };
}
Example #10
0
    return Object.keys(state).reduce((newState, key) => {
        const value = state[key];

        newState[key] = Iterable.isIterable(value) ? value.toJS() : value;

        return newState;
    }, {});
Example #11
0
 return Object.keys(this.constructor.storeProps).reduce((prev, prop) => {
   const val = this.props[prop]
   if (!this.isPropResolving(prop)) {
     prev[prop] = Iterable.isIterable(val) ? val.get('data') || val : val
   }
   return prev
 }, {})
Example #12
0
    return state.update('content', function (content) {
      var immutableRecord = !_immutable.Iterable.isIterable(record) ? new CollectionRecord(record) : record;

      // add
      if (!immutableRecord._id) {
        var _ret = function () {
          var _id = generateUniqID();
          if (content.find(function (rec) {
            return rec._id === _id;
          })) return {
              v: content
            };
          var recordWithId = immutableRecord.set('_id', _id);
          return {
            v: content.push(createRecord(_id, recordWithId))
          };
        }();

        if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
      }
      // edit
      var recordToUpdate = content.find(function (entry) {
        return entry.get('_id') === immutableRecord._id;
      });
      var recordToUpdateIndex = content.indexOf(recordToUpdate);
      return content.update(recordToUpdateIndex, function () {
        return record;
      });
    });
Example #13
0
 return (0, _keys2.default)(this.constructor.storeProps).reduce(function (prev, prop) {
   var val = _this4.props[prop];
   if (!_this4.isPropResolving(prop)) {
     prev[prop] = _immutable.Iterable.isIterable(val) ? val.get('data') || val : val;
   }
   return prev;
 }, {});
Example #14
0
var customizer = function customizer(obj, other) {
  if (obj === other) return true;
  if (!obj && !other) {
    var objIsEmpty = obj === null || obj === undefined || obj === '';
    var otherIsEmpty = other === null || other === undefined || other === '';
    return objIsEmpty === otherIsEmpty;
  }

  if (Iterable.isIterable(obj) && Iterable.isIterable(other)) {
    return obj.count() === other.count() && obj.every(function (value, key) {
      return other.has(key) && _isEqualWith(value, other.get(key), customizer);
    });
  }

  return void 0;
};
Example #15
0
 Object.keys(state).forEach(x=> {
   if (Iterable.isIterable(state[x])) {
     newSate[x] = state[x].toJS()
   } else {
     newSate[x] = state[x]
   }
 })
Example #16
0
  const val = values.map((v, k) => {
    const err = normalized.getIn(['result', k])
    if (err) {
      // console.log('err', errorKey, k, err.toJS(), path)
      if ((err.size === 0 && path.length === 0) ||
        k === errorKey ||
        (pathMappings.get(k) === path.join('.'))) {
        const label = CommonMessages[k] || k
        // console.log('err', errorKey, label, k, err.toJS(), path)
        return Map({
          label: label,
          message: { id: 'Components.Validators.IsRequired.Message' },
          onClick: onClickAction
        })
      }
    }

    if (Iterable.isIterable(v)) {
      const id = v.get('id')
      const errEntity = id && normalized.getIn(['entities', 'errors', id]) || null
      const result = createErrors(v, normalized, errEntity && errEntity.get('key') || errorKey, [...path, k], onClickAction)
      if (result) {
        const filtered = result.filter(x => x)
        return filtered.size ? filtered : null
      }
    }
    return null
  })
export function denormalize(object, entities, schema) {

  if (!Iterable.isIterable(entities)) {
    throw new Error('Denormalizr-immutable accepts an immutable object as its entities.');
  }

  if (!isObject(schema) || Array.isArray(schema)) {
    throw new Error('Denormalizr-immutable accepts an object for schema.');
  }

  if (!(schema instanceof EntitySchema || schema instanceof ArraySchema ||
        schema instanceof RecordEntitySchema || schema instanceof ImmutableArraySchema)) {
    throw new Error('Denormalizr-immutable accepts an EntitySchema, RecordEntitySchema or ArraySchema for schema.');
  }

  if (!object) {
    return object;
  }

  if (schema instanceof ArraySchema || schema instanceof ImmutableArraySchema) {
    return denormalizeArray(object, entities, schema);
  }

  return denormalizeEntity(object, entities, schema);
}
Example #18
0
 transformer: (state) => {
   const newState = {};
   for (let key of Object.keys(state)) {
     newState[key] = Immutable.Iterable.isIterable(state[key]) ? state[key].toJS() : state[key];
   }
   return newState;
 }
Example #19
0
var createReducer = function createReducer(o, parentName) {
  var _createReducers = createReducers(o, parentName);

  var reducers = _createReducers.reducers;
  var isContainer = _createReducers.isContainer;
  var name = _createReducers.name;

  if (isContainer && typeof o.initialState === 'undefined') {
    throw new Error('Reducer "' + (name || 'root') + '" is missing an initialState');
  }
  if (!isContainer && typeof o.initialState !== 'undefined') {
    throw new Error('Reducer "' + (name || 'root') + '" has no reducers, so it can\'t specify an initialState');
  }
  var initialState = getInitialState(o);
  if (!_immutable.Iterable.isIterable(initialState)) {
    throw new Error('Reducer "' + (name || 'root') + '" is missing an Immutable initialState');
  }

  var reducer = _reduceReducers2.default.apply(undefined, (0, _toConsumableArray3.default)((0, _lodash4.default)(reducers)));
  var statePath = name && isContainer ? name.split('.') : undefined;
  return createReducerNode({
    name: name,
    initialState: initialState,
    reducer: reducer,
    statePath: statePath
  });
};
const clearCachedAtRecursive = (state) => {
  if (!Iterable.isIterable(state)) return state;
  if (state.has('cachedAt')) {
    return state.set('cachedAt', moment(0)).map(clearCachedAtRecursive);
  }
  return state.map(clearCachedAtRecursive);
};
Example #21
0
 Object.keys(state).forEach((x) => {
   if(Iterable.isIterable(state[x])){
     newState[x] = state[x].toJS();
   }else {
     newState[x] = state[x];
   }
 });
Example #22
0
export function getList(iterable, keys) {
  if(!Im.Iterable.isIterable(iterable)) {
    return Im.List()
  }
  let val = iterable.getIn(Array.isArray(keys) ? keys : [keys])
  return Im.List.isList(val) ? val : Im.List()
}
Example #23
0
                .then((nextValue) => {
                    if (!Iterable.isIterable(currentValue)) {
                        return assign({}, currentValue, nextValue);
                    }

                    return currentValue.mergeDeep(nextValue);
                })
Example #24
0
  stateTransformer: (state) => mapObject(state, (val) => {
    if (Iterable.isIterable(val)) {
      return val.toJS();
    }

    return val;
  }),
 const stateTransformer = (state) => {
   if (Iterable.isIterable(state)) {
     return state.toJS();
   } else {
     return state;
   }
 };
Example #26
0
File: to-js.js Project: wulv/movie
 ).reduce((newProps, wrappedComponentProp) => {
   newProps[wrappedComponentProp[KEY]] = Iterable.isIterable(
     wrappedComponentProp[VALUE]
   )
     ? wrappedComponentProp[VALUE].toJS()
     : wrappedComponentProp[VALUE]
   return newProps
 }, {})
Example #27
0
 let f = (v, k) => {
     if(I.Iterable.isIterable(v)) {
         return v.map(f);
     } else if (ratioKeys.includes(k)) {
         return new Ratio(v);
     }
     return v;
 };
Example #28
0
var _removeRatios = function(v, k) {
    if(I.Iterable.isIterable(v)) {
        return v.map(_removeRatios);
    } else if (v instanceof Ratio) {
        return v.n;
    }
    return v;
};
Example #29
0
  return function (state) {
    var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

    // if we are the reducer container, pass them our cherry-picked state
    // otherwise pass down the full state to the next container
    var currNodeState = (statePath ? state.getIn(statePath) : state) || initialState;
    if (!_immutable.Iterable.isIterable(currNodeState)) {
      throw new Error('Reducer "' + (name || 'root') + '" was given a non-Immutable state!');
    }
    var nextNodeState = reducer(currNodeState, action);
    if (!_immutable.Iterable.isIterable(nextNodeState)) {
      throw new Error('Reducer "' + (name || 'root') + '" returned a non-Immutable state!');
    }
    var nextRootState = statePath ? state.setIn(statePath, nextNodeState) : nextNodeState;

    return nextRootState;
  };
Example #30
0
const sort = (list, sortAttr) => {
  const nameAttribute = sortAttr
  if (Iterable.isIterable(list)) {
    return list.sortBy(x => x.get ? x.get(nameAttribute) : x[nameAttribute])
  }
  list.sort((a, b) => a[nameAttribute] < b[nameAttribute] ? -1 : 1)
  return list
}