Code Monkey home page Code Monkey logo

mjedit's Introduction

mjedit

const Mjedit = require('mjedit');
const mjedit = new Mjedit(
    filename,
    {
        propName: {
            // Default value
            value: Any,

            // Text printed to the user
            text: String,

            // Text printed to the user when the validator function return false
            invalidText: [String],

            // Return true is the value is valid, false otherwise
            validator: [(value)],

            // Return a transformed value from the answer
            parser: [(answer)],

            // Indicates if the input is required, default false
            required: [Bool],

            // Text printed to the user when a required field was left empty
            requiredText: [String]
        }
    },
    {
        // Will not warn you if the file "filename" is not found, default false
        silent = [true]
    }
)

[] -> Optionals fields

(arg) -> Functions that take arg as argument

// Load the file (filename) as json if it exist
// Ask the questions to the user through stdin
// Save resulting data to the file as json
mjedit.run(); // async function

Example

const Mjedit = require('mjedit');
const mjedit = new Mjedit(
    'hello_mjedit.json',
    {
        idValid: {
            value: false,
            text: 'Are ids valid? (true/false)',
            validator: (value) => ['true', 'false'].includes(value),
            required: true,
            requiredText: 'You must specify if ids are valid'
        },
        ids: {
            value: [],
            text: 'Enter ids (csv)',
            invalidText: 'Invalid ids, each id should be 10 characteres long',
            validator: (ids) => ids.every(id => id.length === 10),
            parser: (answer) => answer.split(',').map(id => id.trim())
        }
    }
);

mjedit.run();
$ node app.js
No existing metadata file found for hello_mjedit.json
Are ids valid? (true/false): true
Enter ids (csv): 1234567890, 12345
Invalid ids, each id should be 10 characteres long
Enter ids (csv): 1234567890, 1234554321

$ cat hello_mjedit.json
{"idValid":"true","ids":["1234567890","1234554321"]}

$ node app.js
Are ids valid? (true/false):
You must specify if ids are valid
Are ids valid? (true/false): true
Enter ids (csv): 1111111111

$ cat hello_mjedit.json
{"idValid":"true","ids":["1111111111"]}

mjedit's People

Contributors

mderam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.