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.4.0 to 0.5.0

33

demo/main.js
var button = document.getElementById("myButton");
button.addEventListener("click", function getHTML() {
var textarea = document.getElementById("inputTextArea");
var textarea2 = document.getElementById("outputTextArea");
outputTextArea.value = envSpec(inputTextArea.value);
document.getElementById("finalOutput").innerHTML = envSpec(
inputTextArea.value
);
envSpec
.parse(inputTextArea.value)
.then(entries => entries.html())
.then(html => (outputTextArea.value = html))
.catch(error => (outputTextArea.value = " "));
envSpec
.parse(inputTextArea.value)
.then(entries => entries.html())
.then(html => (document.getElementById("env-spec-form").innerHTML = html))
.catch(
error =>
(document.getElementById("env-spec-form").innerHTML = error.message)
);
});
var button2 = document.getElementById("myButton2");
button2.addEventListener("click", function serializeForm() {
const form = document.getElementById("env-spec-form");
envSpec
.serializeForm(form)
.then(
entries =>
(document.getElementById(
"env-spec-form-serialized"
).innerHTML = JSON.stringify(entries))
);
});

2

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

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -27,5 +27,5 @@ /** @method checkValidationOfValues

const genericForCheckingRestrChoicesSyntax = /^\[(.*)\]$/;
const allowedTypes = validTypes.map(validType => `"${validType}"`).join(",");
let envSpecLines = parseVarFromType(envSpecString.trim().split("\n")); //split lines based on \n character and parse them
let checkValidation = true;
//envSpecEntries is an array containing entries with their and type or choices

@@ -39,3 +39,19 @@ //element.name is the variable e.g. ADMIN_EMAIL

//this would happen in case input is "DATA: number = " or "DATA :[4,2] = "
checkValidation = false;
throw new EnvSpecSyntaxError(
"Expected default value after =",
element.name
);
} else if (!element.name.match(alphanumericThatDoesNotStartWithDigit)) {
throw new EnvSpecSyntaxError(
"Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit.",
element.name
);
} else if (
!validTypes.includes(element.type) &&
!element.type.match(genericForCheckingRestrChoicesSyntax)
) {
throw new EnvSpecSyntaxError(
`Invalid variable type; it should be one of (${allowedTypes}) and \"=\" if there is a default value`,
element.name
);
} else {

@@ -45,4 +61,3 @@ if (

element.name.match(alphanumericThatDoesNotStartWithDigit) &&
validTypes.includes(element.type) &&
checkValidation === true
validTypes.includes(element.type)
) {

@@ -54,4 +69,3 @@ return element;

element.name.match(alphanumericThatDoesNotStartWithDigit) &&
element.type.match(genericForCheckingRestrChoicesSyntax) &&
checkValidation === true
element.type.match(genericForCheckingRestrChoicesSyntax)
) {

@@ -64,3 +78,6 @@ element.choices = element.type

//check for wrong syntax "DATA: [1, ]"
checkValidation = false;
throw new EnvSpecSyntaxError(
'Expected choice after ",".',
element.name
);
}

@@ -75,3 +92,6 @@ return choice.trim(); //trim valid choices

) {
checkValidation = false;
throw new EnvSpecSyntaxError(
"Invalid default value; it is not included in the provided restricted choices.",
element.name
);
}

@@ -82,3 +102,3 @@ return element;

else {
checkValidation = false;
throw new EnvSpecSyntaxError("Obscure syntax error!", element.name);
}

@@ -88,11 +108,39 @@ }

//in case of one or more invalid variables or types, return error
if (checkValidation === true) {
return envSpecEntries;
} else {
return null;
}
//in case all variables were valid return array of entry objects
checkForDoubles(envSpecEntries);
return envSpecEntries;
};
/** @method checkForDoubles
* @summary Checks if a variable name is appearing twice or more times.
* @param {Array} envSpecEntries contains the valid entry objects that we were going to print
*/
const checkForDoubles = envSpecEntries => {
tempNames = [];
envSpecEntries.forEach(element => {
if (tempNames.includes(element.name)) {
throw new EnvSpecSyntaxError(
`Invalid variable name; variable "${element.name}" already exists.`,
element.name
);
}
tempNames.push(element.name);
});
};
/**
* Class representing a Syntax Error object
* @class
* @augments Error
*/
class EnvSpecSyntaxError extends Error {
/**
* @param {string} message of error that occured
*@param {string} environmentalVar is the name of the environmental variable that was responsible for this error
*/
constructor(message, environmentalVar) {
super("EnvSpecSyntaxError: " + message);
this.nameOfVar = environmentalVar;
}
}
/**
* Class representing an Entry

@@ -116,4 +164,45 @@ * @class

}
/** @method html
* @summary Returns the rendered HTML for the given entry
* @returns {promise} that resolves to string
*/
html() {
const entry = outputHTML([this]);
return new Promise(function(resolve, reject) {
if (entry) {
resolve(entry);
} else {
reject("Error:Wrong Syntax");
}
});
}
}
/**
* Class representing an array of entry objects
* @class
*/
class EntryList {
/**
* @param {Array} entries includes {@link Entry} objects
*/
constructor(entries) {
this.entries = entries;
}
/** @method html
* @summary Returns the rendered HTML for the given array of entries
* @returns {promise} that resolves to string
*/
html() {
const entriesHTMLPromises = this.entries.map(entry => entry.html());
return new Promise(function(resolve, reject) {
Promise.all(entriesHTMLPromises)
.then(values => resolve(values.join("")))
.catch(error => reject(error));
});
}
}
/** @function parseVarFromType

@@ -220,15 +309,42 @@ * @summary Separates the variables' name from their types, their restricted choices and their default values and returns them as an array of {@link Entry} objects.

}
//in case of syntax error , print HTML format
return "Error:Wrong Syntax";
};
/** @function envSpecToHTML
* @desc gives final output using {@link outputHTML} and {@link checkValidationOfValues}
/** @function parse
* @desc transforms .env file to an array of valid values or an error message
* @param {string} envSpec the .env file given in string format
* @returns {string} HTML code
* @returns {promise} that resolves to an array of entry objets
*/
const envSpecToHTML = envSpec => {
return outputHTML(checkValidationOfValues(envSpec));
const parse = envSpecTxt => {
//returns promise ,when resolved returns EntryList OBJECT
return new Promise(function(resolve, reject) {
try {
const entriesList = new EntryList(checkValidationOfValues(envSpecTxt));
resolve(entriesList);
} catch (error) {
reject(error);
}
});
};
module.exports = envSpecToHTML;
/** @function serializeForm
* @desc transforms a HTML form to an array of variables with their values if given by user
* @param {HTMLFormElement} form the form completed by the user
* @returns {promise} that resolves to an array of strings
*/
const serializeForm = form => {
outputForm = [];
form.querySelectorAll("select,input").forEach(input => {
outputForm.push({ [input.name.toUpperCase()]: input.value || null });
});
return new Promise(function(resolve, reject) {
resolve(outputForm);
});
};
parse("DATA\nDATA")
.then(data => data.html())
.then(text => console.log(text))
.catch(e => console.log(e.message));
module.exports.serializeForm = serializeForm;
module.exports.EnvSpecSyntaxError = EnvSpecSyntaxError;
module.exports.parse = parse;

@@ -1,6 +0,8 @@

const envSpecToHTML = require("./main.js");
const envSpec = require("./main.js");
test("Valid input with type", () => {
const testEnv = "DATABASE_URL\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual(
return expect(
envSpec.parse(testEnv).then(data => data.html())
).resolves.toEqual(
'<label for="env_spec_database_url">DATABASE_URL</label>\n' +

@@ -15,3 +17,3 @@ '<input id="env_spec_database_url" name="database_url" type="text" />\n' +

const testEnv = "DATABASE_URL\nNUMBER :[0,1]";
expect(envSpecToHTML(testEnv)).toEqual(
expect(envSpec.parse(testEnv).then(data => data.html())).resolves.toEqual(
'<label for="env_spec_database_url">DATABASE_URL</label>\n' +

@@ -29,3 +31,3 @@ '<input id="env_spec_database_url" name="database_url" type="text" />\n' +

const testEnv = "DEBUG: [0, 1]=1";
expect(envSpecToHTML(testEnv)).toEqual(
expect(envSpec.parse(testEnv).then(data => data.html())).resolves.toEqual(
`<label for="env_spec_debug">DEBUG</label>\n` +

@@ -41,3 +43,3 @@ `<select id="env_spec_debug" name="debug">\n` +

const testEnv = "DATABASE_URL: email = test@mail.com";
expect(envSpecToHTML(testEnv)).toEqual(
expect(envSpec.parse(testEnv).then(data => data.html())).resolves.toEqual(
`<label for="env_spec_database_url">DATABASE_URL</label>\n` +

@@ -50,3 +52,3 @@ `<input id="env_spec_database_url" name="database_url" type="email" value="test@mail.com" />\n`

const testEnv = "DATABASE_URL: email = test@mail.com #comment";
expect(envSpecToHTML(testEnv)).toEqual(
expect(envSpec.parse(testEnv).then(data => data.html())).resolves.toEqual(
`<label for="env_spec_database_url">DATABASE_URL</label>\n` +

@@ -61,3 +63,3 @@ `<input id="env_spec_database_url" name="database_url" type="email" value="test@mail.com" />\n` +

"#DATABASE_URL: email = test@mail.com\nDATABASE_URL: email = test@mail.com";
expect(envSpecToHTML(testEnv)).toEqual(
expect(envSpec.parse(testEnv).then(data => data.html())).resolves.toEqual(
`<label for="env_spec_database_url">DATABASE_URL</label>\n` +

@@ -70,3 +72,8 @@ `<input id="env_spec_database_url" name="database_url" type="email" value="test@mail.com" />\n`

const testEnv = "1DATABASE_URL\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
"EnvSpecSyntaxError: Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit."
});
});

@@ -76,3 +83,8 @@

const testEnv = "DATABASE_URLαα\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
"EnvSpecSyntaxError: Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit."
});
});

@@ -82,3 +94,8 @@

const testEnv = "database_url\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
"EnvSpecSyntaxError: Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit."
});
});

@@ -88,3 +105,8 @@

const testEnv = "\nADMIN_EMAIL: notgood";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
'EnvSpecSyntaxError: Invalid variable type; it should be one of ("color","date","datetime-local","email","month","number","password","tel","text","time","url","week") and "=" if there is a default value'
});
});

