@bigfishtv/cockpit
Version:
48 lines (44 loc) • 1.59 kB
JavaScript
import Immutable from 'immutable'
import { isArray } from './typeUtils'
import _get from 'lodash/get'
/**
* Takes two objects and a list of paths and checks if any have changed
* @param {Object} oldValue
* @param {Object} newValue
* @param {Array} fields Either array of fields or string for a single field
* @return {Boolean}
*/
export function valueChanged(oldValue = {}, newValue = {}, fields = []) {
if (!isArray(fields)) fields = [fields]
for (let field of fields) {
if (isArray(field)) field = field.join('.')
if (_get(oldValue, field) !== _get(newValue, field)) {
return true
}
}
return false
}
/**
* Takes two form values (or prevProps and props) and checks for any differences in provided field list
* @param {Object} oldFormValue Either formValue or props object containing formValue
* @param {Object} newFormValue Either formValue or props object containing formValue
* @param {Array} fields Either array of fields or string for a single field
* @return {Boolean}
*/
export function formValueChanged(oldFormValue, newFormValue, fields = []) {
if ('formValue' in oldFormValue) oldFormValue = oldFormValue.formValue
if ('formValue' in newFormValue) newFormValue = newFormValue.formValue
oldFormValue = oldFormValue.value || {}
newFormValue = newFormValue.value || {}
return valueChanged(oldFormValue, newFormValue, fields)
}
/**
* Take any array or object and get a hash string for it
* @param {Object|Array} data
* @returns {String}
*/
export function getDataHash(data) {
return Immutable.fromJS(data)
.hashCode()
.toString()
}