Git Product home page Git Product logo

regenerate's Introduction

Regenerate Build status Code coverage status

Regenerate is a Unicode-aware regex generator for JavaScript. It allows you to easily generate ES5-compatible regular expressions based on a given set of Unicode symbols or code points. (This is trickier than you might think, because of how JavaScript deals with astral symbols.)

Installation

Via npm:

npm install regenerate

Via Bower:

bower install regenerate

In a browser:

<script src="regenerate.js"></script>

In Node.js, io.js, and RingoJS ≥ v0.8.0:

var regenerate = require('regenerate');

In Narwhal and RingoJS ≤ v0.7.0:

var regenerate = require('regenerate').regenerate;

In Rhino:

load('regenerate.js');

Using an AMD loader like RequireJS:

require(
  {
    'paths': {
      'regenerate': 'path/to/regenerate'
    }
  },
  ['regenerate'],
  function(regenerate) {
    console.log(regenerate);
  }
);

API

regenerate(value1, value2, value3, ...)

The main Regenerate function. Calling this function creates a new set that gets a chainable API.

var set = regenerate()
  .addRange(0x60, 0x69) // add U+0060 to U+0069
  .remove(0x62, 0x64) // remove U+0062 and U+0064
  .add(0x1D306); // add U+1D306
set.valueOf();
// → [0x60, 0x61, 0x63, 0x65, 0x66, 0x67, 0x68, 0x69, 0x1D306]
set.toString();
// → '[`ace-i]|\\uD834\\uDF06'
set.toRegExp();
// → /[`ace-i]|\uD834\uDF06/

Any arguments passed to regenerate() will be added to the set right away. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.

regenerate(0x1D306, 'A', '©', 0x2603).toString();
// → '[A\\xA9\\u2603]|\\uD834\\uDF06'

var items = [0x1D306, 'A', '©', 0x2603];
regenerate(items).toString();
// → '[A\\xA9\\u2603]|\\uD834\\uDF06'

regenerate.prototype.add(value1, value2, value3, ...)

Any arguments passed to add() are added to the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.

regenerate().add(0x1D306, 'A', '©', 0x2603).toString();
// → '[A\\xA9\\u2603]|\\uD834\\uDF06'

var items = [0x1D306, 'A', '©', 0x2603];
regenerate().add(items).toString();
// → '[A\\xA9\\u2603]|\\uD834\\uDF06'

It’s also possible to pass in a Regenerate instance. Doing so adds all code points in that instance to the current set.

var set = regenerate(0x1D306, 'A');
regenerate().add('©', 0x2603).add(set).toString();
// → '[A\\xA9\\u2603]|\\uD834\\uDF06'

Note that the initial call to regenerate() acts like add(). This allows you to create a new Regenerate instance and add some code points to it in one go:

regenerate(0x1D306, 'A', '©', 0x2603).toString();
// → '[A\\xA9\\u2603]|\\uD834\\uDF06'

regenerate.prototype.remove(value1, value2, value3, ...)

Any arguments passed to remove() are removed from the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.

regenerate(0x1D306, 'A', '©', 0x2603).remove('☃').toString();
// → '[A\\xA9]|\\uD834\\uDF06'

It’s also possible to pass in a Regenerate instance. Doing so removes all code points in that instance from the current set.

var set = regenerate('☃');
regenerate(0x1D306, 'A', '©', 0x2603).remove(set).toString();
// → '[A\\xA9]|\\uD834\\uDF06'

regenerate.prototype.addRange(start, end)

Adds a range of code points from start to end (inclusive) to the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted.

regenerate(0x1D306).addRange(0x00, 0xFF).toString(16);
// → '[\\0-\\xFF]|\\uD834\\uDF06'

regenerate().addRange('A', 'z').toString();
// → '[A-z]'

regenerate.prototype.removeRange(start, end)

Removes a range of code points from start to end (inclusive) from the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted.

regenerate()
  .addRange(0x000000, 0x10FFFF) // add all Unicode code points
  .removeRange('A', 'z') // remove all symbols from `A` to `z`
  .toString();
