簡體   English   中英

如何將環境變量轉換為 JS 中的對象?

[英]How do I convert an environment variable to an object in JS?

我正在嘗試將環境變量轉換為 JavaScript 中的配置值對象,但我不知道實現這一目標的最佳方法。

這個想法是將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
}

這是基於您的更完整的解決方案:

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
  • 用下划線分割鍵名,並再次循環每個鍵( ['HELLO', 'WORLD', 'HI']
  • 將它分配給一個對象( { hello: {} } -> { hello: { world: {} } } -> { hello: world: { hi: ? } } }
  • 當我們不再有任何鍵時,將其分配給實際值( { hello: { world: { hi: 5 } } }

有趣的是,我昨晚剛剛完成了一個個人項目的代碼。 我最終使用的並不理想,但對我有用:

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);
}

(帶有 TypeScript 類型注釋)

這里的想法是拆分每個鍵,在向下的過程中創建目標對象,然后設置最終值。

這是我的快速理解:

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));

它的要點是,對於除關鍵部分數組中的最后一個元素之外的所有元素,您都可以在當前父對象中為該鍵獲取或創建一個對象,一旦您對除最后一個鍵之外的所有鍵重復此操作,您就可以“將有倒數第二個內部對象,然后您可以設置該值。

編輯:工作示例

編輯 2:是一個更清晰的例子,它使用一點遞歸來完成同樣的事情

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}));
const _ = require('lodash');
const process = require('process');

const result = Object.entries(process.env).reduce((acc, [key, value]) => {
    _.set(acc, key.toLowerCase().replace('_', '.'), value);
    return acc;
}, {})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM