Skip to content

Commit

Permalink
feat: add additional predicates
Browse files Browse the repository at this point in the history
- includesSymbols
- includesClasses

Closes swagger-api/oss-planning#238
  • Loading branch information
char0n committed Oct 30, 2020
1 parent 59145fe commit 93e49aa
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apidom/packages/apidom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export {
isRefElement,
isSourceMapElement,
hasElementSourceMap,
includesSymbols,
includesClasses,
} from './predicates';
export { default as createPredicate } from './predicates/helpers';

Expand Down
29 changes: 28 additions & 1 deletion apidom/packages/apidom/src/predicates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
LinkElement,
RefElement,
} from 'minim';
import { either, allPass, is, both } from 'ramda';
import { all, isEmpty, either, curry, allPass, is, both } from 'ramda';
import { included } from 'ramda-adjunct';

import SourceMapElement from '../elements/SourceMap';
import createPredicate from './helpers';
Expand Down Expand Up @@ -166,3 +167,29 @@ export const isSourceMapElement = createPredicate(
export const hasElementSourceMap = createPredicate(() => {
return (element) => isSourceMapElement(element.meta.get('sourceMap'));
});

export const includesSymbols = curry(
<T extends Element>(symbols: string[], element: T): boolean => {
if (isEmpty(symbols)) {
return true;
}

const elementSymbols = element.attributes.get('symbols');

if (!isArrayElement(elementSymbols)) {
return false;
}

return all(included(elementSymbols.toValue()), symbols);
},
);

export const includesClasses = curry(
<T extends Element>(classes: string[], element: T): boolean => {
if (isEmpty(classes)) {
return true;
}

return all(included(element.classes.toValue()), classes);
},
);
107 changes: 107 additions & 0 deletions apidom/packages/apidom/test/predicates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
isRefElement,
isSourceMapElement,
hasElementSourceMap,
includesSymbols,
includesClasses,
SourceMapElement,
} from '../../src';

Expand Down Expand Up @@ -665,4 +667,109 @@ describe('predicates', function () {
});
});
});

context('includesSymbols', function () {
context('given intersecting symbols', function () {
specify('should return true', function () {
const element = new ObjectElement();
element.attributes.set('symbols', ['symbol1', 'symbol2', 'symbol3']);

assert.isTrue(includesSymbols(['symbol1', 'symbol3'], element));
});
});

context('given no symbols key in attributes', function () {
let element;

beforeEach(function () {
element = new ObjectElement();
});

context('and providing at least one search symbol', function () {
specify('should return false', function () {
assert.isFalse(includesSymbols(['symbol'], element));
});
});

context('and providing no search symbol', function () {
specify('should return true', function () {
assert.isTrue(includesSymbols([], element));
});
});
});

context('given empty symbols list in attributes', function () {
let element;

beforeEach(function () {
element = new ObjectElement();
element.attributes.set('symbols', []);
});

context('and providing at least one search symbol', function () {
specify('should return false', function () {
assert.isFalse(includesSymbols(['symbol'], element));
});
});

context('and providing no search symbol', function () {
specify('should return true', function () {
assert.isTrue(includesSymbols([], element));
});
});
});

context('given no intersecting symbols', function () {
specify('should return false', function () {
const element = new ObjectElement();
element.attributes.set('symbols', ['symbol1', 'symbol2', 'symbol3']);

assert.isFalse(includesSymbols(['xxx'], element));
});
});
});

context('includesClasses', function () {
context('given intersecting classes', function () {
specify('should return true', function () {
const element = new ObjectElement();
element.classes.push('class1');
element.classes.push('class2');
element.classes.push('class3');

assert.isTrue(includesClasses(['class1', 'class2'], element));
});
});

context('given empty classes list in attributes', function () {
let element;

beforeEach(function () {
element = new ObjectElement();
});

context('and providing at least one search class', function () {
specify('should return false', function () {
assert.isFalse(includesClasses(['class'], element));
});
});

context('and providing no search class', function () {
specify('should return true', function () {
assert.isTrue(includesClasses([], element));
});
});
});

context('given no intersecting classes', function () {
specify('should return false', function () {
const element = new ObjectElement();
element.classes.push('class1');
element.classes.push('class2');
element.classes.push('class3');

assert.isFalse(includesClasses(['xxx'], element));
});
});
});
});

0 comments on commit 93e49aa

Please sign in to comment.