Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/objects.js
// @flow | // @flow | ||||
import stableStringify from 'fast-json-stable-stringify'; | import stableStringify from 'fast-json-stable-stringify'; | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import _difference from 'lodash/fp/difference.js'; | |||||
import _isEqual from 'lodash/fp/isEqual.js'; | import _isEqual from 'lodash/fp/isEqual.js'; | ||||
import _isPlainObject from 'lodash/fp/isPlainObject.js'; | import _isPlainObject from 'lodash/fp/isPlainObject.js'; | ||||
import stringHash from 'string-hash'; | import stringHash from 'string-hash'; | ||||
type ObjectMap<K, T> = { +[key: K]: T }; | type ObjectMap<K, T> = { +[key: K]: T }; | ||||
type NestedObjectMap<K, T> = { +[key: K]: T | NestedObjectMap<K, T> }; | type NestedObjectMap<K, T> = { +[key: K]: T | NestedObjectMap<K, T> }; | ||||
function findMaximumDepth(obj: Object): ?{ path: string, depth: number } { | function findMaximumDepth(obj: Object): ?{ path: string, depth: number } { | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | ): NestedObjectMap<K, T> { | ||||
return diff; | return diff; | ||||
} | } | ||||
function assertObjectsAreEqual<K, T>( | function assertObjectsAreEqual<K, T>( | ||||
processedObject: ObjectMap<K, T>, | processedObject: ObjectMap<K, T>, | ||||
expectedObject: ObjectMap<K, T>, | expectedObject: ObjectMap<K, T>, | ||||
message: string, | message: string, | ||||
) { | ) { | ||||
const processedObjectKeys = Object.keys(processedObject); | if (_isEqual(processedObject)(expectedObject)) { | ||||
const expectedObjectKeys = Object.keys(expectedObject); | return; | ||||
} | |||||
const inProcessedButNotExpected = | const dataProcessedButNotExpected = deepDiff(processedObject, expectedObject); | ||||
_difference(processedObjectKeys)(expectedObjectKeys); | const dataExpectedButNotProcessed = deepDiff(expectedObject, processedObject); | ||||
const inExpectedButNotProcessed = | |||||
_difference(expectedObjectKeys)(processedObjectKeys); | |||||
invariant( | invariant( | ||||
_isEqual(processedObject)(expectedObject), | false, | ||||
`${message}: Objects should be equal.` + | `${message}: Objects should be equal.` + | ||||
` Object keys processed but not expected:` + | ` Data processed but not expected:` + | ||||
` ${inExpectedButNotProcessed.toString()}` + | ` ${JSON.stringify(dataProcessedButNotExpected)}` + | ||||
` Object keys expected but not processed:` + | ` Data expected but not processed:` + | ||||
` ${inProcessedButNotExpected.toString()}`, | ` ${JSON.stringify(dataExpectedButNotProcessed)}`, | ||||
); | ); | ||||
} | } | ||||
export { findMaximumDepth, values, hash, assertObjectsAreEqual, deepDiff }; | export { findMaximumDepth, values, hash, assertObjectsAreEqual, deepDiff }; |