Skip to content

Instantly share code, notes, and snippets.

@or9
Last active June 28, 2019 14:34
Show Gist options
  • Save or9/69dc06705c6a5739acb325a84cd607ec to your computer and use it in GitHub Desktop.
Save or9/69dc06705c6a5739acb325a84cd607ec to your computer and use it in GitHub Desktop.
Get value of a property by key, or determine whether a provided object description has a given property.
var expected = window.location.href;
var actual = getProp(window, "location.href");
expected = JSON.stringify(expected);
actual = JSON.stringify(actual);
console.assert(actual === expected, { error: window.location.href });
expected = "default value";
actual = getProp(window, "nothing.expected.here", "default value");
expected = JSON.stringify(expected);
actual = JSON.stringify(actual);
console.assert(actual === expected, { error: "nothing expected…" });
expected = undefined;
actual = getProp({ a: { b: "" } }, "a.b.c");
expected = JSON.stringify(expected);
actual = JSON.stringify(actual);
console.assert(actual === expected, { error: "a.b.c" });
expected = "";
actual = getProp({ a: "" }, "a");
expected = JSON.stringify(expected);
actual = JSON.stringify(actual);
console.assert(actual === expected, { error: "hasOwnProperty???" })
/**
* Get value for a property described by schema param
* @param {object} - Dictionary on which to perform validation
* @param {string} - Pattern to search object's heirarchy
* @returns {*} - Found value or defaultValue if not found in pattern param
*/
function getProp (obj, schema, defaultReturnValue) {
if (!obj) return defaultReturnValue;
const expectedKeyArr = schema.split(".");
while (expectedKeyArr.length) {
const currentKey = expectedKeyArr.shift();
if (obj.hasOwnProperty(currentKey)) {
obj = obj[currentKey];
continue;
} else return defaultReturnValue;
}
return obj;
}
var expected = true;
var actual = isObjValid(window, "location.href");
console.assert(actual === expected, { error: window.location.href });
expected = false;
actual = isObjValid(window, "nothing.expected.here");
console.assert(actual === expected, { error: "nothing expected…" });
expected = false;
actual = isObjValid({ a: { b: "" } }, "a.b.c");
console.assert(actual === expected, { error: "a.b.c" });
expected = true;
actual = isObjValid({ a: "" }, "a");
console.assert(actual === expected, { error: "hasOwnProperty???" })
/**
* Determine whether a provided object contains a key
* @param {object} - Dictionary on which to perform validation
* @param {string} - Pattern to search object's heirarchy
* @returns {bool} - Indicates whether object matches provided schema
*
* @example
* isObjValid(window, "location.href"); // true
* isObjValid(window, "nothing.expected.here"); // false
* isObjValid({ a: { b: "" } }, "a.b.c"); // false
* isObjValid({ a: "" }, "a"); // true
*/
function isObjValid (obj, schema) {
if (!obj) return false;
const expectedKeyArr = schema.split(".");
while (expectedKeyArr.length) {
let currentKey = expectedKeyArr.shift();
if (obj.hasOwnProperty(currentKey)) {
obj = obj[currentKey];
continue;
} else return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment