source: trip-planner-front/node_modules/ajv/dist/compile/codegen/code.js@ 84d0fbb

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

initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.regexpCode = exports.getProperty = exports.safeStringify = exports.stringify = exports.strConcat = exports.addCodeArg = exports.str = exports._ = exports.nil = exports._Code = exports.Name = exports.IDENTIFIER = exports._CodeOrName = void 0;
4class _CodeOrName {
5}
6exports._CodeOrName = _CodeOrName;
7exports.IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
8class Name extends _CodeOrName {
9 constructor(s) {
10 super();
11 if (!exports.IDENTIFIER.test(s))
12 throw new Error("CodeGen: name must be a valid identifier");
13 this.str = s;
14 }
15 toString() {
16 return this.str;
17 }
18 emptyStr() {
19 return false;
20 }
21 get names() {
22 return { [this.str]: 1 };
23 }
24}
25exports.Name = Name;
26class _Code extends _CodeOrName {
27 constructor(code) {
28 super();
29 this._items = typeof code === "string" ? [code] : code;
30 }
31 toString() {
32 return this.str;
33 }
34 emptyStr() {
35 if (this._items.length > 1)
36 return false;
37 const item = this._items[0];
38 return item === "" || item === '""';
39 }
40 get str() {
41 var _a;
42 return ((_a = this._str) !== null && _a !== void 0 ? _a : (this._str = this._items.reduce((s, c) => `${s}${c}`, "")));
43 }
44 get names() {
45 var _a;
46 return ((_a = this._names) !== null && _a !== void 0 ? _a : (this._names = this._items.reduce((names, c) => {
47 if (c instanceof Name)
48 names[c.str] = (names[c.str] || 0) + 1;
49 return names;
50 }, {})));
51 }
52}
53exports._Code = _Code;
54exports.nil = new _Code("");
55function _(strs, ...args) {
56 const code = [strs[0]];
57 let i = 0;
58 while (i < args.length) {
59 addCodeArg(code, args[i]);
60 code.push(strs[++i]);
61 }
62 return new _Code(code);
63}
64exports._ = _;
65const plus = new _Code("+");
66function str(strs, ...args) {
67 const expr = [safeStringify(strs[0])];
68 let i = 0;
69 while (i < args.length) {
70 expr.push(plus);
71 addCodeArg(expr, args[i]);
72 expr.push(plus, safeStringify(strs[++i]));
73 }
74 optimize(expr);
75 return new _Code(expr);
76}
77exports.str = str;
78function addCodeArg(code, arg) {
79 if (arg instanceof _Code)
80 code.push(...arg._items);
81 else if (arg instanceof Name)
82 code.push(arg);
83 else
84 code.push(interpolate(arg));
85}
86exports.addCodeArg = addCodeArg;
87function optimize(expr) {
88 let i = 1;
89 while (i < expr.length - 1) {
90 if (expr[i] === plus) {
91 const res = mergeExprItems(expr[i - 1], expr[i + 1]);
92 if (res !== undefined) {
93 expr.splice(i - 1, 3, res);
94 continue;
95 }
96 expr[i++] = "+";
97 }
98 i++;
99 }
100}
101function mergeExprItems(a, b) {
102 if (b === '""')
103 return a;
104 if (a === '""')
105 return b;
106 if (typeof a == "string") {
107 if (b instanceof Name || a[a.length - 1] !== '"')
108 return;
109 if (typeof b != "string")
110 return `${a.slice(0, -1)}${b}"`;
111 if (b[0] === '"')
112 return a.slice(0, -1) + b.slice(1);
113 return;
114 }
115 if (typeof b == "string" && b[0] === '"' && !(a instanceof Name))
116 return `"${a}${b.slice(1)}`;
117 return;
118}
119function strConcat(c1, c2) {
120 return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str `${c1}${c2}`;
121}
122exports.strConcat = strConcat;
123// TODO do not allow arrays here
124function interpolate(x) {
125 return typeof x == "number" || typeof x == "boolean" || x === null
126 ? x
127 : safeStringify(Array.isArray(x) ? x.join(",") : x);
128}
129function stringify(x) {
130 return new _Code(safeStringify(x));
131}
132exports.stringify = stringify;
133function safeStringify(x) {
134 return JSON.stringify(x)
135 .replace(/\u2028/g, "\\u2028")
136 .replace(/\u2029/g, "\\u2029");
137}
138exports.safeStringify = safeStringify;
139function getProperty(key) {
140 return typeof key == "string" && exports.IDENTIFIER.test(key) ? new _Code(`.${key}`) : _ `[${key}]`;
141}
142exports.getProperty = getProperty;
143function regexpCode(rx) {
144 return new _Code(rx.toString());
145}
146exports.regexpCode = regexpCode;
147//# sourceMappingURL=code.js.map
Note: See TracBrowser for help on using the repository browser.