0.1.0 • Published 5 years ago

create-runkit-content-from-function v0.1.0

Weekly downloads
9
License
-
Repository
-
Last release
5 years ago

create-runkit-content-from-function

Purpose

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)

Use

import createRunkitContentFromJSFunction from 'create-runkit-content-from-function';

:warning: You'll need to redefine require for the function that will use require ( so it's valid js ) :warning:

such as :

const require = () => {};

:warning: functions must be not in one line so it can work :warning:

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

In situation example :

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 !