0.4.8 • Published 3 years ago

js-to-string v0.4.8

Weekly downloads
465
License
ISC
Repository
github
Last release
3 years ago

js-to-string

NPM version Build Status Dependency Status Coverage percentage Greenkeeper badge

const jsToString = require("js-to-string");

function foo(value) {
    let thing = true;
    let array = [1, 2, 3, 4, 5];
    if (!value) {
        thing = false;
    }
    return thing;
}

const stringFoo = jsToString(foo);

Options

Custom toString methods

Here's a good example of when to use a custom toString method.. if you've merged data outside the function that's being written to string you need to finalise the data first.

const jsToString = require("../lib");
const requireFromString = require("require-from-string");
const notEmpty = {
    data: function() {
        return {
            msg: "Hello world!",
            messageOuter: "Say Foo",
        };
    },
};

function FixData(oldData, newData) {
    const mergedData = Object.assign({}, oldData, newData);
    return function data() {
        return mergedData;
    };
}

const options = {
    functions: [
        {
            name: "data",
            toString: function(script) {
                const func = `module.exports = function data() { return ${jsToString(script())}; };`;
                const required = requireFromString(func);
                return required;
            },
        },
    ],
};

const fixedData = FixData(notEmpty.data(), {foo: true});
notEmpty.data = fixedData;
const result = jsToString(notEmpty, options);
console.log(result);
0.4.8

3 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago