source: trip-planner-front/node_modules/expand-brackets/lib/compilers.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[6a3a178]1'use strict';
2
3var posix = require('posix-character-classes');
4
5module.exports = function(brackets) {
6 brackets.compiler
7
8 /**
9 * Escaped characters
10 */
11
12 .set('escape', function(node) {
13 return this.emit('\\' + node.val.replace(/^\\/, ''), node);
14 })
15
16 /**
17 * Text
18 */
19
20 .set('text', function(node) {
21 return this.emit(node.val.replace(/([{}])/g, '\\$1'), node);
22 })
23
24 /**
25 * POSIX character classes
26 */
27
28 .set('posix', function(node) {
29 if (node.val === '[::]') {
30 return this.emit('\\[::\\]', node);
31 }
32
33 var val = posix[node.inner];
34 if (typeof val === 'undefined') {
35 val = '[' + node.inner + ']';
36 }
37 return this.emit(val, node);
38 })
39
40 /**
41 * Non-posix brackets
42 */
43
44 .set('bracket', function(node) {
45 return this.mapVisit(node.nodes);
46 })
47 .set('bracket.open', function(node) {
48 return this.emit(node.val, node);
49 })
50 .set('bracket.inner', function(node) {
51 var inner = node.val;
52
53 if (inner === '[' || inner === ']') {
54 return this.emit('\\' + node.val, node);
55 }
56 if (inner === '^]') {
57 return this.emit('^\\]', node);
58 }
59 if (inner === '^') {
60 return this.emit('^', node);
61 }
62
63 if (/-/.test(inner) && !/(\d-\d|\w-\w)/.test(inner)) {
64 inner = inner.split('-').join('\\-');
65 }
66
67 var isNegated = inner.charAt(0) === '^';
68 // add slashes to negated brackets, per spec
69 if (isNegated && inner.indexOf('/') === -1) {
70 inner += '/';
71 }
72 if (isNegated && inner.indexOf('.') === -1) {
73 inner += '.';
74 }
75
76 // don't unescape `0` (octal literal)
77 inner = inner.replace(/\\([1-9])/g, '$1');
78 return this.emit(inner, node);
79 })
80 .set('bracket.close', function(node) {
81 var val = node.val.replace(/^\\/, '');
82 if (node.parent.escaped === true) {
83 return this.emit('\\' + val, node);
84 }
85 return this.emit(val, node);
86 });
87};
Note: See TracBrowser for help on using the repository browser.