Giter Site home page Giter Site logo

extract-values's People

Contributors

arunoda avatar bruchmann avatar laktek avatar mcandre avatar zeke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

extract-values's Issues

extracting value from question

thanks a lot for the great lib!

I want to extract values in question
"How about next thursday?", "about {time}"
but want to limit it only time between about and ?

Compile before parsing for performance

Hi

So I messed around and rewrote this to allow for compiling ahead before parsing and got some significant performance increases. I tested using your test case data.

https://jsperf.com/string-value-extraction/1

code

const RGX_SPECIAL_CHARS = /[\\\^\$\*\+\.\?\(\)]/g, // eslint-disable-line
    RGX_WHITESPACE = /\s+/g;

/**
 * compile ahead version of extract values
 * @see https://github.com/laktek/extract-values
 */
class ExtractValues {
    /**
     * @member {Object} ExtractValues~config
     * @private
     */

    /**
     * @member {string} ExtractValues~template
     * @private
     */

    /**
     * @member {string[]|null} ExtractValues~tokens
     * @private
     */

    /**
     * @member {RegExp} ExtractValues~extractor
     * @private
     */

    /**
     *
     * @param template
     * @param options
     */
    constructor(template, options = {}) {
        options = Object.assign({
            delimiters: ['{', '}'],
            lowercase : false,
            whitespace: null
        }, options);

        // validate

        const tokenizer = new RegExp(`${options.delimiters[0]}([^${options.delimiters.join('')}\t\r\n]+)${options.delimiters[1]}`, 'g'),
            tokenNormalizer = new RegExp(`${options.delimiters[0]}|${options.delimiters[1]}`, 'g');

        let tokens = template.match(tokenizer);

        if (tokens) {
            tokens = tokens.map(token => token.replace(tokenNormalizer, '').trim());
        }

        Object.defineProperties(this, {
            config: {
                enumerable: true,
                value     : options
            },
            template: {
                enumerable: true,
                value     : template
            },
            tokens: {
                enumerable: true,
                value     : tokens
            },
            extractor: {
                enumerable: true,
                value     : new RegExp(template.replace(RGX_SPECIAL_CHARS, '\\$&').replace(tokenizer, '(\.+)'))
            }
        });
    }

    /**
     * parses the input according to the compiles template
     * @param {string} str
     * @return {Object<string,string>|null}
     */
    parse(str) {
        if (this.config.lowercase) {
            str = str.toLowerCase();
        }

        if (this.config.whitespace != null) {
            const whitespaced = ' '.repeat(this.config.whitespace);

            str = str.replace(RGX_WHITESPACE, whitespaced);
        }

        let matches = str.match(this.extractor);

        if (!matches) {
            return null;
        }

        // Allow exact string matches to return an empty object instead of null
        if (!this.tokens) {
            return (str === this.template) ? {} : null;
        }

        matches = matches.splice(1);
        const output = {};

        for (let i = 0; i < this.tokens.length; i += 1) {
            output[this.tokens[i]] = matches[i];
        }

        return output;
    }
}

The only downside was that it was 30% or so slower if you had to create a new instance for every parse. I'm pretty sure that that is due to object creation and there's probably faster ways to do that, but for my cases I'll stick with the class since I will almost always compile ahead.

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.