Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!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>
Add Comment
Please, Sign In to add comment