// → '[\\0-@\\{-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]'

regenerate()
  .addRange(0x000000, 0x10FFFF) // add all Unicode code points
  .removeRange(0x0041, 0x007A) // remove all code points from U+0041 to U+007A
  .toString();
// → '[\\0-@\\{-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]'

regenerate.prototype.intersection(codePoints)

Removes any code points from the set that are not present in both the set and the given codePoints array. codePoints must be an array of numeric code point values, i.e. numbers.

regenerate()
  .addRange(0x00, 0xFF) // add extended ASCII code points
  .intersection([0x61, 0x69]) // remove all code points from the set except for these
  .toString();
// → '[ai]'

Instead of the codePoints array, it’s also possible to pass in a Regenerate instance.

var whitelist = regenerate(0x61, 0x69);

regenerate()
  .addRange(0x00, 0xFF) // add extended ASCII code points
  .intersection(whitelist) // remove all code points from the set except for those in the `whitelist` set
  .toString();
// → '[ai]'

regenerate.prototype.contains(value)

Returns true if the given value is part of the set, and false otherwise. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted.

var set = regenerate().addRange(0x00, 0xFF);
set.contains('A');
// → true
set.contains(0x1D306);
// → false

regenerate.prototype.clone()

Returns a clone of the current code point set. Any actions performed on the clone won’t mutate the original set.

var setA = regenerate(0x1D306);
var setB = setA.clone().add(0x1F4A9);
setA.toArray();
// → [0x1D306]
setB.toArray();
// → [0x1D306, 0x1F4A9]

regenerate.prototype.toString(options)

Returns a string representing (part of) a regular expression that matches all the symbols mapped to the code points within the set.

regenerate(0x1D306, 0x1F4A9).toString();
// → '\\uD834\\uDF06|\\uD83D\\uDCA9'

If the bmpOnly property of the optional options object is set to true, the output matches surrogates individually, regardless of whether they’re lone surrogates or just part of a surrogate pair. This simplifies the output, but it can only be used in case you’re certain the strings it will be used on don’t contain any astral symbols.

var highSurrogates = regenerate().addRange(0xD800, 0xDBFF);
highSurrogates.toString();
// → '[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])'
highSurrogates.toString({ 'bmpOnly': true });
// → '[\\uD800-\\uDBFF]'

var lowSurrogates = regenerate().addRange(0xDC00, 0xDFFF);
lowSurrogates.toString();
// → '(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]'
lowSurrogates.toString({ 'bmpOnly': true });
// → '[\\uDC00-\\uDFFF]'

Note that lone low surrogates cannot be matched accurately using regular expressions in JavaScript without the use of lookbehind assertions, which aren't yet widely supported. Regenerate’s output makes a best-effort approach but there can be false negatives in this regard.

If the hasUnicodeFlag property of the optional options object is set to true, the output makes use of Unicode code point escapes (\u{…}) where applicable. This simplifies the output at the cost of compatibility and portability, since it means the output can only be used as a pattern in a regular expression with the ES6 u flag enabled.

var set = regenerate().addRange(0x0, 0x10FFFF);

set.toString();
// → '[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]''

set.toString({ 'hasUnicodeFlag': true });
// → '[\\0-\\u{10FFFF}]'

regenerate.prototype.toRegExp(flags = '')

Returns a regular expression that matches all the symbols mapped to the code points within the set. Optionally, you can pass flags to be added to the regular expression.

var regex = regenerate(0x1D306, 0x1F4A9).toRegExp();
// → /\uD834\uDF06|\uD83D\uDCA9/
regex.test('𝌆');
// → true
regex.test('A');
// → false

// With flags:
var regex = regenerate(0x1D306, 0x1F4A9).toRegExp('g');
// → /\uD834\uDF06|\uD83D\uDCA9/g

Note: This probably shouldn’t be used. Regenerate is intended as a tool that is used as part of a build process, not at runtime.

regenerate.prototype.valueOf() or regenerate.prototype.toArray()

