GithubHelp home page GithubHelp logo

ref-parser's Introduction

Ref-parser

install

npm i --save ref-parser

Usage

const refParser = require('ref-parser')();

var obj = {
    refA: "valueA",
    refB: "%refA%"
};

refParser.parse(obj);

expect(obj).to.deep.equals({
    refA: "valueA",
    refB: "valueA"
}); // === TRUE

## Options

const refParser = require('ref-parser')({
    referenceCharKey: "=",  // Default is %
    recursive: true,       
    global: {
        env: process.env
    },                      // Merged with obj to parse, removed after if removeGlobal option is TRUE
    removeGlobal: true      
});

process.env.NODE_ENV = "development";

var obj = {
    ref: "=env.NODE_ENV="
};

refParser.parse(obj);

expect(obj).to.deep.equals({
    ref: "development"
}); // === TRUE

API

parse(obj: Object, key?: String)

Parse all references inside obj parameter. If key is defined parse only references inside this key

parseReferences(str: String, obj: Object): any

Return the parsed references from str parameters, all references values come from obj parameters.

getReferenceKeys(str: String, referenceChar?: String): Array<String>

Return references keys from the str parameters. A reference key look like <referenceChar><ref-path><referenceChar> (for example %refA%)

hasReferences(str: String, referenceChar?: String): Boolean

Return trus if str parameter has one or more references.

Full example

var refParser = require('ref-parser')();

var obj = {
    refA: "valueA",
    // Simple reference
    refB: "%refA%",
    // Multi references
    refC: "%refA%-%refB%",
    refD: {
        refE: 0,
        refNull: null,
        refUndefined: undefined
    },
    refFalse: false,
    refTrue: true,
    // Multi references with object key and reference to a reference
    refF: "%refD.refE%-%refG%",
    refG: "%refD.refE%",
    // References inside array
    arrayA: [
        "value1",
        "%refD%",
        "%refTrue%"
    ],
    // Reference from array
    refH: "%arrayA.1.refE%",
    // Reference to null
    refI: "%refD.refNull%",
    // Reference to undefined
    refJ: "%refD.refUndefined%",
    refK: "%refD.refNull%-value",
    // Reference to false
    refL: "%refFalse%"
}

refParser.parse(obj);

expect(obj).to.deep.equals({
    refA: "valueA",
    refB: "valueA",
    refC: "valueA-valueA",
    refD: {
        refE: 0,
        refNull: null,
        refUndefined: undefined
    },
    refFalse: false,
    refTrue: true,
    refF: "0-0",
    refG: 0,
    arrayA: [
        "value1",
        {
            refE: 0,
            refNull: null,
            refUndefined: undefined
        },
        true
    ],
    refH: 0,
    refI: null,
    refJ: undefined,
    refK: "null-value",
    refL: false
}); // === TRUE

Test

npm test

ref-parser's People

Contributors

nbonneau avatar

Watchers

James Cloos avatar  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.