0.0.1 • Published 5 months ago

patternfinder v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

CircleCI

Pattern Finder

This tool helps you find patterns in text. It's handy for spotting particular sequences in public keys, making it easier to locate addresses. Plus, it adds a bit of fun to pattern hunting.

For Inspiration: 12 Zeros Address

Quickstart

npm i patternfinder
import { PatternFinder } from 'patternfinder'
const patternFinder = new PatternFinder()
const result = patternFinder
    .getResult( { 
        'str': '0000abcdefghijklmnop00000', 
        'presetKey': 'startsAndEndsWithZeros',
        'flattenResult': false
    } )
console.log( JSON.stringify( result, null, 4 ) )

Table of Contents

  • Pattern Finder
  • Quickstart
  • Table of Contents
  • Methods
    • getPresetKeys()
    • getResult()
    • setPreset()
  • Challenges
  • License

Methods

The module has the main method .getResults() that processes a string using predefined presets in just a few lines. You can query all loaded preset keys with getPresetKeys(). You can create your custom presets using setPreset(), which are then available through .getResults().

getPresetKeys()

This method returns all available presets, including those added later via .setPresets().

import { PatternFinder } from 'patternfinder'
const patternFinder = new PatternFinder()
const presetKeys = patternFinder.getPresetKeys()
console.log( `Available PresetsKeys: ${presetKeys.join( ', ' )}` )

getResult()

.getResult( { str, presetKey, flattenResult=false } )
KeyTypeDescriptionRequired
strStringThe string to be analyzed.true
presetKeyStringThe preset to use.true
flattenResultObject, BooleanOverride the default values to force a detailed result. Depending on the method, additional information may be available.true
import { PatternFinder } from 'patternfinder'
const patternFinder = new PatternFinder()
const result = patternFinder
    .getResult( { 
        'str': '0000abcdefghijklmnop00000', 
        'presetKey': 'startsAndEndsWithZeros',
        'flattenResult': false
    } )
console.log( JSON.stringify( result, null, 4 ) )

setPreset()

This method allows you to preload your custom challenges so that they can be accessed via getResult().

.setPreset( { presetKey, challenge } )
KeyTypeDescriptionRequired
presetKeyStringExpects the key under which the challenge is to be found.true
challengeObjectContains all the information needed to perform a challenge.true
import { PatternFinder } from 'patternfinder'
const patternFinder = new PatternFinder()
const preset = {
    'presetKey': 'customPreset',
    'challenge':         {
        'logic': {
            'and': [
                {
                    'value': '0',
                    'description': 'Search for a given character.',
                    'method': 'inSuccession',
                    'option':  'startsWith', // 'inBetween', // 'endsWith',
                    'expect': {
                        'logic': '>=',
                        'value': 2
                    }
                }
            ]
        }
    }
}

const search = '000abcdefghi'
const result = patternFinder
    .setPreset( preset )
    .getResult( { 
        'str': search, 
        'presetKey': 'customPreset',
        'flattenResult': true
    } )

console.log( JSON.stringify( result, null, 4 ) )

You can find all default presets here: ./src/data/presets.mjs

Challenges

A preset consists of a presetKey name, an optional description, and the actual logic section.

OperatorDescription
andRequires that all patterns be found.
orRequires that at least one pattern be found.

The following basic operators are available: and and or. and expects all patterns to be found, while or expects at least one pattern to be found.

Search TypeDescriptionOptionsLogic
regularExpressionAllows complex search patterns using regular expressions.=
inSuccessionAllows counting the same characters at the beginning, end, or anywhere in the text and comparing with a number.startsWith, endsWith, inBetween=, >, >=, <, <=

There are currently two different basic search types: regularExpression and inSuccession. regularExpression allows for high complexity, while inSuccession allows counting the same characters at the beginning (startsWith), end (endsWith), or anywhere in the text (inBetween). You can specify a number using the expect.value key and a comparison operator using expect.logic.

These individual patterns can be grouped as described above using and and or, and the additional function in getResults({ ... flattenResults: 'true' }) simplifies the output with a Boolean value.

In this example, it searches for 0s that are in sequence at the beginning of the string. If there are at least 2, it is considered true.

const preset = {
    'presetKey': 'customPreset',
    'challenge': {
        'logic': {
            'and': [
                {
                    'value': '0',
                    'description': 'Search for a given character.',
                    'method': 'inSuccession',
                    'option':  'startsWith', // 'inBetween', // 'endsWith',
                    'expect': {
                        'logic': '>=',
                        'value': 2
                    }
                }
            ]
        }
    }
}

You can find all default presets here: ./src/data/presets.mjs

License

This project is licensed under the MIT License - see the LICENSE file for details.