mirror of
https://github.com/umap-project/umap.git
synced 2025-04-28 19:42:36 +02:00
Add a fieldInSchema
utility function.
Check that fields are present in the schema before calling render
This commit is contained in:
parent
4f0404149e
commit
8eae7990dd
3 changed files with 47 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
|||
import { fieldInSchema } from '../utils.js'
|
||||
|
||||
/**
|
||||
* Updaters are classes able to convert messages
|
||||
* received from other peers (or from the server) to changes on the map.
|
||||
|
@ -42,9 +44,10 @@ class BaseUpdater {
|
|||
|
||||
export class MapUpdater extends BaseUpdater {
|
||||
update({ key, value }) {
|
||||
if (key !== 'numberOfConnectedPeers') {
|
||||
if (fieldInSchema(key)){
|
||||
this.updateObjectValue(this.map, key, value)
|
||||
}
|
||||
|
||||
this.map.render([key])
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +61,11 @@ export class DataLayerUpdater extends BaseUpdater {
|
|||
|
||||
update({ key, metadata, value }) {
|
||||
const datalayer = this.getDataLayerFromID(metadata.id)
|
||||
this.updateObjectValue(datalayer, key, value)
|
||||
if (fieldInSchema(key)) {
|
||||
this.updateObjectValue(datalayer, key, value)
|
||||
} else {
|
||||
console.debug('Not applying update for datalayer because key is not in the schema', key)
|
||||
}
|
||||
datalayer.render([key])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,6 +54,20 @@ export function getImpactsFromSchema(fields, schema) {
|
|||
return Array.from(impacted)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a field exists in the schema.
|
||||
*
|
||||
* @param {string} field
|
||||
* @param {object} schema
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function fieldInSchema(field, schema) {
|
||||
if (typeof field !== 'string') return false
|
||||
field = field.replace('options.', '').split('.')[0]
|
||||
schema = schema || U.SCHEMA
|
||||
return schema[field] !== undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Import DOM purify, and initialize it.
|
||||
*
|
||||
|
|
|
@ -747,6 +747,30 @@ describe('Utils', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('#fieldInSchema', () => {
|
||||
it('should return true if the field is in the schema', () => {
|
||||
assert.equal(Utils.fieldInSchema('foo', { foo: {} }), true)
|
||||
})
|
||||
it('should return false if the field is not in the schema', () => {
|
||||
assert.equal(Utils.fieldInSchema('foo', { bar: {} }), false)
|
||||
})
|
||||
it('should return false if the schema is not provided', () => {
|
||||
assert.equal(Utils.fieldInSchema('foo', {}), false)
|
||||
})
|
||||
it('should return false if the field is undefined', () => {
|
||||
assert.equal(Utils.fieldInSchema(undefined, {}), false)
|
||||
})
|
||||
// check that options. is removed
|
||||
it('should remove options. from the field', () => {
|
||||
assert.equal(Utils.fieldInSchema('options.foo', { foo: {} }), true)
|
||||
})
|
||||
|
||||
// check that subfields are removed
|
||||
it('should remove subfields from the field', () => {
|
||||
assert.equal(Utils.fieldInSchema('options.foo.bar', { foo: { bar: {} } }), true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#parseNaiveDate', () => {
|
||||
it('should parse a date', () => {
|
||||
assert.equal(
|
||||
|
|
Loading…
Reference in a new issue