Returns a sorted array of unique code points in the set.

regenerate(0x1D306)
  .addRange(0x60, 0x65)
  .add(0x59, 0x60) // note: 0x59 is added after 0x65, and 0x60 is a duplicate
  .valueOf();
// → [0x59, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x1D306]

regenerate.version

A string representing the semantic version number.

Combine Regenerate with other libraries

Regenerate gets even better when combined with other libraries such as Punycode.js. Here’s an example where Punycode.js is used to convert a string into an array of code points, that is then passed on to Regenerate:

var regenerate = require('regenerate');
var punycode = require('punycode');

var string = 'Lorem ipsum dolor sit amet.';
// Get an array of all code points used in the string:
var codePoints = punycode.ucs2.decode(string);

// Generate a regular expression that matches any of the symbols used in the string:
regenerate(codePoints).toString();
// → '[ \\.Ladeilmopr-u]'

In ES6 you can do something similar with Array.from which uses the string’s iterator to split the given string into an array of strings that each contain a single symbol. regenerate() accepts both strings and code points, remember?

var regenerate = require('regenerate');

var string = 'Lorem ipsum dolor sit amet.';
// Get an array of all symbols used in the string:
var symbols = Array.from(string);

// Generate a regular expression that matches any of the symbols used in the string:
regenerate(symbols).toString();
// → '[ \\.Ladeilmopr-u]'

Support

Regenerate supports at least Chrome 27+, Firefox 3+, Safari 4+, Opera 10+, IE 6+, Node.js v0.10.0+, io.js v1.0.0+, Narwhal 0.3.2+, RingoJS 0.8+, PhantomJS 1.9.0+, and Rhino 1.7RC4+.

Unit tests & code coverage

After cloning this repository, run npm install to install the dependencies needed for Regenerate development and testing. You may want to install Istanbul globally using npm install istanbul -g.

Once that’s done, you can run the unit tests in Node using npm test or node tests/tests.js. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use grunt test.

To generate the code coverage report, use grunt cover.

Author

twitter/mathias
Mathias Bynens

License

Regenerate is available under the MIT license.

regenerate's People

Contributors

bnjmnt4n avatar devongovett avatar jackmoore avatar mathiasbynens avatar seb35 avatar yaffle 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  avatar

regenerate's Issues

Add `regenerate.prototype.remove(fn)`

Things like regenerate(codePoints).removeRange(0x010000, 0x10FFFF) are very slow, since it creates an array containing all the code points in that range, and then walks through the code points looking for matches.

Something like this would be a much faster solution in that case:

regenerate(codePoints).remove(function(codePoint) {
  return codePoint > 0xFFFF; // remove astral code points from the set
});

Avoid overhead of calling `dataFromCodePoints()` etc. internally

When passing CodePointSets as arguments, e.g. setC = regenerate(setA).difference(setB), the overhead of calling dataFromCodePoints() etc. internally can be avoided. We could optimize these cases, although it’s debatable whether it’s worth it.

For difference specifically, we could do something similar to what @inimino uses, now that we use the same data structure:

function dataDifferenceData(dataA, dataB) {
  var ret = [];
  var i = 0;
  var j = 0;
  var a;
  var b;
  var al = dataA.length;
  var bl = dataB.length;
  var last;
  var state = 0;
  if (!al) {
    return [];
  }
  if (!bl) {
    return dataA;
  }
  a = dataA[0];
  b = dataB[0];
  if (isNaN(a) || isNaN(b)) {
    throw Error('cset_difference: bad input');
  }
  for (;;) {
    if (a < b) {
      if (!(state & 1)) {
        if (a == last) {
          ret.pop();
        }
        else {
          ret.push(a);
        }
        last = a;
      }
      state ^= 2;
      a = ++i < al ? dataA[i] : 0x110000;
    } else {
      if (a == 0x110000 && b == 0x110000) {
        return ret;
      }
      if (state & 2) {
        if (b == last) {
          ret.pop();
        } else {
          ret.push(b);
        }
        last = b;
      }
      state ^= 1;
      b = ++j < bl ? dataB[j] : 0x110000;
    }
  }
}

