Skip to content

Commit

Permalink
feat(playground): add s-expression and value interpretations
Browse files Browse the repository at this point in the history
Refs #476
  • Loading branch information
char0n committed Jul 9, 2021
1 parent d4aee1e commit 99a1f36
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions experiments/apidom-playground/src/playground/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export const resolveApiDOM = createAsyncThunk(
);

export const interpretApiDOM = createAsyncThunk('interpretApiDOMStatus', async (interpreter) => {
// pre-defined interpreters
if (['to-value', 's-expression'].includes(interpreter.toLowerCase())) {
return interpreter;
}
eval(interpreter); // eslint-disable-line no-eval
return interpreter;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const ApiDOMInterpreterDialog = ({ open, onClose }) => {
Interpreter function is a pure function that receives an Element instance and returns
string representation of an Element.
</Typography>
<Typography variant="caption" display="block" color="textSecondary" gutterBottom>
There are also couple of pre-defined handy interpreters: <strong>s-expression</strong>,{' '}
<strong>to-value</strong>
</Typography>
</DialogContent>
</Dialog>
);
Expand Down
10 changes: 9 additions & 1 deletion experiments/apidom-playground/src/playground/selectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSelector } from 'swagger-adjust';
import { isEmptyString, isNonEmptyString, isNull } from 'ramda-adjunct';
import { from, traverse, createNamespace } from 'apidom';
import { from, traverse, createNamespace, sexprs, toValue } from 'apidom';
/* eslint-disable camelcase */
import openApi3_1NsPlugin from 'apidom-ns-openapi-3-1';
import asyncApi2_0NsPlugin from 'apidom-ns-asyncapi-2-0';
Expand Down Expand Up @@ -58,6 +58,14 @@ export const selectApiDOMInterpretation = createSelector(
return apiDOM;
}

// pre-defined interpreters
if (interpreter.toLowerCase() === 's-expression') {
return sexprs(element);
}
if (interpreter.toLowerCase() === 'to-value') {
return JSON.stringify(toValue(element), null, 2);
}

const callback = eval(interpreter); // eslint-disable-line no-eval
let result = '';
traverse((el) => {
Expand Down

0 comments on commit 99a1f36

Please sign in to comment.