UNPKG

@can-it/operators-nested

Version:

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

32 lines (31 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "NestedComparator", { enumerable: true, get: function() { return NestedComparator; } }); const _constants = require("./constants"); const escapeRegex = (text)=>text.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&'); let NestedComparator = class NestedComparator { isAllowed(ri, permissionRi) { return this.getTestableRi(permissionRi).test(ri); } isDenied(reqRi, permRi) { return this.isAllowed(reqRi, permRi); } getTestableRi(ri) { const resourceWildcardRegex = new RegExp(`${escapeRegex(this.riPattern.wildcard + this.riPattern.separator)}`, 'g'); const endWildcardRegex = new RegExp(`${escapeRegex(this.riPattern.separator + this.riPattern.wildcard)}$`); const testableRi = ri// replace all the wildcard that appears at start and center of a ri .replace(resourceWildcardRegex, `${this.riPattern.resourceRegex}${this.riPattern.separator}`)// if the wildcard appears at the end, mean we only support up to that level .replace(endWildcardRegex, `(${this.riPattern.separator}${this.riPattern.resourceRegex})?$`); return new RegExp(`^${testableRi}`); } constructor(riPattern = _constants.DEFAULT_RI_PATTERN){ this.riPattern = riPattern; } };