@@ -94,3 +116,3 @@

const testEnv = "DATABASE_URL\nADMIN_EMAIL:email";
expect(envSpecToHTML(testEnv)).toEqual(
expect(envSpec.parse(testEnv).then(data => data.html())).resolves.toEqual(
'<label for="env_spec_database_url">DATABASE_URL</label>\n' +

@@ -104,4 +126,9 @@ '<input id="env_spec_database_url" name="database_url" type="text" />\n' +

test("Multiple invalid variables and types", () => {
const testEnv3 = "DATABASE_URLαα: αα\nADMIN_EMAIL:\n1DEBUG";
expect(envSpecToHTML(testEnv3)).toEqual("Error:Wrong Syntax");
const testEnv = "DATABASE_URLαα: αα\nADMIN_EMAIL:\n1DEBUG";
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
"EnvSpecSyntaxError: Invalid variable name; it should contain only latin alphanumeric characters, underscores and not start with a digit."
});
});

@@ -111,3 +138,8 @@

const testEnv = "DATABASE_URL\nADMIN_EMAIL:email\nDATA:[0,";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
'EnvSpecSyntaxError: Invalid variable type; it should be one of ("color","date","datetime-local","email","month","number","password","tel","text","time","url","week") and "=" if there is a default value'
});
});

