run-my-code

Execute javascript code from a string based on new function.

Usage no npm install needed!

<script type="module">
  import runMyCode from 'https://cdn.skypack.dev/run-my-code';
</script>

README

run-my-code

Execute javascript code from a string based on new function, inspired by zx.

Install

$ npm install --save run-my-code

Usage

Tagged Template:

const $ = require('run-my-code');

const obj = { foo: 'foo' };
const arr = [1, 2];

console.log(

	
		
		
		
		
		
		
		
	npm:run-my-code | Skypack
	
		
		
		
		return 1 + 1`); // 2
console.log(

	
		
		
		
		
		
		
		
	npm:run-my-code | Skypack
	
		
		
		
		return ${obj}`); // { foo: 'foo' }
console.log(

	
		
		
		
		
		
		
		
	npm:run-my-code | Skypack
	
		
		
		
		return ${arr}.map((n) => n * 2)`); // [2, 4]

Normal Function:

const $ = require('run-my-code');

console.log($('return 1 + 1')); // 2

With Context:

console.log($({ a: 1, b: 2 })(`return a + b`)); // 3
console.log($({ a: 1, b: 2 })`return a + b`); // 3

License

MIT