Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Provide predicates #12

Open
kylef opened this issue May 22, 2018 · 1 comment
Open

Provide predicates #12

kylef opened this issue May 22, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@kylef
Copy link
Member

kylef commented May 22, 2018

See refractproject/minim-api-description#38

@kylef kylef added the enhancement New feature or request label May 22, 2018
pksunkara added a commit that referenced this issue Nov 30, 2018
pksunkara pushed a commit that referenced this issue Dec 12, 2018
@pksunkara
Copy link
Contributor

Comment by @char0n

Hi,

It think we should have a system of functional predicates and expose them as new module called predicates as part of minim-api-description. Currently people have to use a lot of imperative string comparisons when using minim and identifying elements. And that doesn't scale, is prone to errors and is not compo-sable in any way.

Imperative aprroach

apiElement.children.filter(element => element.element !== 'copy');

Declarative predicate approach

apiElement => apiElement.children.filter(isNotCopy);

This is the list of predicates that I am currently using. Minim is using lodash so I implemented the predicates using lodash.

import { get, curry, overEvery, invokeArgs, negate, overSome } from 'lodash/fp';


// isElementType :: String -> Element -> Boolean
export const isElementType = curry((type, element) => get('element', element) === type);

// isCategory :: Element -> Boolean
export const isCategory = isElementType('category');

// isApi :: Element -> Boolean
export const isApi = overEvery([isCategory, invokeArgs(['classes', 'contains'], ['api'])]);

// isNotApi :: Element -> Boolean
export const isNotApi = negate(isApi);

// isCopy :: Element -> Boolean
export const isCopy = isElementType('copy');

// isNotCopy :: Element -> Boolean
export const isNotCopy = negate(isCopy);

// isResource :: Element -> Boolean
export const isResource = isElementType('resource');

// isNotResource :: Element -> Boolean
export const isNotResource = negate(isResource);

// isResourceGroup :: Element -> Boolean
export const isResourceGroup = overEvery([isCategory, invokeArgs(['classes', 'contains'], ['resourceGroup'])]);

// isNotResourceGroup :: Element -> Boolean
export const isNotResourceGroup = negate(isResourceGroup);

// isHttpTransaction :: Element -> Boolean
export const isHttpTransaction = isElementType('httpTransaction');

// isNotHttpTransaction :: Element -> Boolean
export const isNotHttpTransaction = negate(isHttpTransaction);

// isDataStructure :: Element -> Boolean
export const isDataStructure = isElementType('dataStructure');

// isNotDataStructure :: Element -> Boolean
export const isNotDataStructure = negate(isDataStructure);

// isAuthSchemes :: Element -> Boolean
export const isAuthSchemes = overEvery([isCategory, invokeArgs(['classes', 'contains'], ['authSchemes'])]);

// isNotAuthSchemes :: Element -> Boolean
export const isNotAuthSchemes = negate(isAuthSchemes);

// isOAuth2Scheme :: Element -> Boolean
export const isOAuth2Scheme = isElementType('OAuth2 Scheme');

// isTokenAuthenticationScheme :: Element -> Boolean
export const isTokenAuthenticationScheme = isElementType('Token Authentication Scheme');

// isBasicAuthenticationScheme :: Element -> Boolean
export const isBasicAuthenticationScheme = isElementType('Basic Authentication Scheme');

// isAuthScheme :: Element -> Boolean
export const isAuthScheme = overSome([
  isOAuth2Scheme,
  isTokenAuthenticationScheme,
  isBasicAuthenticationScheme,
]);

// isNotAuthScheme :: Element -> Boolean
export const isNotAuthScheme = negate(isAuthScheme);

// isAnnotation :: Element -> Boolean
export const isAnnotation = isElementType('annotation');

// isNotAnnotation :: Element -> Boolean
export const isNotAnnotation = negate(isAnnotation);

// isAnnotationWarning :: Element -> Boolean
export const isAnnotationWarning = overEvery([isAnnotation, invokeArgs(['classes', 'contains'], ['warning'])]);

// isAnnotationError :: Element -> Boolean
export const isAnnotationError = overEvery([isAnnotation, invokeArgs(['classes', 'contains'], ['error'])]);

// isTransition :: Element -> Boolean
export const isTransition = isElementType('transition');

// isNotTransition :: Element -> Boolean
export const isNotTransition = negate(isTransition);

I can issue a PR with test If we agree this is a way to go.

PS: I will be adding more predicates via PRs as I have usecases for them.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants