UNPKG

@can-it/operators-nested

Version:

[Documentation](https://can-it.github.io/packages/operators/nested)

40 lines (39 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "NestedGenerator", { enumerable: true, get: function() { return NestedGenerator; } }); const _constants = require("./constants"); let NestedGenerator = class NestedGenerator { /** * Generate the resource identity from an array of strings. * @param resources string[] - List of strings. An empty string will be converted to a wildcard string. * @returns string - The generated resource identity. * * Example usage: * ```typescript * transform('orgs', '', 'users', ''); // 'orgs::*::users::*' * ``` */ transform(...resources) { this.validateResources(resources); return resources.map((ri)=>ri || this.riPattern.wildcard).join(this.riPattern.separator); } validateResources(resources) { resources.filter(Boolean).forEach((resource)=>this.validateResource(resource)); } validateResource(ri) { if (this.resourceRegex.test(ri)) { return; } throw new Error(`The resource does not match the provided "resourceRegex: ${this.riPattern.resourceRegex}"`); } constructor(riPattern = _constants.DEFAULT_RI_PATTERN){ this.riPattern = riPattern; this.resourceRegex = new RegExp(`^${this.riPattern.resourceRegex}$`); } };