Skip to content

Commit

Permalink
proper modules for comparing vals
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Mar 27, 2019
1 parent 637fda8 commit 473381c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 84 deletions.
5 changes: 3 additions & 2 deletions src/core/Calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { Duration, createDuration } from './datelib/duration'
import reduce from './reducers/main'
import { parseDateSpan, DateSpanInput, DateSpan, buildDateSpanApi, DateSpanApi, buildDatePointApi, DatePointApi } from './structs/date-span'
import { memoize, memoizeOutput } from './util/memoize'
import { mapHash, isPropsEqual, anyKeysRemoved, computeChangedProps } from './util/object'
import { mapHash } from './util/object'
import { isObjectsSimilar, anyKeysRemoved, computeChangedProps } from './util/object-similarity'
import { DateRangeInput } from './datelib/date-range'
import DateProfileGenerator from './DateProfileGenerator'
import { EventSourceInput, parseEventSource, EventSourceHash } from './structs/event-source'
Expand Down Expand Up @@ -77,7 +78,7 @@ export default class Calendar {
private buildTheme = memoize(buildTheme)
private buildEventUiSingleBase = memoize(this._buildEventUiSingleBase)
private buildSelectionConfig = memoize(this._buildSelectionConfig)
private buildEventUiBySource = memoizeOutput(buildEventUiBySource, isPropsEqual)
private buildEventUiBySource = memoizeOutput(buildEventUiBySource, isObjectsSimilar)
private buildEventUiBases = memoize(buildEventUiBases)

eventUiBases: EventUiHash // solely for validation system
Expand Down
3 changes: 2 additions & 1 deletion src/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export {
translateRect
} from './util/geom'

export { isPropsEqual, mapHash, filterHash } from './util/object'
export { mapHash, filterHash } from './util/object'
export { isObjectsSimilar, isValuesSimilar } from './util/object-similarity'

export {
findElements,
Expand Down
5 changes: 3 additions & 2 deletions src/core/option-change-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createPlugin } from './plugin-system'
import { Calendar } from './main'
import { hashValuesToArray, isObjsSimilar } from './util/object'
import { hashValuesToArray } from './util/object'
import { isValuesSimilar } from './util/object-similarity'
import { EventSource } from './structs/event-source'

export default createPlugin({
Expand All @@ -21,7 +22,7 @@ function handleEventSources(inputs, calendar: Calendar) {
let inputFound = false

for (let i = 0; i < unfoundSources.length; i++) {
if (isObjsSimilar(unfoundSources[i]._raw, input)) {
if (isValuesSimilar(unfoundSources[i]._raw, input, 2)) {
unfoundSources.splice(i, 1) // delete
inputFound = true
break
Expand Down
79 changes: 0 additions & 79 deletions src/core/util/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isArraysEqual } from './array'

// Merges an array of objects into a single object.
// The second argument allows for an array of property names who's object values will be merged together.
Expand Down Expand Up @@ -97,81 +96,3 @@ export function hashValuesToArray(obj) {

return a
}


export function isPropsEqual(obj0, obj1): boolean {

for (let key in obj0) {
if (obj0[key] !== obj1[key]) {
return false
}
}

for (let key in obj1) {
if (!(key in obj0)) {
return false
}
}

return true
}


export function anyKeysRemoved(obj0, obj1) {
for (let prop in obj0) {
if (!(prop in obj1)) {
return true
}
}
return false
}

// will go recursively one level deep
export function computeChangedProps(obj0, obj1) {
let res = {}

for (let prop in obj1) {

if (!(prop in obj0)) {
res[prop] = obj1[prop]

} else {
if (!isValuesSimilar(obj0[prop], obj1[prop])) {
res[prop] = obj1[prop]
}
}
}

return res
}

// will go recursively one level deep
export function isObjsSimilar(obj0, obj1) {

for (let prop in obj0) {
if (!(prop in obj1)) {
return false
}
}

for (let prop in obj1) {

if (!(prop in obj0)) {
return false

} else {
if (!isValuesSimilar(obj0[prop], obj1[prop])) {
return false
}
}
}

return true
}


function isValuesSimilar(val0, val1) {
return val0 === val1 ||
(Array.isArray(val0) && Array.isArray(val1) && isArraysEqual(val0, val1)) ||
(typeof val0 === 'object' && typeof val1 === 'object' && isPropsEqual(val0, val1))
}

0 comments on commit 473381c

Please sign in to comment.