Avoid `Array#splice` where possible

As an optimization, we should replace calls to array.splice(a, b, c) where b=1 with just array[a] = c (and similar for other argument counts).

Test more environments

I want to add this to the README:

## Support

Regenerate has been tested in at least Chrome 27-29, Firefox 3-22, Safari 4-6, Opera 10-12, IE 6-10, Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.9, and Rhino 1.7RC4.

Let’s make it happen! Environments to test:

  • Chrome 27-29
  • Firefox 3-22
  • Safari 4-6
  • Opera 10-12
  • IE 6-10
  • Node.js v0.10.0
  • Narwhal 0.3.2
  • RingoJS 0.8-0.9
  • Rhino 1.7RC4
  • PhantomJS

The test suite is pretty damn slow, but hey. Testing Rhino can take up to 90 seconds on my machine. Narwhal: 150 seconds.

Script to convert any Regenerate set to code that generates it

This is not really a bug report, but rather a script that is useful when generating test cases:

regenerate.prototype.toCode = function() {
    var data = this.data;
    // Iterate over the data per `(start, end)` pair.
    var index = 0;
    var start;
    var end;
    var length = data.length;
    var loneCodePoints = [];
    var ranges = [];
    while (index < length) {
        start = data[index];
        end = data[index + 1] - 1; // Note: the `- 1` makes `end` inclusive.
        if (start == end) {
            loneCodePoints.push('0x' + start.toString(16).toUpperCase());
        } else {
            ranges.push(
                'addRange(0x' + start.toString(16).toUpperCase() +
                ', 0x' + end.toString(16).toUpperCase() + ')'
            );
        }
        index += 2;
    }
    return 'regenerate(' + loneCodePoints.join(', ') + ')' +
        (ranges.length ? '.' + ranges.join('.') : '');
};

var set = regenerate(0x1D306, 'a').addRange(0x2CEE, 0x4DFF);
console.log(set.toCode());
// → 'regenerate(0x61, 0x1D306).addRange(0x2CEE, 0x4DFF)'

Let’s allow extending regenerate.prototype for plugins like this.

Incorrect optimization in surrogateMappings

The surrogate pair of 0x1F610 is \uD83D\uDE10 and it got lost during some RegExp optimizations.
Consider this test case:

const set = regenerate().addRange(0x1F000, 0x1F1FF).addRange(0x1F300, 0x1F5FF).add(0x1F610);
const testChar = String.fromCodePoint(0x1F610);

The RegExp in Unicode mode is correct:

console.log(set.toRegExp('u'));    // /[\u{1F000}-\u{1F1FF}\u{1F300}-\u{1F5FF}\u{1F610}]/u
testChar.match(set.toRegExp('u')); // match found

The RegExp in "legacy" mode is incorrect (missing surrogate pair for 0x1F610):

console.log(set.toRegExp());    // /[\uD83C\uD83D][\uDC00-\uDDFF\uDF00-\uDFFF]/
testChar.match(set.toRegExp()); // null

I know that patches are welcome, but I don't have spare time right now. Will look into the source code later :-)

Invert regular expression?

I wonder if it's possible to negate the resulting regexp in regenerate.

One could apparently use negative look-ahead (like (?!expr)), but it is said to perform bad. One could also invert the result of regexp.test(str) but it might not be ideal either.

How would I go about to "flip" the ranges? Ideally, there would be a built-in method .invert(). Another possible approach would be to add the entire range of Unicode characters, then use .remove() and .removeRange() on that.

Bug in `.remove(set)`?

var ID_Start = regenerate(require('unicode-6.3.0/properties/ID_Start/code-points'));
var ID_Continue = regenerate(require('unicode-6.3.0/properties/ID_Continue/code-points'));
var set = ID_Continue.clone().remove(ID_Start);

set’s data structure looks messed up. Here’s the first 20 entries:

