@creditkarma/dynamic-config
Version:
Dynamic Config for Node.js backed by Consul and Vault
177 lines • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.memoize = exports.dashToCamel = exports.splitKey = exports.normalizePath = exports.parseArrayKey = exports.isNumeric = exports.isObject = exports.isSomething = exports.isNothing = exports.isPrimitive = exports.isPrimitiveType = exports.readFirstMatch = exports.readFromEnvOrProcess = exports.readFromEnv = exports.readValueFromArgs = void 0;
const MALFORMED_ARGUMENT = '<Error[malformed argument]>';
function readValueFromArgs(key, args) {
return args
.filter((next) => {
return next.startsWith(key);
})
.map((match) => {
const parts = match.split('=');
if (parts.length === 2) {
return parts[1];
}
else {
return MALFORMED_ARGUMENT;
}
})
.filter((next) => {
return next !== MALFORMED_ARGUMENT;
})[0];
}
exports.readValueFromArgs = readValueFromArgs;
function readFromEnv(key) {
const value = process.env[key];
if (value === undefined || value === 'undefined') {
return undefined;
}
else {
return value;
}
}
exports.readFromEnv = readFromEnv;
function readFromEnvOrProcess(key) {
return readValueFromArgs(key, process.argv) || readFromEnv(key);
}
exports.readFromEnvOrProcess = readFromEnvOrProcess;
function readFirstMatch(...keys) {
if (keys.length === 0) {
return undefined;
}
else {
const [head, ...tail] = keys;
const value = readFromEnvOrProcess(head);
if (value === undefined) {
return readFirstMatch(...tail);
}
else {
return value;
}
}
}
exports.readFirstMatch = readFirstMatch;
function isPrimitiveType(type) {
return type === 'number' || type === 'string' || type === 'boolean';
}
exports.isPrimitiveType = isPrimitiveType;
function isPrimitive(obj) {
return isPrimitiveType(typeof obj);
}
exports.isPrimitive = isPrimitive;
function isNothing(obj) {
return obj === null || obj === undefined;
}
exports.isNothing = isNothing;
function isSomething(obj) {
return !isNothing(obj);
}
exports.isSomething = isSomething;
function isObject(obj) {
return obj !== null && !Array.isArray(obj) && typeof obj === 'object';
}
exports.isObject = isObject;
function isNumeric(val) {
return val >= '0' && val <= '9';
}
exports.isNumeric = isNumeric;
/**
* This will handle reading a key that contains an array index.
*
* Example:
*
* 'databases[2]' -> { key: 'databases', index: 2 }
*
* TODO:
* This will not handle multi-dimensional arrays
*
* Example:
*
* databases[1][4] -> { key: 'databases[1]', index: 4 }
*/
function parseArrayKey(rawKey) {
const len = rawKey.length;
let cursor = len - 1;
// bail early if we know the last char isn't a right bracket
if (rawKey.charAt(cursor) === ']') {
const stack = [];
let index = '';
while (cursor >= 0) {
const next = rawKey.charAt(cursor);
// Bust out when we get to our right bracket
if (next === '[') {
if (stack.length === 1 && index !== '') {
return {
key: rawKey.substring(0, cursor),
index: parseInt(index, 10),
};
}
else {
return null;
}
}
else if (next === ']') {
stack.push(next);
}
else if (stack.length === 1 && isNumeric(next)) {
index = next + index;
}
cursor -= 1;
}
}
return null;
}
exports.parseArrayKey = parseArrayKey;
function normalizePath(key) {
const parts = splitKey(key).reduce((acc, next) => {
const arrayKey = parseArrayKey(next);
if (arrayKey !== null) {
acc.push(arrayKey.key);
acc.push(arrayKey.index);
}
else {
acc.push(next);
}
return acc;
}, []);
return parts.join('.');
}
exports.normalizePath = normalizePath;
function splitKey(key) {
return (key || '')
.split('.')
.map((val) => {
return val.trim();
})
.filter((val) => {
return val !== '';
});
}
exports.splitKey = splitKey;
function dashToCamel(str) {
const parts = str.split('-');
if (parts.length > 1) {
const base = parts
.map((part) => {
return (part.charAt(0).toUpperCase() +
part.substring(1).toLocaleLowerCase());
})
.join('');
return base.charAt(0).toLocaleLowerCase() + base.substring(1);
}
else {
return str;
}
}
exports.dashToCamel = dashToCamel;
function memoize(fn) {
let cachedValue;
return (...args) => {
if (cachedValue === undefined) {
cachedValue = fn(...args);
}
return cachedValue;
};
}
exports.memoize = memoize;
//# sourceMappingURL=basic.js.map