Contentful Personalization & Analytics
    Preparing search index...

    Tracks whether a given value is present within one or more logical scopes.

    • Scope names are case-sensitive.
    • Presence is based on Set.has reference equality for objects and value equality for primitives.
    const presence = new ValuePresence({ default: ['a', 'b'] })
    presence.isPresent('default', 'a') // true
    presence.addValue('default', 'c')
    presence.removeValue('default', 'b')
    presence.reset('default')

    ValuePresenceScope

    Index

    Constructors

    • Create a new ValuePresence.

      Parameters

      • OptionaldefaultMap: Record<string, unknown[]>

        Optional initial data. Keys are scope names; values are arrays of items to seed. Empty-string keys are normalized to the default scope (undefined).

      Returns ValuePresence

      • If defaultMap contains duplicate items for a scope, duplicates are collapsed by the Set.

    Methods

    • Add a value to a scope, creating the scope if it does not exist.

      Parameters

      • scope: ValuePresenceScope

        Scope to add the value to. Use undefined for the default scope.

      • value: unknown

        The value to add.

      Returns void

      void

      • No-op if the value is already present (due to Set semantics).
      presence.addValue('users', userId)
      
    • Check whether a value is present within a given scope.

      Parameters

      • scope: ValuePresenceScope

        The scope to check. Use undefined for the default scope.

      • value: unknown

        The value to test for presence.

      Returns boolean

      true if the value is present in the specified scope; otherwise false.

      Presence testing uses Set.prototype.has semantics.

      presence.isPresent(undefined, 42) // e.g., true or false
      
    • Remove a value from a scope.

      Parameters

      • scope: ValuePresenceScope

        Scope to remove from. Use undefined for the default scope.

      • value: unknown

        The value to remove.

      Returns void

      void

      If the scope does not exist or the value is not present, this is a no-op.

      presence.removeValue('users', userId)
      
    • Clear values from a single scope, or from all scopes.

      Parameters

      • Optionalscope: ValuePresenceScope

        If provided, clears only that scope. If omitted, clears all scopes.

      Returns void

      void

      • When called with a specific scope that does not exist, this is a no-op.
      • When called with no arguments, all scopes and values are removed.
      • Clearing a non-existent scope will not create the scope.
      // Clear one scope
      presence.reset('users')

      // Clear all scopes
      presence.reset()