[ '30', // odd = start
  '3a', // even = end (exclusive, so at least 1 higher than `start`)
  '5f',
  '60',
  'b7',
  'b8',
  '300',
  '370',
  '375', // start
  '375', // end → WTF
  '387',
  '388',
  '38b', // start
  '38b', // end → WTF
  '483',
  '488',
  '591',
  '5be',
  '5bf',
  '5c0' ]

Don’t move unmatched high surrogates to the end of the regex if it’s not needed

Currently:

// Create a regular expression that matches any BMP code point:
regenerate.fromCodePointRange(0x0000, 0xFFFF);
// → '[\\0-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF]'

Any unmatched high surrogates need to be moved to the end of the regex only if the list of code points includes at least 1 astral symbol. This is not the case in the above example, so (I think) the result could just be:

// Create a regular expression that matches any BMP code point:
regenerate.fromCodePointRange(0x0000, 0xFFFF);
// → '[\\0-\\uFFFF]'

I might be missing something here, so I’m hoping someone like @slevithan can double-check this.

Optimize `regenerate.prototype.removeRange`

Ref. #12.

Instead of creating a range in such a case, just convert the arguments to code points (if they aren’t already) and loop over the code points in the set, removing those that are not within the bounds of the range.

`regenerate.fromCodePointRanges` or similar

Make it easier to create regular expressions that consist of a set of ranges:

E.g. when presented with a list like this (taken from http://www.w3.org/TR/turtle/#sec-grammar-grammar):

[A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6] | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

Maybe something like this? (Using different code points than in the above example to keep it simple.)

regenerate.foo([
  '200C',
  'F900-FDCF',
  'FDF0-FFFD',
  '010000-0EFFFF'
]);

Any ideas on how to name such a method (foo in the example), or ideas on what would be a better API are more than welcome :) Perhaps we could overload fromCodePoints at the cost of performance?

Optimization / “bug” with ranges

regenerate.fromCodePoints([ 0x20, 0x21 ]);
// → '[\\x20\\x21] (correct)

regenerate.fromCodePoints([ 0x20, 0x21, 0x23 ]);
// → '[\\x20-\\x21\\x23]', which works fine, but just '[\\x20\\x21\\x23]' would be better

Non-minimal regex when adding a second range lower than the first

With the specific following instructions, regenerate produces a non-minimal (but correct) regex:

regenerate().addRange( 99, 100 ).addRange( 65, 99 ).toString()
'[cdA-c]'

By switching the order, we have a correct minimal regex:

regenerate().addRange( 65, 99 ).addRange( 99, 100 ).toString()
'[A-d]'

Playing a bit with the boundaries, it seems it only occur when the end of the second range is exactly the same as the start of the first range.

Reduce file size to use it as a RegExp polyfill

The library size is too big for using it as a polyfill. Maybe you can do something to reduce it side? For example: The polyfill do not need a methods such 'intersection' or 'contains', it's only need a addRange and removeRange.

I tried to make a small version of you library, but it doesn't work as good as the original one. Also it based on old version of regenerate library. But my version is very small - just what I need.

Unexpected matches in the resulting RegExp

