feat(util/ModelUtil): expose `isAny`

It is a core utility that will be tree-shaken if not used.

We'll continue to expose it via `ModelingUtil` for backwards
compatibility.
This commit is contained in:
Nico Rehwaldt 2021-12-11 15:00:30 +01:00 committed by fake-join[bot]
parent 99516bc19b
commit a42ba5cd90
3 changed files with 85 additions and 49 deletions

View File

@ -1,24 +1,6 @@
import {
some
} from 'min-dash';
import { is } from '../../../util/ModelUtil';
/**
* Return true if element has any of the given types.
*
* @param {djs.model.Base} element
* @param {Array<string>} types
*
* @return {boolean}
*/
export function isAny(element, types) {
return some(types, function(t) {
return is(element, t);
});
}
export { is, isAny } from '../../../util/ModelUtil';
import { isAny } from '../../../util/ModelUtil';
/**
* Return the parent of the element with any of the given types.

View File

@ -1,3 +1,8 @@
import {
some
} from 'min-dash';
/**
* Is an element of the given BPMN type?
*
@ -13,6 +18,20 @@ export function is(element, type) {
}
/**
* Return true if element has any of the given types.
*
* @param {djs.model.Base} element
* @param {Array<string>} types
*
* @return {boolean}
*/
export function isAny(element, types) {
return some(types, function(t) {
return is(element, t);
});
}
/**
* Return the business object for a given element.
*

View File

@ -8,6 +8,7 @@ import modelingModule from 'lib/features/modeling';
import {
is,
isAny,
getDi
} from 'lib/util/ModelUtil';
@ -24,51 +25,85 @@ describe('util/ModelUtil', function() {
}));
it('should work with diagram element', inject(function(elementFactory) {
describe('#is', function() {
// given
var messageFlowConnection = elementFactory.createConnection({ type: 'bpmn:MessageFlow' });
it('should work with diagram element', inject(function(elementFactory) {
// then
expect(is(messageFlowConnection, 'bpmn:MessageFlow')).to.be.true;
expect(is(messageFlowConnection, 'bpmn:BaseElement')).to.be.true;
// given
var messageFlowConnection = elementFactory.createConnection({ type: 'bpmn:MessageFlow' });
expect(is(messageFlowConnection, 'bpmn:SequenceFlow')).to.be.false;
expect(is(messageFlowConnection, 'bpmn:Task')).to.be.false;
}));
// then
expect(is(messageFlowConnection, 'bpmn:MessageFlow')).to.be.true;
expect(is(messageFlowConnection, 'bpmn:BaseElement')).to.be.true;
expect(is(messageFlowConnection, 'bpmn:SequenceFlow')).to.be.false;
expect(is(messageFlowConnection, 'bpmn:Task')).to.be.false;
}));
it('should work with business object', inject(function(bpmnFactory) {
it('should work with business object', inject(function(bpmnFactory) {
// given
var gateway = bpmnFactory.create('bpmn:Gateway');
// given
var gateway = bpmnFactory.create('bpmn:Gateway');
// then
expect(is(gateway, 'bpmn:Gateway')).to.be.true;
expect(is(gateway, 'bpmn:BaseElement')).to.be.true;
// then
expect(is(gateway, 'bpmn:Gateway')).to.be.true;
expect(is(gateway, 'bpmn:BaseElement')).to.be.true;
expect(is(gateway, 'bpmn:SequenceFlow')).to.be.false;
}));
expect(is(gateway, 'bpmn:SequenceFlow')).to.be.false;
}));
it('should work with untyped business object', inject(function() {
it('should work with untyped business object', inject(function() {
// given
var foo = { businessObject: 'BAR' };
// given
var foo = { businessObject: 'BAR' };
// then
expect(is(foo, 'FOO')).to.be.false;
}));
// then
expect(is(foo, 'FOO')).to.be.false;
}));
it('should work with untyped diagram element', inject(function() {
it('should work with untyped diagram element', inject(function() {
// given
var foo = { };
// given
var foo = { };
// then
expect(is(foo, 'FOO')).to.be.false;
}));
// then
expect(is(foo, 'FOO')).to.be.false;
}));
});
describe('isAny', function() {
it('should work on shape', inject(function(bpmnFactory, elementFactory) {
// given
var element = elementFactory.createShape({ type: 'bpmn:Gateway' });
// then
expect(isAny(element, [ 'bpmn:Gateway' ])).to.be.true;
expect(isAny(element, [ 'bpmn:SequenceFlow', 'bpmn:Gateway' ])).to.be.true;
expect(isAny(element, [ 'bpmn:BaseElement' ])).to.be.true;
expect(isAny(element, [ 'bpmn:SequenceFlow' ])).to.be.false;
}));
it('should work on businessObject', inject(function(bpmnFactory, elementFactory) {
// given
var businessObject = bpmnFactory.create('bpmn:Gateway');
// then
expect(isAny(businessObject, [ 'bpmn:Gateway' ])).to.be.true;
expect(isAny(businessObject, [ 'bpmn:SequenceFlow', 'bpmn:Gateway' ])).to.be.true;
expect(isAny(businessObject, [ 'bpmn:BaseElement' ])).to.be.true;
expect(isAny(businessObject, [ 'bpmn:SequenceFlow' ])).to.be.false;
}));
});
describe('#getDi', function() {