This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple and educational `malloc` implementation. | |
* | |
* Dmitry Soshnikov <[email protected]> | |
* | |
* Maintains explicit linked list of allocated memory blocks. Each block | |
* has a header, containing meta-information, such as whether a block is | |
* free, its size, and a reference to the next block. | |
* | |
* Homework assignments: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using `x` flag with `re` shorthand | |
// Meta-chars like \d can be escaped using single slash. | |
// Plugin should specify `useRe` option. | |
re`/ | |
# A regular expression for date. | |
(?<year>\d{4})- # year part of a date | |
(?<month>\d{2})- # month part of a date |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using `x` flag with `RegExp`: | |
new RegExp(` | |
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using named capturing groups: | |
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; | |
const result = re.exec('2017-04-24'); | |
// NOTE: need runtime support to access `groups`, | |
// see `useRuntime` option: | |
console.log(result.groups.year); // 2017 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using `s` flag to match all chars | |
// including \n: | |
const allCharsRe = /.+/s; | |
console.log(allCharsRe.test('\n')); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^$/.test(''); // true | |
/$^/.test(''); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ | |
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const regexpTree = require('regexp-tree'); | |
// Using new syntax. | |
const originalRe = '/(?<all>.)\\k<all>/s'; | |
// For legacy engines. | |
const compatTranspiledRe = regexpTree | |
.compatTranspile(originalRe) | |
.toRegExp(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const regexpTree = require('regexp-tree'); | |
const re = regexpTree.generate({ | |
type: 'RegExp', | |
body: { | |
type: 'Char', | |
value: 'a', | |
kind: 'simple', | |
}, | |
flags: 'gi', |