Giter VIP home page Giter VIP logo

pattern.js's Introduction

So.. what's this?

Pattern.js allows you to match a string against a set of predefined patterns through boolean functions or regular expressions.

Usage

In order for pattern.js to match a given string, you must define the patterns it needs to be able to understand and the entities that make those patterns up.

You would for example define what a funkyNumber and an operation are through the Pattern.defineEntities method as follows:

Pattern.defineEntities([
    {
        name: 'funkyNumber',
        definition: function(str) {
            var result = false;
            /* a funkyNumber is an integer of less than 5 digits that is 
            divisible by the sum of its digits.
            Eg: 36 is funky because 36/(3+6) = 4 */
            var intVersion = parseInt(str)
            if(str.length < 5 && !isNaN(intVersion)) {
                var digitsSum = 0;
                for(var i=0; i<str.length; ++i) {
                    digitsSum += parseInt(str[i]);
                }
                if(intVersion % digitsSum == 0) result = true;
            }
            return result;
        }
    },
    /* an operation is simply a string matching + or - or * or / */
    {
        name: 'operation',
        definition: /^(\+|-|\*|\/)$/
    },
    /* this will define the entity = as a string matching the string "=" */
    {
        name:"="
    }
])

Now, you can define which patterns to allow as follows:

Pattern.definePatterns([
    'funkyNumber operation funkyNumber = funkyNumber', // would match 36 + 300 = 336
    'funkyNumber operation = funkyNumber' // would match 36 * = 300, but not 36 * = 17
])

Finally, you can feed a string to Pattern.match to verify that it is matched by any of the provided patterns. The method Pattern.match returns a Match object, that can be empty if none is found.

Pattern.match('36 + 300 = 336'); // returns a Match
Pattern.match('36 + 301 = 336'); // returns {}

Matches

soon

pattern.js's People

Contributors

ale316 avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar Terence Schumacher avatar

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.