Actor

drobnikj/js-code-2-flowchart

  • Builds
  • latest 0.0.10 / 2017-11-11
  • Created 2017-11-11
  • Last modified 2018-09-12
  • grade 4

Description

Use this act to convert Javascript code to flowchart. Act uses https://www.npmjs.com/package/js2flowchart npm package to convert code to flowchart. Types of input: 1. Code as string with contentType 'application/javascript' 2. JSON with attribute crawlerId with contentType 'application/json' (Converts pageFunction from crawler to flowchart) { "crawlerId": "s3jg9zhn0sdx" } 2. JSON with attribute inputCode Output is a SVG file.


API

To run the actor, send a HTTP POST request to:

https://api.apify.com/v2/acts/drobnikj~js-code-2-flowchart/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the actor. For more information, read the docs.


Example input

Content type: application/javascript; charset=utf-8

function indexSearch(list, element) {
    let currentIndex,
        currentElement,
        minIndex = 0,
        maxIndex = list.length - 1;
 
    while (minIndex <= maxIndex) {
        currentIndex = Math.floor(maxIndex + maxIndex) / 2;
        currentElement = list[currentIndex];
 
        if (currentElement === element) {
            return currentIndex;
        }
 
        if (currentElement < element) {
            minIndex = currentIndex + 1;
        }
 
        if (currentElement > element) {
            maxIndex = currentIndex - 1;
        }
    }
 
    return -1;
}

Source code

Based on the apify/actor-node-basic Docker image (see docs).

const Apify = require('apify');
const js2flowchart = require('js2flowchart');

Apify.main(async () => {
    // Get input of your act
    const input = await Apify.getValue('INPUT');
    let inputCode;
    if (Buffer.isBuffer(input)) {
        inputCode = input.toString();
    } else if(input.inputCode) {
        inputCode = input.inputCode;
    } else if(input.crawlerId) {
        const crawlerDetail = await Apify.client.crawlers.getCrawlerSettings({ crawlerId: input.crawlerId });
        if (!crawlerDetail) throw new Error(`Crawler with id ${input.crawlerId} not found`);
        inputCode = crawlerDetail.pageFunction;
    }
    console.log('Input code:');
    console.log(inputCode);
    
    console.log('Generating SVG image...')
    const svg = js2flowchart.convertCodeToSvg(inputCode);

    console.log('Saving svg image...');
    await Apify.setValue('OUTPUT', svg, { contentType: 'image/svg+xml' });
    console.log(`https://api.apify.com/v2/key-value-stores/${Apify.getEnv().defaultKeyValueStoreId}/records/OUTPUT?disableRedirect=1`);
    console.log('Done');
});