2.0.4 • Published 6 days ago

@evolv/mutate v2.0.4

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
6 days ago

Evolv AI - Mutate

Mutate is a library of helpers to maintain persistent modifications to the rendered DOM of a web page for implementation of temporary, experimental changes and features for rapid experimentation.

It is intended to be framework independent and equally effective for SPAs and traditionally rendered webpages.

Getting Set Up

Installation

$ npm install @evolv/mutate

Building From Source

Clone the repository and run the following commands.

$ npm install
$ npm run build

Run Demo Website

There is a demo site included.

Fair warning: It is hideous and may cause seizures :)

$ npm run demo

Getting Started

The API for Mutate is similar in a lot of ways to the API of jQuery with a significant difference, that is selectors (Collectors) refer to all current and future matching Elements, and the functions to modify the DOM (Mutations) are persistent.

This means that you don't need to worry about timing, or dynamic areas of the rendered page.

Mutate also provides the ability to "project" Elements of the DOM into other Elements of the DOM, binding all interactions with the "projection" back to the original Elements.

As everyone building variants have learned the hard way, most Elements are dependent on their location in the DOM for both style and functionality. Projection allows the implementer to "move" and restyle Elements without losing the position dependent functionality of the original Elements.

Importing

import {collect, mutate} from '@evolv/mutate';

Basic Usage

The basic flow when using Mutate is to first define a Collector.

collect('<selector>', '<collector name>');

Then to define a Mutator for the Collector.

mutate('<collector name>').hide();

Mutators allow for Mutations to be chained together, similar to jQuery which will be evaluated in order of invocation.

mutate('<collector name>').text('<new text value>').classes({'<new class>': true});

Updating the Mutate library

For updating instructions refer to the Update Process

Reference

collect(selector: string, name: string, parent?: string): Collector

Create a new named Collector.

note If parent is not specified, the library will attempt to discover the closest non-volatile parents of any Element that match the selector.

Parameters

NameTypeDescription
selectorstringA CSS selector that the Collector should manage.
namestringThe name of the collector of future reference from mutate.
parent?string(Optional) An optional selector if the nearest non-volatile parent is known.

Returns

Collector

The Collector instance created. This Collector can be retrieved by name as well.


mutate(collectorName: string, variantKey?: string): Mutator

Create a Mutator associated with an named Collector

Parameters

NameType
collectorNamestring
variantKey?string

Returns

Mutator

Collector

The Collector provides all functionality for creating persistent select Elements currently present or that will be present on the page at a future time.

A Collector can have any number of Mutator associated with it.

destroyed: boolean

True if the Collector has been destroyed, otherwise False.


elements: Element[]

All Elements the Collector has collected.


id: string

The id of the Collector.


isValid: boolean

True if all validation has been satisfied, otherwise False.


name: undefined | string

The name of the Collector.


observedTargets: Element[]

All Elements the Collector is currently observing.


parentSelector: undefined | string

The current selector for parent Elements if set.


paused: boolean

True if the Collector is paused, otherwise False.


root: Element | Document

The outermost parent Element to observe.


selector: undefined | string

The current selector for Elements to be collected if set.

atLeast(count: number): Collector

Specify the minimum number of Elements the Collector must find before it is considered valid.

Parameters

NameTypeDescription
countnumberThe minimum number of Elements the Collector must match to be considered value.

Returns

Collector


atMost(count: number): Collector

Specify the maximum number of Elements the Collector should find to be considered valid.

Parameters

NameTypeDescription
countnumberThe maximum number of Elements the Collector must match to be considered value.

Returns

Collector


claim(): Promise<Element>

Permanently claim a collected Element for modification.

Returns

Promise<Element>


destroy(): void

Permanently destroy this Collector.


exactly(count): Collector

Specify the total number of Elements the Collector should find to be considered valid.

Parameters

NameTypeDescription
countnumberThe total number of Elements the Collector must match to be considered value.

Returns

Collector


filter(predicate: Predicate): Collector

Add a Predicate Elements must satisfy to be included in the Collector.

Parameters

NameTypeDescription
predicatePredicateA Predicate Elements must satisfy to be included in the in the Collector

Returns

Collector


observeParent(parentSelector): Collector

Set a selector for the parent Elements to be observed for matching Elements.

Parameters

NameTypeDescription
parentSelectorstringThe selector for parent Elements.

Returns

Collector


pause(): Collector

Pause collection and Element related notifications for this Collector.

Returns

Collector


subscribe(listener: ElementListener): Collector

Subscribe to notifications about the changing of Elements within the Collector.

Parameters

NameTypeDescription
listenerElementListenerA callback to be notified about changes in the Elements.

Returns

Collector


subscribeState(listener: CollectorStateListener): Collector

Subscribe to notifications about the changing of state within the collector.

Parameters

NameTypeDescription
listenerCollectorStateListenerA callback to be notified when the state of the Collector changes.

Returns

Collector


unpause

unpause(): Collector

Unpause collection and Element related notifications for this Collector.

Returns

Collector


validate(predicate: Predicate): Collector

Add a Predicate that the Collector must satisfy before it is considered valid and ready to apply Effects.

Parameters

NameTypeDescription
predicatePredicateA Predicate the collector must meet before applying Effects.

Returns

Collector


within(`millis`: number): Collector

Specify the maximum amount of time in milliseconds that a Collector has to satisfy all validation criteria.

Parameters

NameTypeDescription
millisnumberThe maximum amount of time the Collector has to satisfy validation criteria.

Returns

Collector

Mutator

Mutator provides a collection of methods to persistently mutate the DOM.

with: Binder

Bind Effects to non-DOM related events or state changes.

The bound value will be available for use in Effects as well as triggering reapplication of Effects.

Type declaration

