| 1 |
/** |
| 2 |
* Obtain the parent account name. |
| 3 |
* @param name - an account name. |
| 4 |
*/ |
| 5 |
export function parent(name: string): string { |
| 6 |
const parentEnd = name.lastIndexOf(":"); |
| 7 |
return parentEnd > 0 ? name.slice(0, parentEnd) : ""; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Obtain the leaf part of the account name, the last segment. |
| 12 |
* @param name - an account name. |
| 13 |
*/ |
| 14 |
export function leaf(name: string): string { |
| 15 |
const parentEnd = name.lastIndexOf(":"); |
| 16 |
return parentEnd > 0 ? name.slice(parentEnd + 1) : name; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Check whether an account is a descendant of another. |
| 21 |
* @param name - an account name. |
| 22 |
* @param of - the possible ancestor |
| 23 |
*/ |
| 24 |
export function isDescendant(name: string, of: string): boolean { |
| 25 |
if (of === "") { |
| 26 |
return true; |
| 27 |
} |
| 28 |
return name === of || name.startsWith(`${of}:`); |
| 29 |
} |