Skip to content

Commit

Permalink
Merge 961c0aa into b81931d
Browse files Browse the repository at this point in the history
  • Loading branch information
dsfields committed Oct 24, 2017
2 parents b81931d + 961c0aa commit 90a0d5e
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 27 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## 1.0

### 1.3.0

__New Features__
* Adding `Like.prototype.toRegex()`.
* Adding `Like.prototype.toRegexString()`.

__Bug Fixes__
* Encountering the escape a character threw a parser error if it preceded a character that is not one of `spleen`'s special characters. This fix will ensure that the escape character is ignored if it's escaping an un-escapable character.

### 1.2.0

__New Features__
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,11 @@ Represents a "like" string matching expression. This clause is used as the "obj

- `value`: a string value to match.

This method returns a Boolean.
This method returns a Boolean.

+ `Like.prototype.toRegex()`: converts the Like pattern to a compatible regular expression. This method returns in an instance of [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).

+ `Like.prototype.toRegexString()`: converts the Like pattern to a compatible regular expression string. This method returns a string.

#### Class: `PrioritizeStrategy`

Expand Down
80 changes: 58 additions & 22 deletions lib/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,69 @@ class Like {
/** @private */
constructor(value) {
this.value = value;
this.regex = null;
this._regex = null;
this._regexString = null;
}


/** @private */
static _parse(value) {
/**
* Determines whether or not the pattern matches a string value.
*
* @param {String} value
* A value to match
*
* @return {Boolean}
*/
match(value) {
if (typeof value !== 'string') return false;
if (this._regex === null) this._regex = this.toRegex();
return this._regex.test(value);
}


/**
* Converts the Like pattern to a compatible regular expression.
*
* @return {RegExp}
*/
toRegex() {
if (this._regex !== null) return this._regex;
const regex = this.toRegexString();
this._regex = new RegExp(regex, 'i');
return this._regex;
}


/**
* Converts the Like pattern to a compatible regular expression string.
*
* @return {String}
*/
toRegexString() {
if (this._regexString !== null) return this._regexString;

let regex = '^';
let isEscaping = false;

for (let i = 0; i < value.length; i++) {
const char = value[i];
for (let i = 0; i < this.value.length; i++) {
const char = this.value[i];

if (isEscaping) {
isEscaping = false;
if (char === '*') regex += '\\*';
else regex += char;
switch (char) {
case '*':
regex += '\\*';
break;

case '_':
regex += '_';
break;

default:
regex += `\\\\${char}`;
break;
}

continue;
}

Expand All @@ -47,24 +94,13 @@ class Like {
}
}

regex += '$';
if (isEscaping) regex += '\\\\';

return new RegExp(regex, 'i');
}
regex += '$';

this._regexString = regex;

/**
* Determines whether or not the pattern matches a string value.
*
* @param {String} value
* A value to match
*
* @return {Boolean}
*/
match(value) {
if (typeof value !== 'string') return false;
if (this.regex === null) this.regex = Like._parse(this.value);
return this.regex.test(value);
return regex;
}


Expand Down
11 changes: 9 additions & 2 deletions lib/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class Tokenizer {


error(data) {
const val = elv.coalesce(this.current, data).value.toString();
const val = elv.coalesce(
data,
this.current,
{ value: 'undefined' }
).value.toString();

const pos = this.cursor - val.length;

const msg = 'Unexpected token "'
Expand Down Expand Up @@ -353,14 +358,16 @@ class Tokenizer {
break;
}

isEscaping = false;

if (type === Token.none) type = Token.unknown;
value += char;
this.cursor++;
break;
}
}

if (isInString) throw this.error({ value });
if (isInString || isEscaping) throw this.error({ value });

if (type === Token.target) {
this._handleTargetPart(type, value);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "spleen",
"longName": "spleen",
"description": "Dynamic filter expression parsing, formatting, and abstractions for web APIs.",
"version": "1.2.0",
"version": "1.3.0",
"dependencies": {
"elv": "^1.0.0"
},
Expand All @@ -26,7 +26,8 @@
"search",
"api",
"input",
"param"
"param",
"predicates"
],
"license": "MIT",
"main": "./lib",
Expand Down

0 comments on commit 90a0d5e

Please sign in to comment.