NameType
attr(attrName: string, transform?: (v: any) => any) => Binder
content(transform?: (v: any) => any) => Binder
context(propertyName: string, transform?: (v: any) => any) => Binder
apply(transform): Mutator

WARNING! This function is experimental and should only be used in testing.

Parameters

NameTypeDescription
transform(subject: Element) => ElementA function that modifies the DOM of subject.

Returns

Mutator


attributes

attributes(attributes): Mutator

Set attributes on the Elements in the Collector.

Parameters

NameTypeDescription
attributesobject | Map<string, string | (state: Map<string, any>) => string>A {@type Map} of attribute names to {@type string} or {@type (() => string)} representing their desired state.

Returns

Mutator


classes

classes(classes): Mutator

Set or remove classes on the Elements in the Collector.

Parameters

NameTypeDescription
classesobject | Map<string, boolean | (state: Map<string, any>) => boolean>A {@type Map} of class names to {@type boolean} or {@type (() => boolean)} representing their desired state.

Returns

Mutator


customEffect(initialize, modify, revert): Mutator

Create a custom Effect that is persistently applied.

Parameters

NameTypeDescription
initialize() => voidThe function to be invoked to initialize the Effect.
modify() => voidThe function to be invoked when an Element is modified.
revert() => voidThe function to be invoked when the Effect should be reverted.

Returns

Mutator


hide(): Mutator

Hide all Elements in the Collector.

Returns

Mutator


html(newHtml, clone?): Mutator

Replace the HTML content of the Elements matched by the Collector.

Parameters

NameTypeDefault valueDescription
newHtmlstring | Node[]undefinedThe new HTML or Nodes to replace the content with.
clonebooleantrueWhether or not a the nodes should be cloned before insertion. (Default: True)

Returns

Mutator


insert

insert(nodes, clone?): Mutator

Insert one or more Nodes into all elements in the Collector.

Parameters

NameTypeDefault valueDescription
nodesstring | Node | Node[]undefinedThe Nodes to insert, or a string containing the HTML to insert.
clonebooleantrueWhether or not a the nodes should be cloned before insertion. (Default: True)

Returns

Mutator


once(): Mutator

Specify that the Effects be applied no more than once.

Returns

Mutator


pause(): Mutator

Pause all Effects that are applied through this Mutator.

Returns

Mutator


project(to): Mutator

Clone and hide an Element, mapping all events from the clone into the original elements. The clone is then inserted into the DOM element that matches the {@param to} selector.

The purpose of the functionality is to avoid disabling or otherwise negatively impacting existing event listeners.

Parameters

NameTypeDescription
tostringThe selector for an element into which the cloned Element should be projected.

Returns

Mutator


reevaluate(): Mutator

Reinitialize all Effects on the Elements in the Collector.

Returns

Mutator


remove(nodes: Node[]): Mutator

Remove the specified Nodes from all elements in the Collector.

Parameters

NameTypeDescription
nodesstring | Node | Node[]the Nodes or a selector for Nodes that should be removed.

Returns

Mutator


revert(subject?): Mutator

Revert all Effects applied through this Mutator to all Elements in the Collector or the specified {@param subject}.

Parameters

NameTypeDescription
subject?Element(Optional) The Element to revert the Effects on.

Returns

Mutator


show(): Mutator

Show all Elements in the Collector.

Returns

Mutator


styles(styles): Mutator

Set styles on the Elements in the Collector.

Parameters

NameTypeDescription
stylesobject | Map<string, string | (state: Map<string, any>) => string>A {@type Map} of style names to {@type string} or {@type (() => string)} representing their desired state.

Returns

Mutator


text(newText: string): Mutator

Replace the text content of the Elements matched by the Collector.

Parameters

NameTypeDescription
newTextstring | (state: default) => stringThe new text content.

Returns

Mutator


unpause(): Mutator

Unpause all Effects that are applied through this Mutator.

Returns

Mutator


interface CollectorStateListener

Type declaration

(collectorState, collector): void

Parameters
NameType
collectorStateCollectorState
collectorCollector

interface ElementListener

Type declaration

(action, element, elementList): void

Parameters
NameType
actionEventType
elementElement
elementListElement[]

interface Predicate

Type declaration

(element): boolean

Parameters
NameType
elementHTMLElement
Returns

boolean

How to test your changes

  1. run npm start
  2. Create a simple website or use codesandbox
  3. Add a snippet to head <script src="http://localhost:8080/index.js"></script> (make sure that your local is running on 8080, otherwise update src)
  4. Apply changes to the website in the console
evolv.collect('h1', 'l6aiigykj')
evolv.mutate("l6aiigykj").html("Test");
2.0.3

6 days ago

2.0.4

6 days ago

2.0.1

2 months ago

2.0.0

2 months ago

1.11.13

2 months ago

1.11.12

3 months ago

1.11.10

3 months ago

1.11.9

3 months ago

1.11.8

3 months ago

1.11.6

4 months ago

1.11.5

4 months ago

1.11.4

4 months ago

1.11.3

4 months ago

1.11.0

5 months ago

1.10.5

9 months ago

1.10.4

9 months ago

1.10.3

10 months ago

1.10.9

8 months ago

1.10.8

8 months ago

1.10.7

8 months ago

1.10.6

8 months ago

1.10.0

10 months ago

1.9.15

12 months ago

1.9.14

12 months ago

1.9.13

12 months ago

1.9.12

1 year ago

1.9.11

1 year ago

1.9.10

1 year ago

1.9.9

1 year ago

1.9.8

1 year ago

1.9.7

1 year ago

1.9.6

1 year ago

1.9.5

1 year ago

1.9.3

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.9.2

1 year ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.3.3

2 years ago

1.5.0

2 years ago

1.3.2

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.0.0

2 years ago