2016-08-29 1 views
0

Я пытаюсь преобразовать переменную среды в объект значений для конфигурации в JavaScript, но я не знаю, как это сделать.Как преобразовать переменную среды в объект в JS?

Идея будет выводить SAMPLE_ENV_VAR=value как:

{ 
    sample: { 
     env: { 
      var: value 
     } 
    } 
} 

То, что я до сих пор:

const _ = require('lodash'); 
const process = require('process'); 

_.each(process.env, (value, key) => { 
    key = key.toLowerCase().split('_'); 
    // Convert to object here 
} 

ответ

2

Я понятия не имею, почему вы хотите, чтобы достичь этого. Но в любом случае, здесь более полное решение, основанное на вашем:

const _ = require('lodash'); 
const result = {}; 

// We'll take the following as an example: 
// process.env = { HELLO_WORLD_HI: 5 } 
// We'll expect the following output: 
// result = { hello: { world: { hi: 5 } } } 
_.each(process.env, (value, key) => { 
    // We'll separate each key every underscore. 
    // In simple terms, this will turn: 
    // "HELLLO_WORLD_HI" -> ['HELLO', 'WORLD', 'HI'] 
    const keys = key.toLowerCase().split('_'); 

    // We'll start on the top-level object 
    let current = result; 

    // We'll assign here the current "key" we're iterating on 
    // It will have the values: 
    // 'hello' (1st loop), 'world' (2nd), and 'hi' (last) 
    let currentKey; 

    // We'll iterate on every key. Moreover, we'll 
    // remove every key (starting from the first one: 'HELLO') 
    // and assign the removed key as our "currentKey". 
    // currentKey = 'hello', keys = ['world', 'hi'] 
    // currentKey = 'world', keys = ['hi'], and so on.. 
    while ((currentKey = keys.shift())) { 
     // If we still have any keys to nest, 
     if (keys.length) { 
      // We'll assign that object property with an object value 
      // result =// { HELLO: {} } 
      current[currentKey] = {}; 

      // And then move inside that object so 
      // could nest for the next loop 
      // 1st loop: { HELLO: { /*We're here*/ } } 
      // 2nd loop: { HELLO: { WORLD: { /*We're here*/ } } } 
      // 3rd loop: { HELLO: { WORLD: { HI : { /*We're here*/ } } } } 
      current = current[currentKey]; 
     } else { 
      // Lastly, when we no longer have any key to nest 
      // e.g., we're past the third loop in our example 
      current[currentKey] = process.env[key] 
     } 
    } 
}); 

console.log(result); 

просто положить:

  • Мы будем цикл по каждой переменной окружения (from process.env)
  • Split имя ключа с подчеркиванием , и, опять же, петля каждый ключ (['HELLO', 'WORLD', 'HI'])
  • Назначают его к объекту ({ hello: {} } ->{ hello: { world: {} } } ->{ hello: world: { hi: ? } } })
  • Когда мы больше не имеют каких-либо клавиш влево, не назначить его фактическое значение ({ hello: { world: { hi: 5 } } })
0

достаточно странно, я только что закончил код для этой последней ночи для личного проекта. То, что я закончил с использованием не идеально, но работает для меня:

export function keyReducer(
    src: any, 
    out: any, 
    key: string, 
    pre: string, 
    del: string 
): ConfigScope { 
    const path = key.toLowerCase().split(del); 
    if (path[0] === pre.toLowerCase()) { 
    path.shift(); 
    } 

    if (path.length === 1) { // single element path 
    const [head] = path; 
    out[head] = src[key]; 
    } else { 
    const tail = path.pop(); 
    const target = path.reduce((parent: any, next: string) => { 
     if (parent[next]) { 
     return parent[next]; 
     } else { 
     return (parent[next] = <ConfigScope>{}); 
     } 
    }, out); 
    target[tail] = src[key]; 
    } 
    return out; 
} 

static fromEnv(env: Environment, {prefix = 'ABC', delimiter = '_'} = {}) { 
    const data: ConfigScope = Object.keys(env).filter(key => { 
    return StringUtils.startsWith(key, prefix); 
    }).reduce((out, key) => { 
    return keyReducer(env, out, key, prefix, '_'); 
    }, <ConfigScope>{}); 
    return new Config(data); 
} 

(с примечаниями типа машинопись)

Идея заключается в том, чтобы разделить каждый ключ, создать целевые объекты на пути вниз, затем установите окончательное значение.

0

Это мой быстрый взять на него:

var object = {}; // the object to store the value in 
var name = "SAMPLE_ENV_VAR"; // the environment variable key 
var value = "value"; // the value of the environment variable 

// helper function to automatically create an inner object if none exists 
function getOrCreateInnerObj(obj, name) { 
    if (!obj.hasOwnProperty()) { 
    obj[name] = {}; 
    } 
    return obj[name]; 
} 

// an array of the individual parts (e.g. ["sample", "env", "var"]) 
var keyParts = name.toLowerCase().split("_"); 

// innerObj will contain the second to last element object in the tree based on the array of keys 
var innerObj = getOrCreateInnerObj(object, keyParts[0]); 
for (var i = 1; i < keyParts.length - 1; i++) { 
    innerObj = getOrCreateInnerObj(innerObj, keyParts[i]); 
} 

// set the value itself 
innerObj[keyParts[keyParts.length - 1]] = value; 

$("body").html(JSON.stringify(object)); 

Суть ее в том, для всех, кроме последнего элемента в массиве ключевых частей, Вы получаете или создать объект в текущем родительского объекта для этот ключ, и как только вы повторите это для всех, кроме последнего ключа, у вас будет второй-второй внутренний объект, который вы можете установить.

Edit: Working example

Edit 2: Here гораздо чище пример, который использует немного рекурсии сделать то же самое

0
const basic = {}; 
let current; 
`YOUR_VARIABLE_NAME` 
    .split(`_`) 
    .forEach((item, index, array) => { 
    if(index === 0) { 
     return current = basic[item] = {}; 
    } 
    if(index === array.length - 1) { 
     return current[item] = process.env.HE_LO_NA; 
    } 
    current = current[item] = {}; 
}); 

console.log(require('util').inspect(basic, {depth: 10})); 
Смежные вопросы