This package ( no dependencies ! ) exposes a simple function to transform a js function into a string that React-runkit can consume as source ( via source props)
import createRunkitContentFromJSFunction from 'create-runkit-content-from-function';
such as :
const require = () => {};
bad :
const test = () => 42;
good :
const test = () => {
return 42;
};
general use case :
const myFunction = () => {
// demonstration of filter
const collection = [1,2];
const filtered = collection.filter(x => x > 2);
console.log(filtered);
}
will populate runkit with
// demonstration of filter
const collection = [1,2];
const filtered = collection.filter(x => x > 2);
console.log(filtered);
This use case should not come so often since we usually don not want our runkit to be populated with an unique instruction such as
return 42
import Embed from 'react-runkit'
import createRunkitContentFromJSFunction from 'create-runkit-content-from-function';
// only to allow to require some package in the runkit
const require = () => {}
const codeToInsertInEmbedRunkit = () => {
const fetch = require('fetch');
fetch('someUrl').then(()=> console.log('fetched ! '))
}
const ComponentDisplayed = () => <Embed source={createRunkitContentFromJSFunction(codeToInsertInEmbedRunkit)}/>
( but I wanted to let you know )
Please open an issue if needed.
Happy coding !