laktek / extract-values Goto Github PK
View Code? Open in Web Editor NEWA simple helper to extract values from a string based on a pattern.
A simple helper to extract values from a string based on a pattern.
When I originally added the unit tests section, I used backticks improperly. You'll see it.
https://github.com/laktek/extract-values/blob/master/Readme.md#unit-tests
JSHint shows a warning about window
being undefined. We should be able to hide this by inserting "browser": true
in .jshintrc
, per jshint/jshint#1063 (comment).
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 ?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.