I was trying to use this for twemoji but I've found a very weird behavior I'm not sure it's me doing it wrong or there's a bug in here (haven't checked your source code yet).

So, the resulting RegExp matches # and every char between 0 and 9 and I've no idea what's going on and why is that, so I've prepared this test code:

var list = ["🇨🇳", "🇺🇸", "🇷🇺", "🇰🇷", "🇯🇵", "🇮🇹", "🇬🇧", "🇫🇷", "🇪🇸", "🇩🇪", "9⃣", "8⃣", "7⃣", "6⃣", "5⃣", "4⃣", "3⃣", "2⃣", "1⃣", "0⃣", "#⃣", "🚳", "🚱", "🚰", "🚯", "🚮", "🚦", "🚣", "🚡", "🚠", "🚟", "🚞", "🚝", "🚜", "🚛", "🚘", "🚖", "🚔", "🚐", "🚎", "🚍", "🚋", "🚊", "🚈", "🚆", "🚂", "🚁", "😶", "😴", "😯", "😮", "😬", "😧", "😦", "😟", "😛", "😙", "😗", "😕", "😑", "😐", "😎", "😈", "😇", "😀", "🕧", "🕦", "🕥", "🕤", "🕣", "🕢", "🕡", "🕠", "🕟", "🕞", "🕝", "🕜", "🔭", "🔬", "🔕", "🔉", "🔈", "🔇", "🔆", "🔅", "🔄", "🔂", "🔁", "🔀", "📵", "📯", "📭", "📬", "💷", "💶", "💭", "👭", "👬", "👥", "🐪", "🐖", "🐕", "🐓", "🐐", "🐏", "🐋", "🐊", "🐉", "🐈", "🐇", "🐆", "🐅", "🐄", "🐃", "🐂", "🐁", "🐀", "🏤", "🏉", "🏇", "🍼", "🍐", "🍋", "🌳", "🌲", "🌞", "🌝", "🌜", "🌚", "🌘", "🃏", "🅰", "🅱", "🅾", "🆎", "🆑", "🆒", "🆓", "🆔", "🆕", "🆖", "🆗", "🆘", "🆙", "🆚", "👷", "🛅", "🛄", "🛃", "🛂", "🛁", "🚿", "🚸", "🚷", "🚵", "🈁", "🈂", "🈲", "🈳", "🈴", "🈵", "🈶", "🈷", "🈸", "🈹", "🈺", "🉐", "🉑", "🌀", "🌁", "🌂", "🌃", "🌄", "🌅", "🌆", "🌇", "🌈", "🌉", "🌊", "🌋", "🌌", "🌏", "🌑", "🌓", "🌔", "🌕", "🌙", "🌛", "🌟", "🌠", "🌰", "🌱", "🌴", "🌵", "🌷", "🌸", "🌹", "🌺", "🌻", "🌼", "🌽", "🌾", "🌿", "🍀", "🍁", "🍂", "🍃", "🍄", "🍅", "🍆", "🍇", "🍈", "🍉", "🍊", "🍌", "🍍", "🍎", "🍏", "🍑", "🍒", "🍓", "🍔", "🍕", "🍖", "🍗", "🍘", "🍙", "🍚", "🍛", "🍜", "🍝", "🍞", "🍟", "🍠", "🍡", "🍢", "🍣", "🍤", "🍥", "🍦", "🍧", "🍨", "🍩", "🍪", "🍫", "🍬", "🍭", "🍮", "🍯", "🍰", "🍱", "🍲", "🍳", "🍴", "🍵", "🍶", "🍷", "🍸", "🍹", "🍺", "🍻", "🎀", "🎁", "🎂", "🎃", "🎄", "🎅", "🎆", "🎇", "🎈", "🎉", "🎊", "🎋", "🎌", "🎍", "🎎", "🎏", "🎐", "🎑", "🎒", "🎓", "🎠", "🎡", "🎢", "🎣", "🎤", "🎥", "🎦", "🎧", "🎨", "🎩", "🎪", "🎫", "🎬", "🎭", "🎮", "🎯", "🎰", "🎱", "🎲", "🎳", "🎴", "🎵", "🎶", "🎷", "🎸", "🎹", "🎺", "🎻", "🎼", "🎽", "🎾", "🎿", "🏀", "🏁", "🏂", "🏃", "🏄", "🏆", "🏈", "🏊", "🏠", "🏡", "🏢", "🏣", "🏥", "🏦", "🏧", "🏨", "🏩", "🏪", "🏫", "🏬", "🏭", "🏮", "🏯", "🏰", "🐌", "🐍", "🐎", "🐑", "🐒", "🐔", "🐗", "🐘", "🐙", "🐚", "🐛", "🐜", "🐝", "🐞", "🐟", "🐠", "🐡", "🐢", "🐣", "🐤", "🐥", "🐦", "🐧", "🐨", "🐩", "🐫", "🐬", "🐭", "🐮", "🐯", "🐰", "🐱", "🐲", "🐳", "🐴", "🐵", "🐶", "🐷", "🐸", "🐹", "🐺", "🐻", "🐼", "🐽", "🐾", "👀", "👂", "👃", "👄", "👅", "👆", "👇", "👈", "👉", "👊", "👋", "👌", "👍", "👎", "👏", "👐", "👑", "👒", "👓", "👔", "👕", "👖", "👗", "👘", "👙", "👚", "👛", "👜", "👝", "👞", "👟", "👠", "👡", "👢", "👣", "👤", "👦", "👧", "👨", "👩", "👪", "👫", "👮", "👯", "👰", "👱", "👲", "👳", "👴", "👵", "👶", "🚴", "👸", "👹", "👺", "👻", "👼", "👽", "👾", "👿", "💀", "💁", "💂", "💃", "💄", "💅", "💆", "💇", "💈", "💉", "💊", "💋", "💌", "💍", "💎", "💏", "💐", "💑", "💒", "💓", "💔", "💕", "💖", "💗", "💘", "💙", "💚", "💛", "💜", "💝", "💞", "💟", "💠", "💡", "💢", "💣", "💤", "💥", "💦", "💧", "💨", "💩", "💪", "💫", "💬", "💮", "💯", "💰", "💱", "💲", "💳", "💴", "💵", "💸", "💹", "💺", "💻", "💼", "💽", "💾", "💿", "📀", "📁", "📂", "📃", "📄", "📅", "📆", "📇", "📈", "📉", "📊", "📋", "📌", "📍", "📎", "📏", "📐", "📑", "📒", "📓", "📔", "📕", "📖", "📗", "📘", "📙", "📚", "📛", "📜", "📝", "📞", "📟", "📠", "📡", "📢", "📣", "📤", "📥", "📦", "📧", "📨", "📩", "📪", "📫", "📮", "📰", "📱", "📲", "📳", "📴", "📶", "📷", "📹", "📺", "📻", "📼", "🔃", "🔊", "🔋", "🔌", "🔍", "🔎", "🔏", "🔐", "🔑", "🔒", "🔓", "🔔", "🔖", "🔗", "🔘", "🔙", "🔚", "🔛", "🔜", "🔝", "🔞", "🔟", "🔠", "🔡", "🔢", "🔣", "🔤", "🔥", "🔦", "🔧", "🔨", "🔩", "🔪", "🔫", "🔮", "🔯", "🔰", "🔱", "🔲", "🔳", "🔴", "🔵", "🔶", "🔷", "🔸", "🔹", "🔺", "🔻", "🔼", "🔽", "🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚", "🕛", "🗻", "🗼", "🗽", "🗾", "🗿", "😁", "😂", "😃", "😄", "😅", "😆", "😉", "😊", "😋", "😌", "😍", "😏", "😒", "😓", "😔", "😖", "😘", "😚", "😜", "😝", "😞", "😠", "😡", "😢", "😣", "😤", "😥", "😨", "😩", "😪", "😫", "😭", "😰", "😱", "😲", "😳", "😵", "😷", "😸", "😹", "😺", "😻", "😼", "😽", "😾", "😿", "🙀", "🙅", "🙆", "🙇", "🙈", "🙉", "🙊", "🙋", "🙌", "🙍", "🙎", "🙏", "🚀", "🚃", "🚄", "🚅", "🚇", "🚉", "🚌", "🚏", "🚑", "🚒", "🚓", "🚕", "🚗", "🚙", "🚚", "🚢", "🚤", "🚥", "🚧", "🚨", "🚩", "🚪", "🚫", "🚬", "🚭", "🚲", "🚶", "🚹", "🚺", "🚻", "🚼", "🚽", "🚾", "🛀", "🇦", "🇧", "🇨", "🇩", "🇪", "🇫", "🇬", "🇭", "🇮", "🇯", "🇰", "🇱", "🇲", "🇳", "🇴", "🇵", "🇶", "🇷", "🇸", "🇹", "🇺", "🇻", "🇼", "🇽", "🇾", "🇿", "🌍", "🌎", "🌐", "🌒", "🌖", "🌗", "", "〰", "➰", "➗", "➖", "➕", "❕", "❔", "❓", "❎", "❌", "✨", "✋", "✊", "✅", "⛎", "⏳", "⏰", "⏬", "⏫", "⏪", "⏩", "™", "➿", "©", "®"];
var regenerate = require('regenerate');
var regenerated = regenerate.apply(null, list);
console.log(list.filter(function (chr) {
  return (0 <= chr && chr <= 9) || chr == '#';
}).length ?
  'There should be #0-9 in the RegExp' :
  'There should be NO #0-9 in the RegExp'
);
console.log(regenerated.toRegExp());

This should not result in the following RegExp:

/[#0-9\xA9\xAE\u2122\u23E9-\u23EC\u23F0\u23F3\u26CE\u2705\u270A\u270B\u2728\u274C\u274E\u2753-\u2755\u2795-\u2797\u27B0\u27BF\u3030\uE50A]|\uD83C[\uDCCF\uDD70\uDD71\uDD7E\uDD8E\uDD91-\uDD9A\uDDE6-\uDDFF\uDE01\uDE02\uDE32-\uDE3A\uDE50\uDE51\uDF00-\uDF20\uDF30-\uDF35\uDF37-\uDF7C\uDF80-\uDF93\uDFA0-\uDFC4\uDFC6-\uDFCA\uDFE0-\uDFF0]|\uD83D[\uDC00-\uDC3E\uDC40\uDC42-\uDCF7\uDCF9-\uDCFC\uDD00-\uDD3D\uDD50-\uDD67\uDDFB-\uDE40\uDE45-\uDE4F\uDE80-\uDEC5]/

'cause #0-9 is absolutely undesired as a match.

Thanks for any sort of outcome.

Allow chaining

E.g.

regenerate(regenerate.range(0x0000, 0x000A))
    .add(0x1D306)
    .add([0x00A9, 0x0192])
    .addRange(0x0100, 0x0200)
    .remove(0x0002)
    .removeRange(0x0100, 0x0190)
    .difference([0x0004, 0x0007])
    .intersection([0x192, 0x196])
    .toString();
    // [\\u0192\\u0192\\u0196]

(Silly example, but you get the idea.)

Add `difference` and others

// get an array of all BMP code points except U+0067, U+23FC, and U+F3FC
var codePoints = regenerate.difference(regenerate.range(0x0, 0xFFFF), [0x67, 0x23FC, 0xF3FC]);

Maybe also these:

union(array1, array2) / add(array1, array2)
intersect(array1, array2)
remove(array1, array2)

isArray is not using the native isArray method, causing incompatibilities with prototype.js

isArray should use the browser's native implementation of isArray if available.
This causes errors when running regenerate together with prototype.js:

symbol.charCodeAt is not a function
	TypeError: symbol.charCodeAt is not a function
	    at symbolToCodePoint (http://localhost:1090/IPdeploy/js/systemjs-babel-browser.js:24493:23)
	    at regenerate.add (http://localhost:1090/IPdeploy/js/systemjs-babel-browser.js:24928:64)
	    at regenerate (http://localhost:1090/IPdeploy/js/systemjs-babel-browser.js:24902:28)
	    at eval (http://localhost:1090/IPdeploy/js/systemjs-babel-browser.js:25033:8)

Write a fuzzer to try and find more bugs

We can use Regenerate v0.5.4 (for which the output is 100% trustworthy), patch its codePointToSymbol to the latest version, and then compare its results with v0.6.x results. Any differences in output are most likely definitely bugs.

Improve performance / decrease memory usage

At the moment, Regenerate internally stores an array of all code points in the current set in memory. This is nice and easy, but it gets pretty slow for large sets of code points. It might be better to make this array smaller by making use of ranges. For example, instead of:

[0, 1, 2, 3, 4, 5, 6, 10, 12, 13, 14, 15, 18]

…we could just store something like:

[[0, 6], 10, [12, 15], 18]

This would require a complete rewrite of Regenerate, though.

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.