mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-15 19:17:02 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import flat from 'flat';
|
|
import deepmerge from 'deepmerge';
|
|
|
|
const { unflatten } = flat;
|
|
const DOT_REPLACEMENT = '☃';
|
|
|
|
//function that takes a list of path and returns a deeply nested object
|
|
//representing a tree of all of those paths
|
|
//
|
|
//
|
|
// given ["foo", "bar", "foo1", "foo/bar", "foo/baz", "foo/bar/baz"]
|
|
//
|
|
// returns {
|
|
// bar: null,
|
|
// foo: {
|
|
// bar: {
|
|
// baz: null
|
|
// },
|
|
// baz: null,
|
|
// },
|
|
// foo1: null,
|
|
// }
|
|
export default function (paths) {
|
|
// first sort the list by length, then alphanumeric
|
|
const list = paths.slice(0).sort((a, b) => b.length - a.length || b.localeCompare(a));
|
|
// then reduce to an array
|
|
// and we remove all of the items that have a string
|
|
// that starts with the same prefix from the list
|
|
// so if we have "foo/bar/baz", both "foo" and "foo/bar"
|
|
// won't be included in the list
|
|
let tree = list.reduce((accumulator, ns) => {
|
|
const nsWithPrefix = accumulator.find((path) => path.startsWith(ns));
|
|
// we need to make sure it's a match for the full path part
|
|
const isFullMatch = nsWithPrefix && nsWithPrefix.charAt(ns.length) === '/';
|
|
if (!isFullMatch) {
|
|
accumulator.push(ns);
|
|
}
|
|
return accumulator;
|
|
}, []);
|
|
|
|
tree = tree.sort((a, b) => a.localeCompare(b));
|
|
// after the reduction we're left with an array that contains
|
|
// strings that represent the longest branches
|
|
// we'll replace the dots in the paths, then expand the path
|
|
// to a nested object that we can then query with Ember.get
|
|
return deepmerge.all(
|
|
tree.map((p) => {
|
|
p = p.replace(/\.+/g, DOT_REPLACEMENT);
|
|
return unflatten({ [p]: null }, { delimiter: '/', object: true });
|
|
})
|
|
);
|
|
}
|