Created
September 18, 2018 12:05
-
-
Save petevb/6f231c7c2b1c7c9bb4b3218e0a32a405 to your computer and use it in GitHub Desktop.
Hierarchical Config// source https://jsbin.com/gudubop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Hierarchical Config"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
const org = { | |
name: "NewOrbit", | |
parents: [0, 4, 27] | |
}; | |
const userName = getSetting("UserName", org.parents); | |
console.log ("username: " + userName); | |
const password = getSetting("Password", org.parents); | |
console.log ("password: " + password); | |
function getSetting(name, ctx) { | |
const documents = [ | |
{ setting: "UserName", parents: [0], value: "demodrivercheck" }, | |
{ setting: "UserName", parents: [0, 4], value: "override" }, | |
{ setting: "UserName", parents: [0, 4, 27], value: "override an override" }, | |
{ setting: "UserName", parents: [0, 4, 27, 28], value: "bug" }, | |
{ setting: "UserName", parents: [0, 4, 43], value: "bug" }, | |
{ setting: "Password", parents: [0], value: null }, | |
{ setting: "Password", parents: [0, 4], value: "******" }, | |
{ setting: "Unrelated", parents: [0, 4], value: "bug" }, | |
{ setting: "Unrelated2", parents: [0], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0,4], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27, 28], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27, 29], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 43], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 5], value: "bug" }, | |
]; | |
// match on setting name | |
const settings = documents.filter(a => a.setting === name); | |
// then for [0, 4, 27] this will filter ...28 & ...43 as too specific & inapproriate respectively: | |
const inContext = settings.filter((a) => match(a.parents, ctx)); | |
// now grab the most specific value for this org. | |
const mostSpecific = inContext.sort((a,b) => b.parents.length - a.parents.length)[0]; | |
return mostSpecific.value; | |
} | |
function match(a,b) { | |
let a1 = a.join('/'); | |
let b1 = b.join('/'); | |
return b1.startsWith(a1); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
const org = { | |
name: "NewOrbit", | |
parents: [0, 4, 27] | |
}; | |
const userName = getSetting("UserName", org.parents); | |
console.log ("username: " + userName); | |
const password = getSetting("Password", org.parents); | |
console.log ("password: " + password); | |
function getSetting(name, ctx) { | |
const documents = [ | |
{ setting: "UserName", parents: [0], value: "demodrivercheck" }, | |
{ setting: "UserName", parents: [0, 4], value: "override" }, | |
{ setting: "UserName", parents: [0, 4, 27], value: "override an override" }, | |
{ setting: "UserName", parents: [0, 4, 27, 28], value: "bug" }, | |
{ setting: "UserName", parents: [0, 4, 43], value: "bug" }, | |
{ setting: "Password", parents: [0], value: null }, | |
{ setting: "Password", parents: [0, 4], value: "******" }, | |
{ setting: "Unrelated", parents: [0, 4], value: "bug" }, | |
{ setting: "Unrelated2", parents: [0], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0,4], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27, 28], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27, 29], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 43], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 5], value: "bug" }, | |
]; | |
// match on setting name | |
const settings = documents.filter(a => a.setting === name); | |
// then for [0, 4, 27] this will filter ...28 & ...43 as too specific & inapproriate respectively: | |
const inContext = settings.filter((a) => match(a.parents, ctx)); | |
// now grab the most specific value for this org. | |
const mostSpecific = inContext.sort((a,b) => b.parents.length - a.parents.length)[0]; | |
return mostSpecific.value; | |
} | |
function match(a,b) { | |
let a1 = a.join('/'); | |
let b1 = b.join('/'); | |
return b1.startsWith(a1); | |
} | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
const org = { | |
name: "NewOrbit", | |
parents: [0, 4, 27] | |
}; | |
const userName = getSetting("UserName", org.parents); | |
console.log ("username: " + userName); | |
const password = getSetting("Password", org.parents); | |
console.log ("password: " + password); | |
function getSetting(name, ctx) { | |
const documents = [ | |
{ setting: "UserName", parents: [0], value: "demodrivercheck" }, | |
{ setting: "UserName", parents: [0, 4], value: "override" }, | |
{ setting: "UserName", parents: [0, 4, 27], value: "override an override" }, | |
{ setting: "UserName", parents: [0, 4, 27, 28], value: "bug" }, | |
{ setting: "UserName", parents: [0, 4, 43], value: "bug" }, | |
{ setting: "Password", parents: [0], value: null }, | |
{ setting: "Password", parents: [0, 4], value: "******" }, | |
{ setting: "Unrelated", parents: [0, 4], value: "bug" }, | |
{ setting: "Unrelated2", parents: [0], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0,4], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27, 28], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 27, 29], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 4, 43], value: "bug" }, | |
{ setting: "Unrelated3", parents: [0, 5], value: "bug" }, | |
]; | |
// match on setting name | |
const settings = documents.filter(a => a.setting === name); | |
// then for [0, 4, 27] this will filter ...28 & ...43 as too specific & inapproriate respectively: | |
const inContext = settings.filter((a) => match(a.parents, ctx)); | |
// now grab the most specific value for this org. | |
const mostSpecific = inContext.sort((a,b) => b.parents.length - a.parents.length)[0]; | |
return mostSpecific.value; | |
} | |
function match(a,b) { | |
let a1 = a.join('/'); | |
let b1 = b.join('/'); | |
return b1.startsWith(a1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment