Skip to content

Commit

Permalink
Added support for well-known symbols. Fixes #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Dec 2, 2017
1 parent dc69a61 commit 6c59ad2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -58,6 +58,23 @@ Supported methods:
* [`re2.test(str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test)
* [`re2.toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString)

Starting with 1.6.0 following well-known symbol-based methods are supported (see [Symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)):

* [`re2[Symbol.match](str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/match)
* [`re2[Symbol.search](str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/search)
* [`re2[Symbol.replace](str, newSubStr|function)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/replace)
* [`re2[Symbol.split](str[, limit])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/split)

It allows to use `RE2` instances on strings directly, just like `RegExp` instances:

```js
var re = new RE2("1");
"213".match(re); // [ '1', index: 1, input: '213' ]
"213".search(re); // 1
"213".replace(re, "+"); // 2+3
"213".split(re); // [ '2', '3' ]
```

## Extensions

### Shortcut construction
Expand All @@ -83,6 +100,8 @@ exchanging positions of a string, and a regular expression:
* `re2.split(str[, limit])`
* See [`str.split(regexp[, limit])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)

Starting with 1.6.0, these methods added as well-known symbol-based methods to be used transparently with ES6 string/regex machinery.

### `Buffer` support

In order to support `Buffer` directly, most methods can accept buffers instead of strings. It speeds up all operations.
Expand Down Expand Up @@ -295,6 +314,7 @@ Or:

## Release history

- 1.6.0 *Added well-known symbol-based methods of ES6. Refreshed NAN.*
- 1.5.0 *Bug fixes, error checks, better docs. Thx [Jamie Davis](https://github.com/davisjam), and [omg](https://github.com/omg)!*
- 1.4.1 *Minor corrections in README.*
- 1.4.0 *Use re2 as a git submodule. Thx [Ben James](https://github.com/benhjames)!*
Expand Down
11 changes: 9 additions & 2 deletions re2.js
@@ -1,5 +1,12 @@
"use strict";

var re2 = require('./build/Release/re2.node');
var RE2 = require('./build/Release/re2.node');

module.exports = re2;
if (typeof Symbol != 'undefined') {
Symbol.match && (RE2.prototype[Symbol.match] = function (str) { return this.match(str); });
Symbol.search && (RE2.prototype[Symbol.search] = function (str) { return this.search(str); });
Symbol.replace && (RE2.prototype[Symbol.replace] = function (str, repl) { return this.replace(str, repl); });
Symbol.split && (RE2.prototype[Symbol.split] = function (str, limit) { return this.split(str, limit); });
}

module.exports = RE2;
95 changes: 95 additions & 0 deletions tests/test_symbols.js
@@ -0,0 +1,95 @@
"use strict";


var unit = require("heya-unit");
var RE2 = require("../re2");


// tests

unit.add(module, [
function test_match_symbol (t) {
"use strict";

if (typeof Symbol == 'undefined' || !Symbol.match) return;

var str = "For more information, see Chapter 3.4.5.1";

var re = new RE2(/(chapter \d+(\.\d)*)/i);
var result = str.match(re);

eval(t.TEST("result.input === str"));
eval(t.TEST("result.index === 26"));
eval(t.TEST("result.length === 3"));
eval(t.TEST("result[0] === 'Chapter 3.4.5.1'"));
eval(t.TEST("result[1] === 'Chapter 3.4.5.1'"));
eval(t.TEST("result[2] === '.1'"));
},
function test_search_symbol (t) {
"use strict";

if (typeof Symbol == 'undefined' || !Symbol.search) return;

var str = "Total is 42 units.";

var re = new RE2(/\d+/i);
var result = str.search(re);
eval(t.TEST("result === 9"));

re = new RE2("\\b[a-z]+\\b");
result = str.search(re);
eval(t.TEST("result === 6"));

re = new RE2("\\b\\w+\\b");
result = str.search(re);
eval(t.TEST("result === 0"));

re = new RE2("z", "gm");
result = str.search(re);
eval(t.TEST("result === -1"));
},
function test_replace_symbol (t) {
"use strict";

if (typeof Symbol == 'undefined' || !Symbol.replace) return;

var re = new RE2(/apples/gi);
var result = "Apples are round, and apples are juicy.".replace(re, "oranges");
eval(t.TEST("result === 'oranges are round, and oranges are juicy.'"));

re = new RE2(/xmas/i);
result = "Twas the night before Xmas...".replace(re, "Christmas");
eval(t.TEST("result === 'Twas the night before Christmas...'"));

re = new RE2(/(\w+)\s(\w+)/);
result = "John Smith".replace(re, "$2, $1");
eval(t.TEST("result === 'Smith, John'"));
},
function test_split(t) {
"use strict";

if (typeof Symbol == 'undefined' || !Symbol.split) return;

var re = new RE2(/\s+/);
var result = "Oh brave new world that has such people in it.".split(re);
eval(t.TEST("t.unify(result, ['Oh', 'brave', 'new', 'world', 'that', 'has', 'such', 'people', 'in', 'it.'])"));

re = new RE2(",");
result = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(re);
eval(t.TEST("t.unify(result, ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'])"));

re = new RE2(/\s*;\s*/);
result = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ".split(re);
eval(t.TEST("t.unify(result, ['Harry Trump', 'Fred Barney', 'Helen Rigby', 'Bill Abel', 'Chris Hand '])"));

re = new RE2(/\s+/);
result = "Hello World. How are you doing?".split(re, 3);
eval(t.TEST("t.unify(result, ['Hello', 'World.', 'How'])"));

re = new RE2(/(\d)/);
result = "Hello 1 word. Sentence number 2.".split(re);
eval(t.TEST("t.unify(result, ['Hello ', '1', ' word. Sentence number ', '2', '.'])"));

eval(t.TEST("'asdfghjkl'.split(RE2(/[x-z]*/)).reverse().join('') === 'lkjhgfdsa'"));
}
]);
1 change: 1 addition & 0 deletions tests/tests.js
Expand Up @@ -12,6 +12,7 @@ var testReplace = require("./test_replace");
var testSearch = require("./test_search");
var testSplit = require("./test_split");
var testInvalid = require("./test_invalid");
var testSplit = require("./test_symbols");


unit.run();

0 comments on commit 6c59ad2

Please sign in to comment.