@incanta/config
Advanced tools
Comparing version 0.1.22 to 0.2.0
@@ -5,2 +5,3 @@ export interface IConfigOptions { | ||
export declare class Loader { | ||
static convertKebabToCamelCase(obj: any): any; | ||
static loadFile(filePath: string): any; | ||
@@ -7,0 +8,0 @@ static load(folder: string): any; |
@@ -14,2 +14,23 @@ "use strict"; | ||
class Loader { | ||
// add additional camelCase keys for any kebab-case keys (without overwriting existing keys) | ||
static convertKebabToCamelCase(obj) { | ||
const newObj = {}; | ||
for (const key of Object.keys(obj)) { | ||
if (typeof obj[key] === "object" && | ||
!Array.isArray(obj[key]) && | ||
obj[key] !== null) { | ||
newObj[key] = Loader.convertKebabToCamelCase(obj[key]); | ||
} | ||
else { | ||
newObj[key] = obj[key]; | ||
} | ||
const newKey = key.replace(/-([a-zA-Z0-9])/g, function (_, match) { | ||
return match.toUpperCase(); | ||
}); | ||
if (newKey !== key) { | ||
newObj[newKey] = newObj[key]; | ||
} | ||
} | ||
return newObj; | ||
} | ||
static loadFile(filePath) { | ||
@@ -24,22 +45,25 @@ if (!fs_1.default.existsSync(filePath)) { | ||
const extension = fileParts[fileParts.length - 1]; | ||
let obj; | ||
if (/^ya?ml$/i.exec(extension)) { | ||
// yaml file | ||
return js_yaml_1.default.load(fileContents); | ||
obj = js_yaml_1.default.load(fileContents); | ||
} | ||
else if (/^json$/i.exec(extension)) { | ||
// json file | ||
return JSON.parse(fileContents); | ||
obj = JSON.parse(fileContents); | ||
} | ||
else if (/^jsonc$/i.exec(extension)) { | ||
// jsonc file | ||
return (0, jsonc_parser_1.parse)(fileContents); // todo: handle error callback | ||
obj = (0, jsonc_parser_1.parse)(fileContents); // todo: handle error callback | ||
} | ||
else if (/^json5$/i.exec(extension)) { | ||
// json5 file | ||
return json5_1.default.parse(fileContents); | ||
obj = json5_1.default.parse(fileContents); | ||
} | ||
else { | ||
console.log(`Invalid file name ${filePath}. Only yml, yaml, json, jsonc, json5 extensions are supported (case insensitive).`); | ||
return {}; | ||
obj = {}; | ||
} | ||
const newObj = Loader.convertKebabToCamelCase(obj); | ||
return newObj; | ||
} | ||
@@ -46,0 +70,0 @@ static load(folder) { |
{ | ||
"name": "@incanta/config", | ||
"version": "0.1.22", | ||
"version": "0.2.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -13,2 +13,29 @@ import fs from "fs"; | ||
export class Loader { | ||
// add additional camelCase keys for any kebab-case keys (without overwriting existing keys) | ||
public static convertKebabToCamelCase(obj: any): any { | ||
const newObj: any = {}; | ||
for (const key of Object.keys(obj)) { | ||
if ( | ||
typeof obj[key] === "object" && | ||
!Array.isArray(obj[key]) && | ||
obj[key] !== null | ||
) { | ||
newObj[key] = Loader.convertKebabToCamelCase(obj[key]); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
const newKey = key.replace(/-([a-zA-Z0-9])/g, function (_, match) { | ||
return match.toUpperCase(); | ||
}); | ||
if (newKey !== key) { | ||
newObj[newKey] = newObj[key]; | ||
} | ||
} | ||
return newObj; | ||
} | ||
public static loadFile(filePath: string): any { | ||
@@ -26,14 +53,15 @@ if (!fs.existsSync(filePath)) { | ||
let obj: any; | ||
if (/^ya?ml$/i.exec(extension)) { | ||
// yaml file | ||
return YAML.load(fileContents); | ||
obj = YAML.load(fileContents); | ||
} else if (/^json$/i.exec(extension)) { | ||
// json file | ||
return JSON.parse(fileContents); | ||
obj = JSON.parse(fileContents); | ||
} else if (/^jsonc$/i.exec(extension)) { | ||
// jsonc file | ||
return parseJsonc(fileContents); // todo: handle error callback | ||
obj = parseJsonc(fileContents); // todo: handle error callback | ||
} else if (/^json5$/i.exec(extension)) { | ||
// json5 file | ||
return JSON5.parse(fileContents); | ||
obj = JSON5.parse(fileContents); | ||
} else { | ||
@@ -43,4 +71,8 @@ console.log( | ||
); | ||
return {}; | ||
obj = {}; | ||
} | ||
const newObj = Loader.convertKebabToCamelCase(obj); | ||
return newObj; | ||
} | ||
@@ -47,0 +79,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39747
713