Skip to content

Instantly share code, notes, and snippets.

// RegExp:
/x*?/
// 1. Char is main, repetition is secondary (modifier)
{
type: 'Char',
value: 'x',
modifier: {
@DmitrySoshnikov
DmitrySoshnikov / Boolean.parse.md
Last active March 21, 2017 20:10
ES proposal: Boolean.parse

Boolean.parse(string)

Specification

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

When the Boolean.parse function is called with a string argument the following steps are taken:

  1. If Type(string) is not "string"
  • Return false
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-example1.json
Last active March 30, 2017 05:39
regexp-tree-example1.json
{
"type": "RegExp",
"body": {
"type": "Repetition",
"expression": {
"type": "CharacterClass",
"expressions": [
{
"type": "ClassRange",
"from": {
const regexpTree = require('regexp-tree');
console.log(regexpTree.parse(/a|b/i)); // RegExp AST
@DmitrySoshnikov
DmitrySoshnikov / ES7 Notes.txt
Last active February 9, 2018 19:42
ES7 Notes
// by Dmitry Soshnikov
ES7:
== Lexical environment ==
- Environment Record
- parent (can be null)
== Environment Record ==
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-processing.js
Last active April 4, 2017 06:31
regexp-tree-processing
const regexpTree = require('regexp-tree');
// Get AST.
const ast = regexpTree.parse('/[a-z]{1,}/');
// Handle nodes.
regexpTree.traverse(ast, {
// Handle "Quantifier" node type,
// transforming `{1,}` quantifier to `+`.
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-generator.js
Last active March 30, 2017 05:39
regexp-tree-generator
const regexpTree = require('regexp-tree');
const re = regexpTree.generate({
type: 'RegExp',
body: {
type: 'Char',
value: 'a',
kind: 'simple',
},
flags: 'gi',
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-optimizer.js
Created April 17, 2017 19:54
regexp-tree-optimizer
const regexpTree = require('regexp-tree');
const originalRe = /[a-zA-Z_0-9][A-Z_\da-z]*\e{1,}/;
const optimizedRe = regexpTree
.optimize(originalRe)
.toRegExp();
console.log(optimizedRe); // /\w+e+/
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-compat-transpiler.js
Created April 17, 2017 19:57
regexp-tree-compat-transpiler
const regexpTree = require('regexp-tree');
// Using new syntax.
const originalRe = '/(?<all>.)\\k<all>/s';
// For legacy engines.
const compatTranspiledRe = regexpTree
.compatTranspile(originalRe)
.toRegExp();
/
# A regular expression for date.
(?<year>\d{4})- # year part of a date
(?<month>\d{2})- # month part of a date
(?<day>\d{2}) # day part of a date
/x
// Is translated into: