Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

env-spec

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

env-spec - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

6

package.json
{
"name": "env-spec",
"version": "0.1.0",
"version": "0.2.0",
"description": "modify env files to HTML",

@@ -11,8 +11,10 @@ "main": "index.js",

"jest": "^23.4.1",
"prettier": "1.13.7"
"prettier": "^1.13.7",
"prettier-check": "^2.0.0"
},
"scripts": {
"test": "jest",
"lint": "prettier-check src/*.js",
"format": "prettier --write src/*.js"
}
}
//function that checks if the env value is valid
//value cannot start with a digit and must only contain alphanumeric characters or an underscore
const checkValidationOfValues = envSpecString => {
const validTypes = [
"color",
"date",
"datetime-local",
"email",
"month",
"number",
"password",
"tel",
"text",
"time",
"url",
"week"
];
const alphanumericThatDoesNotStartWithDigit = /^[A-Z_][0-9A-Z_]*$/;
let envValuesChecked = envSpecString.split("\n"); //split lines based on \n character
let envValArray = parseVarFromType(envSpecString.trim().split("\n")); //split lines based on \n character and parse them
let checkValidation = true;
return (envValuesChecked = envValuesChecked.filter(element => {
return element.match(alphanumericThatDoesNotStartWithDigit); //keep valid values
//envValArray is a two-dimensional array containing only the types and variables
//element[0] is the variable e.g. ADMIN_EMAIL
//element[1] is the type e.g. test
envValArray = envValArray.filter(element => {
//check for valid variables AND types
if (
element[0].match(alphanumericThatDoesNotStartWithDigit) &&
validTypes.includes(element[1])
) {
return element;
} else {
checkValidation = false;
}
});
//in case of one or more invalid variables or types, return error
if (checkValidation === true) {
return envValArray;
} else {
return null;
}
};
//function that separates the variables from their types and returns them as a two-dimensional array
const parseVarFromType = envSpecAsArray => {
return (envSpecAsArray = envSpecAsArray.map(element => {
if (element.includes(":")) {
element = element.split(":");
return [element[0].trim(), element[1].trim()];
} else {
//if untyped environmental variable set default as text
return [element.trim(), "text"];
}
}));

@@ -13,14 +59,23 @@ };

//function that returns the HTML code as a string
//envValues is a two-dimensional array containing the variable name and its type
const outputHTML = envValues => {
//create HTML format
envValues = envValues.map(
element =>
`<label for="env_spec_${element.toLowerCase()}">${element}</label>\n` +
`<input id="env_spec_${element.toLowerCase()}" name="${element.toLowerCase()}" />\n`
);
//return as string value
return envValues.join("");
if (envValues) {
envValues = envValues.map(
element =>
`<label for="env_spec_${element[0].toLowerCase()}">${
element[0]
}</label>\n` +
`<input id="env_spec_${element[0].toLowerCase()}" name="${element[0].toLowerCase()}" type="${
element[1]
}" />\n`
);
//return as string value
return envValues.join("");
}
//in case of syntax error , print HTML format
return "Error:Wrong Syntax";
};
//function that calls functions for final output
//function for final output
const envSpecToHTML = envSpec => {

@@ -27,0 +82,0 @@ return outputHTML(checkValidationOfValues(envSpec));

const envSpecToHTML = require("./main.js");
test("String starting with number", () => {
const testEnv1 = "\n1ADMIN_EMAIL\nDEBUG";
expect(envSpecToHTML(testEnv1)).toEqual(
'<label for="env_spec_debug">DEBUG</label>\n' +
'<input id="env_spec_debug" name="debug" />\n'
test("Valid input", () => {
const testEnv = "DATABASE_URL\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual(
'<label for="env_spec_database_url">DATABASE_URL</label>\n' +
'<input id="env_spec_database_url" name="database_url" type="text" />\n' +
'<label for="env_spec_admin_email">ADMIN_EMAIL</label>\n' +
'<input id="env_spec_admin_email" name="admin_email" type="email" />\n'
);
});
test("String containing wrong characters", () => {
const testEnv2 = "DATABASaaE_URL\nφφADMIN_EMAIL\nDEBUG\n";
expect(envSpecToHTML(testEnv2)).toEqual(
'<label for="env_spec_debug">DEBUG</label>\n' +
'<input id="env_spec_debug" name="debug" />\n'
);
test("Invalid environmental variable : starts with number", () => {
const testEnv = "1DATABASE_URL\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Valid input", () => {
const testEnv3 = "DATABASE_URL\nADMIN_EMAIL\nDEBUG";
expect(envSpecToHTML(testEnv3)).toEqual(
test("Invalid environmental variable : contains non alphanumeric characters", () => {
const testEnv = "DATABASE_URLαα\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Invalid environmental variable : contains lowercase letters", () => {
const testEnv = "database_url\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Invalid type", () => {
const testEnv = "\nADMIN_EMAIL: notgood";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Untyped environmental variable", () => {
const testEnv = "DATABASE_URL\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual(
'<label for="env_spec_database_url">DATABASE_URL</label>\n' +
'<input id="env_spec_database_url" name="database_url" />\n' +
'<input id="env_spec_database_url" name="database_url" type="text" />\n' +
'<label for="env_spec_admin_email">ADMIN_EMAIL</label>\n' +
'<input id="env_spec_admin_email" name="admin_email" />\n' +
'<label for="env_spec_debug">DEBUG</label>\n' +
'<input id="env_spec_debug" name="debug" />\n'
'<input id="env_spec_admin_email" name="admin_email" type="email" />\n'
);
});
test("Multiple invalid variables and types", () => {
const testEnv3 = "DATABASE_URLαα: αα\nADMIN_EMAIL:\n1DEBUG";
expect(envSpecToHTML(testEnv3)).toEqual("Error:Wrong Syntax");
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc