Skip to content

NotationGlob

Defined in: src/core/NotationGlob.ts:68

NotationGlob is a utility for validating, comparing and sorting dot-notation globs.

You can use http://www.linfo.org/wildcard.html|wildcard stars * and negate the notation by prepending a bang !. A star will include all the properties at that level and a negated notation will be excluded.

// for the following object;
{ name: 'John', billing: { account: { id: 1, active: true } } };
'billing.account.*' // represents value `{ id: 1, active: true }`
'billing.account.id' // represents value `1`
'!billing.account.*' // represents value `{ name: 'John' }`
'name' // represents `'John'`
'*' // represents the whole object
const glob = new NotationGlob('billing.account.*');
glob.test('billing.account.id'); // true

new NotationGlob(glob): NotationGlob

Defined in: src/core/NotationGlob.ts:84

Constructs a NotationGlob object with the given glob string.

string

Notation string with globs.

NotationGlob

  • If given notation glob is invalid.

get glob(): string

Defined in: src/core/NotationGlob.ts:103

Gets the normalized glob notation string.

string


get absGlob(): string

Defined in: src/core/NotationGlob.ts:111

Gets the absolute glob notation without the negation prefix ! and redundant trailing wildcards.

string


get isNegated(): boolean

Defined in: src/core/NotationGlob.ts:118

Specifies whether this glob is negated with a ! prefix.

boolean


get regexp(): RegExp

Defined in: src/core/NotationGlob.ts:126

Represents this glob in regular expressions. Note that the negation prefix (!) is ignored, if any.

RegExp


get notes(): string[]

Defined in: src/core/NotationGlob.ts:137

List of notes (levels) of this glob notation. Note that trailing, redundant wildcards are removed from the original glob notation.

string[]


get first(): string

Defined in: src/core/NotationGlob.ts:144

Gets the first note of this glob notation.

string


get last(): string

Defined in: src/core/NotationGlob.ts:151

Gets the last note of this glob notation.

string


get parent(): string | null

Defined in: src/core/NotationGlob.ts:166

Gets the parent notation (up to but excluding the last note) from the glob notation string. Note that initially, trailing/redundant wildcards are removed.

const glob = NotationGlob.create;
glob('first.second.*').parent; // "first.second"
glob('*.x.*').parent; // "*" ("*.x.*" normalizes to "*.x")
glob('*').parent; // null (no parent)

string | null

test(notation): boolean

Defined in: src/core/NotationGlob.ts:195

Checks whether the given notation value matches the source notation glob.

string

The notation string to be tested. Cannot have any globs.

boolean

  • If given notation is not valid or contains any globs.
const glob = new NotationGlob('!prop.*.name');
glob.test("prop.account.name"); // true

covers(glob): boolean

Defined in: src/core/NotationGlob.ts:215

Specifies whether this glob notation can represent (or cover) the given glob notation. Note that negation prefix is ignored, if any.

string | string[] | NotationGlob

Glob notation string, glob notes array or a NotationGlob instance.

boolean

const glob = NotationGlob.create;
glob('*.y').covers('x.y') // true
glob('x[*].y').covers('x[*]') // false

intersect(glob, restrictive?): string | null

Defined in: src/core/NotationGlob.ts:236

Gets the intersection of this and the given glob notations. When restrictive, if any one of them is negated, the outcome is negated. Otherwise, only if both of them are negated, the outcome is negated.

string

Second glob to be used.

boolean = false

Whether the intersection should be negated when one of the globs is negated.

string | null

  • Intersection notation if any; otherwise null.
const glob = NotationGlob.create;
glob('x.*').intersect('!*.y') // 'x.y'
glob('x.*').intersect('!*.y', true) // '!x.y'

static create(glob): NotationGlob

Defined in: src/core/NotationGlob.ts:255

Basically constructs a new NotationGlob instance with the given glob string.

string

The source notation glob.

NotationGlob

const glob = NotationGlob.create(strGlob);
// equivalent to:
const glob = new NotationGlob(strGlob);

static isValid(glob): boolean

Defined in: src/core/NotationGlob.ts:265

Validates the given notation glob.

string

Notation glob to be validated.

boolean


static isValidNote(note): boolean

Defined in: src/core/NotationGlob.ts:274

Validates the given glob note.

string

Note to be validated.

boolean


static hasMagic(glob): boolean

Defined in: src/core/NotationGlob.ts:284

Specifies whether the given glob notation includes any valid wildcards (*) or negation bang prefix (!).

string

Glob notation to be checked.

boolean


static toRegExp(glob): RegExp

Defined in: src/core/NotationGlob.ts:298

Gets a regular expressions instance from the given glob notation. Note that the bang ! prefix will be ignored if the given glob is negated.

string

Glob notation to be converted.

RegExp

  • A RegExp instance from the glob.
  • If given notation glob is invalid.

static split(glob, normalize?): string[]

Defined in: src/core/NotationGlob.ts:339

Splits the given glob notation string into its notes (levels). Note that this will exclude the ! negation prefix, if it exists.

string

Glob notation string to be splitted.

boolean = false

Whether to remove trailing, redundant wildcards.

string[]

  • A string array of glob notes (levels).
  • If given glob notation is invalid.
NotationGlob.split('*.list[2].prop') // ['*', 'list', '[2]', 'prop']
// you can get the same result from the .notes property of a NotationGlob instance.

static join(notes, normalize?): string

Defined in: src/core/NotationGlob.ts:362

Joins the given notes into a glob notation.

No negation is allowed in notes array.

string[]

Array of notes.

boolean = false

Whether to remove trailing wildcards.

string

  • A string of glob notation.
  • If given notes are invalid.

static compare(globA, globB): number

Defined in: src/core/NotationGlob.ts:408

Compares two given notation globs and returns an integer value as a result. This is generally used to sort glob arrays. Loose globs (with stars especially closer to beginning of the glob string) and globs representing the parent/root of the compared property glob come first. Verbose/detailed/exact globs come last. (* < *.abc < abc).

For instance; store.address comes before store.address.street. So this works both for *, store.address.street, !store.address and *, store.address, !store.address.street. For cases such as prop.id vs !prop.id which represent the same property; the negated glob comes last.

string

First notation glob to be compared.

string

Second notation glob to be compared.

number

  • Returns -1 if globA comes first, 1 if globB comes first and 0 if equivalent priority.
  • If either globA or globB is invalid glob notation.
const { compare } = NotationGlob;
compare('*', 'info.user') // -1
compare('*', '[*]') // 0
compare('info.*.name', 'info.user') // 1

static sort(globList): string[]

Defined in: src/core/NotationGlob.ts:457

Sorts the notation globs in the given array by their priorities. Loose globs (with stars especially closer to beginning of the glob string); globs representing the parent/root of the compared property glob come first. Verbose/detailed/exact globs come last. (* < *.y < x.y).

For instance; store.address comes before store.address.street. For cases such as prop.id vs !prop.id which represent the same property; the negated glob wins (comes last).

string[]

The notation globs array to be sorted. The passed array reference is modified.

string[]

  • Logically sorted globs array.
NotationGlob.sort(['!prop.*.name', 'prop.*', 'prop.id']) // ['prop.*', 'prop.id', '!prop.*.name'];

static normalize(globList, restrictive?): string[]

Defined in: src/core/NotationGlob.ts:499

Normalizes the given notation globs array by removing duplicate or redundant items, eliminating extra verbosity (also with intersection globs) and returns a priority-sorted globs array.

  • If any exact duplicates found, all except first is removed.
    example: `['car', 'dog', 'car']` normalizes to `['car', 'dog']`.
  • If both normal and negated versions of a glob are found, negated wins.
    example: `['*', 'id', '!id']` normalizes to `['*', '!id']`.
  • If a glob is covered by another, it's removed.
    example: `['car.*', 'car.model']` normalizes to `['car']`.
  • If a negated glob is covered by another glob, it's kept.
    example: `['*', 'car', '!car.model']` normalizes as is.
  • If a negated glob is not covered by another or it does not cover any other; then we check for for intersection glob. If found, adds them to list; removes the original negated.
    example: `['car.*', '!*.model']` normalizes as to `['car', '!car.model']`.
  • In restrictive mode; if a glob is covered by another negated glob, it's removed. Otherwise, it's kept.
    example: `['*', '!car.*', 'car.model']` normalizes to `['*', '!car']` if restrictive.

string | string[]

Notation globs array to be normalized.

boolean = false

Whether negated items strictly remove every match. Note that, regardless of this option, if any item has an exact negated version; non-negated is always removed.

string[]

  • If any item in globs list is invalid.
const { normalize } = NotationGlob;
normalize(['*', '!id', 'name', '!car.model', 'car.*', 'id', 'name']); // ['*', '!id', '!car.model']
normalize(['!*.id', 'user.*', 'company']); // ['company', 'user', '!company.id', '!user.id']
normalize(['*', 'car.model', '!car.*']); // ["*", "!car.*", "car.model"]
// restrictive normalize:
normalize(['*', 'car.model', '!car.*'], true); // ["*", "!car.*"]

static union(globsA, globsB, restrictive?): string[]

Defined in: src/core/NotationGlob.ts:719

Gets the union from the given couple of glob arrays and returns a new array of globs.

  • If the exact same element is found in both arrays, one of them is removed to prevent duplicates.
    example: `['!id', 'name'] ∪ ['!id']` unites to `['!id', 'name']`
  • If any non-negated item is covered by a glob in the same or other array, the redundant item is removed.
    example: `['*', 'name'] ∪ ['email']` unites to `['*']`
  • If one of the arrays contains a negated equivalent of an item in the other array, the negated item is removed.
    example: `['!id'] ∪ ['id']` unites to `['id']`
  • If any item covers/matches a negated item in the other array, the negated item is removed.
    example #1: `['!user.id'] ∪ ['user.*']` unites to `['user']`
    example #2: `['*'] ∪ ['!password']` unites to `['*']`

string[]

First array of glob strings.

string[]

Second array of glob strings.

boolean = false

Whether negated items in each of the lists, strictly remove every match in themselves (not the cross list). This option is used when pre-normalizing each glob list and normalizing the final union list.

string[]

const a = ['user.*', '!user.email', 'car.model', '!*.id'];
const b = ['!*.date', 'user.email', 'car', '*.age'];
const { union } = NotationGlob;
union(a, b) // ['car', 'user', '*.age', '!car.date', '!user.id']