@@ -117,3 +149,7 @@

const testEnv = "DEBUG: [0, ]";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message: 'EnvSpecSyntaxError: Expected choice after ",".'
});
});

@@ -123,3 +159,8 @@

const testEnv = "DATABASE_URL\nADMIN_EMAIL:email\nDATA: text dasdsa";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
'EnvSpecSyntaxError: Invalid variable type; it should be one of ("color","date","datetime-local","email","month","number","password","tel","text","time","url","week") and "=" if there is a default value'
});
});

@@ -129,3 +170,8 @@

const testEnv = "DEBUG: [0, 1]=4";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message:
"EnvSpecSyntaxError: Invalid default value; it is not included in the provided restricted choices."
});
});

@@ -135,3 +181,60 @@

const testEnv = "DEBUG: [0, 1]=";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message: "EnvSpecSyntaxError: Expected default value after ="
});
});
test("Wrong Syntax for default value,missing value", () => {
const testEnv = "DEBUG\nDEBUG";
expect(
envSpec.parse(testEnv).then(data => data.html())
).rejects.toMatchObject({
message: `EnvSpecSyntaxError: Invalid variable name; variable "DEBUG" already exists.`
});
});
test("Test for serialization of data", () => {
const myForm = document.createElement("form");
const aLabel = document.createElement("label");
const anInput = document.createElement("input");
anInput.type = "text";
anInput.name = "data";
anInput.id = "env_spec_data";
myForm.appendChild(anInput);
expect(envSpec.serializeForm(myForm)).resolves.toMatchObject([
{ DATA: null }
]);
});
test("Test for serialization of data with specific value", () => {
const myForm = document.createElement("form");
const aLabel = document.createElement("label");
const anInput = document.createElement("input");
anInput.type = "text";
anInput.name = "data";
anInput.id = "env_spec_data";
anInput.value = "test";
myForm.appendChild(anInput);
expect(envSpec.serializeForm(myForm)).resolves.toMatchObject([
{ DATA: "test" }
]);
});
test("Test for serialization of data with restricted choices", () => {
const myForm = document.createElement("form");
const aLabel = document.createElement("label");
const aSelect = document.createElement("select");
aSelect.id = "env_spec_data";
aSelect.name = "data";
const anOption = document.createElement("option");
anOption.value = "1";
const secOption = document.createElement("option");
secOption.value = "2";
aSelect.appendChild(anOption);
aSelect.appendChild(secOption);
aSelect.selectedIndex = 1;
myForm.appendChild(aSelect);
expect(envSpec.serializeForm(myForm)).resolves.toMatchObject([{ DATA: "2" }]);
});

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