Code Monkey home page Code Monkey logo

js-rx-exprs's Introduction

Regular Expressions for RxJS

RxJS implementation of Regular Expressions for Reactive Streams

npm i exprs-rx

Intro

This library allows us to apply RegExp-like syntax to Observables, in order to select events from them.

Imagine that we have three streams:

A  ----o---------------
B  -o-o--o--o--o---o-o-
C  --------------o-----

And we want to select only those emissions that happen after A and before C emits

A  ----o---------------
B  -x-x--o--o--o---x-x-
C  --------------o-----

We can do that by applying the following expression:

AB*C

This expression means:

Take one emission from A
Then take all emissions from B
Until C emits. Take that one emission from C too
Then complete

Check out this intro article for more:
"RegExp syntax for Observables: Never Easier!"
https://dev.to/kosich/regexp-for-reactive-streams-143g
2 min read

Usage example

Query mousemove events after mousedown and before mouseup
(as in a drag-n-drop flow)

import { fromEvent } from 'rxjs'; 
import { repeat } from 'rxjs/operators';
import { exec } from 'exprs-rx';

// listen to events
const A = fromEvent(item, 'mousedown');
const B = fromEvent(document, 'mousemove');
const C = fromEvent(document, 'mouseup');

// apply a regular expression
exec('AB*C', { A, B, C })
    // it returns an Observable
    .pipe(
        repeat() // repeat the capturing
    )
    // use the selected events
    .subscribe(console.log);

Try this code in a sandbox:
https://stackblitz.com/edit/rxjs-regular-expressions

Notes

Currently, the library supports A โ€” capital letters that represent a single emission of a corresponding stream.

And a repeat A* notation that will consume multiple events from the corresponding stream, until completion or until next entry in the regex matches.

For regex specification and plans on vocabulary, please, see this repo https://github.com/expressions-for-reactive-streams/spec-regular-expressions

by Kostia Palchyk

js-rx-exprs's People

Contributors

dependabot[bot] avatar kosich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

stephensong

js-rx-exprs's Issues

Similar libraries, projects

Hi! Really cool idea, I am looking for something very similar. May I ask if you are aware of similar projects or libraries which influenced you? I am to create something very similar but with types and a bit more expressive regex-like capabilities.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.