Skip to content

Instantly share code, notes, and snippets.

@iamogbz
Last active July 19, 2023 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamogbz/60e654342532f80ae264128909e84198 to your computer and use it in GitHub Desktop.
Save iamogbz/60e654342532f80ae264128909e84198 to your computer and use it in GitHub Desktop.
Node Process Env Test
let value;
checkProp = (o, p) => {
console.log("value", JSON.stringify(o[p]));
console.log("hasProperty", p, Object.prototype.hasOwnProperty.call(o, p));
console.log("ownProperty", p, Object.getOwnPropertyNames(o).includes(p));
console.log();
};
runTests = object => {
const prop = "undefinedProp";
console.log("prop is not defined");
checkProp(object, prop);
value = 1
console.log(`prop is declared as ${value} with assignment (=)`);
object[prop] = value;
checkProp(object, prop);
console.log("prop is deleted", delete object[prop]);
checkProp(object, prop);
value = 1;
console.log(`prop is configured as ${value} with value`);
Object.defineProperty(object, prop, { configurable: true, value });
checkProp(object, prop);
console.log("prop is deleted", delete object[prop]);
checkProp(object, prop);
value = 1;
console.log(`prop is configured as ${value} with getter|setter`);
const get = () => value;
const set = v => (value = v);
Object.defineProperty(object, prop, { configurable: true, get, set });
checkProp(object, prop);
console.log("prop is deleted", delete object[prop]);
checkProp(object, prop);
value = 1
console.log(`prop is redeclared as ${value} with assignment (=)`);
object[prop] = value;
checkProp(object, prop);
console.log("prop is deleted", delete object[prop]);
checkProp(object, prop);
console.log();
};
console.log(">>> plain object <<<");
runTests({});
console.log(">>> process.env <<<");
runTests(process.env);
@iamogbz
Copy link
Author

iamogbz commented Jul 19, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment