@rhinostone/swig-core 2.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Lexer token type enum — the contract between a flavor-specific lexer
3
+ * and the shared TokenParser in @rhinostone/swig-core.
4
+ *
5
+ * Every frontend (native Swig, future Twig / Jinja2 / Django) supplies
6
+ * its own lexer with per-flavor regex rules. The TokenParser dispatches
7
+ * on these numeric IDs, so the IDs themselves must be stable across
8
+ * flavors. Flavor-specific extensions (e.g. Twig's `~` concatenation,
9
+ * `??` null-coalescing) may add new IDs above the reserved range, but
10
+ * must not re-use existing ones.
11
+ *
12
+ * Kept as a standalone module (rather than folded into the lexer) so
13
+ * that TokenParser can consume the enum without reaching into any
14
+ * frontend-specific code.
15
+ *
16
+ * @readonly
17
+ * @enum {number}
18
+ */
19
+ module.exports = {
20
+ /** Whitespace */
21
+ WHITESPACE: 0,
22
+ /** Plain string */
23
+ STRING: 1,
24
+ /** Variable filter */
25
+ FILTER: 2,
26
+ /** Empty variable filter */
27
+ FILTEREMPTY: 3,
28
+ /** Function */
29
+ FUNCTION: 4,
30
+ /** Function with no arguments */
31
+ FUNCTIONEMPTY: 5,
32
+ /** Open parenthesis */
33
+ PARENOPEN: 6,
34
+ /** Close parenthesis */
35
+ PARENCLOSE: 7,
36
+ /** Comma */
37
+ COMMA: 8,
38
+ /** Variable */
39
+ VAR: 9,
40
+ /** Number */
41
+ NUMBER: 10,
42
+ /** Math operator */
43
+ OPERATOR: 11,
44
+ /** Open square bracket */
45
+ BRACKETOPEN: 12,
46
+ /** Close square bracket */
47
+ BRACKETCLOSE: 13,
48
+ /** Key on an object using dot-notation */
49
+ DOTKEY: 14,
50
+ /** Start of an array */
51
+ ARRAYOPEN: 15,
52
+ /** End of an array
53
+ * Currently unused
54
+ ARRAYCLOSE: 16, */
55
+ /** Open curly brace */
56
+ CURLYOPEN: 17,
57
+ /** Close curly brace */
58
+ CURLYCLOSE: 18,
59
+ /** Colon (:) */
60
+ COLON: 19,
61
+ /** JavaScript-valid comparator */
62
+ COMPARATOR: 20,
63
+ /** Boolean logic */
64
+ LOGIC: 21,
65
+ /** Boolean logic "not" */
66
+ NOT: 22,
67
+ /** true or false */
68
+ BOOL: 23,
69
+ /** Variable assignment */
70
+ ASSIGNMENT: 24,
71
+ /** Start of a method */
72
+ METHODOPEN: 25,
73
+ /** End of a method
74
+ * Currently unused
75
+ METHODEND: 26, */
76
+ /** Unknown type */
77
+ UNKNOWN: 100
78
+ };
package/lib/utils.js ADDED
@@ -0,0 +1,184 @@
1
+ var isArray;
2
+
3
+ /**
4
+ * Strip leading and trailing whitespace from a string.
5
+ * @param {string} input
6
+ * @return {string} Stripped input.
7
+ */
8
+ exports.strip = function (input) {
9
+ return input.replace(/^\s+|\s+$/g, '');
10
+ };
11
+
12
+ /**
13
+ * Test if a string starts with a given prefix.
14
+ * @param {string} str String to test against.
15
+ * @param {string} prefix Prefix to check for.
16
+ * @return {boolean}
17
+ */
18
+ exports.startsWith = function (str, prefix) {
19
+ return str.indexOf(prefix) === 0;
20
+ };
21
+
22
+ /**
23
+ * Test if a string ends with a given suffix.
24
+ * @param {string} str String to test against.
25
+ * @param {string} suffix Suffix to check for.
26
+ * @return {boolean}
27
+ */
28
+ exports.endsWith = function (str, suffix) {
29
+ return str.indexOf(suffix, str.length - suffix.length) !== -1;
30
+ };
31
+
32
+ /**
33
+ * Iterate over an array or object.
34
+ * @param {array|object} obj Enumerable object.
35
+ * @param {Function} fn Callback function executed for each item.
36
+ * @return {array|object} The original input object.
37
+ */
38
+ exports.each = function (obj, fn) {
39
+ var i, l;
40
+
41
+ if (isArray(obj)) {
42
+ i = 0;
43
+ l = obj.length;
44
+ for (i; i < l; i += 1) {
45
+ if (fn(obj[i], i, obj) === false) {
46
+ break;
47
+ }
48
+ }
49
+ } else {
50
+ for (i in obj) {
51
+ if (obj.hasOwnProperty(i)) {
52
+ if (fn(obj[i], i, obj) === false) {
53
+ break;
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ return obj;
60
+ };
61
+
62
+ /**
63
+ * Test if an object is an Array.
64
+ * @param {object} obj
65
+ * @return {boolean}
66
+ */
67
+ exports.isArray = isArray = (Array.hasOwnProperty('isArray')) ? Array.isArray : function (obj) {
68
+ return (obj) ? (typeof obj === 'object' && Object.prototype.toString.call(obj).indexOf() !== -1) : false;
69
+ };
70
+
71
+ /**
72
+ * Test if an item in an enumerable matches your conditions.
73
+ * @param {array|object} obj Enumerable object.
74
+ * @param {Function} fn Executed for each item. Return true if your condition is met.
75
+ * @return {boolean}
76
+ */
77
+ exports.some = function (obj, fn) {
78
+ var i = 0,
79
+ result,
80
+ l;
81
+ if (isArray(obj)) {
82
+ l = obj.length;
83
+
84
+ for (i; i < l; i += 1) {
85
+ result = fn(obj[i], i, obj);
86
+ if (result) {
87
+ break;
88
+ }
89
+ }
90
+ } else {
91
+ exports.each(obj, function (value, index) {
92
+ result = fn(value, index, obj);
93
+ return !(result);
94
+ });
95
+ }
96
+ return !!result;
97
+ };
98
+
99
+ /**
100
+ * Return a new enumerable, mapped by a given iteration function.
101
+ * @param {object} obj Enumerable object.
102
+ * @param {Function} fn Executed for each item. Return the item to replace the original item with.
103
+ * @return {object} New mapped object.
104
+ */
105
+ exports.map = function (obj, fn) {
106
+ var i = 0,
107
+ result = [],
108
+ l;
109
+
110
+ if (isArray(obj)) {
111
+ l = obj.length;
112
+ for (i; i < l; i += 1) {
113
+ result[i] = fn(obj[i], i);
114
+ }
115
+ } else {
116
+ for (i in obj) {
117
+ if (obj.hasOwnProperty(i)) {
118
+ result[i] = fn(obj[i], i);
119
+ }
120
+ }
121
+ }
122
+ return result;
123
+ };
124
+
125
+ /**
126
+ * Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.
127
+ * @param {...object} arguments
128
+ * @return {object}
129
+ */
130
+ exports.extend = function () {
131
+ var args = arguments,
132
+ target = args[0],
133
+ objs = (args.length > 1) ? Array.prototype.slice.call(args, 1) : [],
134
+ i = 0,
135
+ l = objs.length,
136
+ key,
137
+ obj;
138
+
139
+ for (i; i < l; i += 1) {
140
+ obj = objs[i] || {};
141
+ for (key in obj) {
142
+ if (obj.hasOwnProperty(key)) {
143
+ target[key] = obj[key];
144
+ }
145
+ }
146
+ }
147
+ return target;
148
+ };
149
+
150
+ /**
151
+ * Get all of the keys on an object.
152
+ * @param {object} obj
153
+ * @return {array}
154
+ */
155
+ exports.keys = function (obj) {
156
+ if (!obj) {
157
+ return [];
158
+ }
159
+
160
+ if (Object.keys) {
161
+ return Object.keys(obj);
162
+ }
163
+
164
+ return exports.map(obj, function (v, k) {
165
+ return k;
166
+ });
167
+ };
168
+
169
+ /**
170
+ * Throw an error with possible line number and source file.
171
+ * @param {string} message Error message
172
+ * @param {number} [line] Line number in template.
173
+ * @param {string} [file] Template file the error occured in.
174
+ * @throws {Error} No seriously, the point is to throw an error.
175
+ */
176
+ exports.throwError = function (message, line, file) {
177
+ if (line) {
178
+ message += ' on line ' + line;
179
+ }
180
+ if (file) {
181
+ message += ' in file ' + file;
182
+ }
183
+ throw new Error(message + '.');
184
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@rhinostone/swig-core",
3
+ "version": "2.0.0-alpha.3",
4
+ "description": "Shared IR, backend, and runtime for the @rhinostone/swig family of template engines. First publish at 2.0.0-alpha.3 — see @rhinostone/swig #T14 (Phase 1 carve).",
5
+ "keywords": [
6
+ "template",
7
+ "templating",
8
+ "ir",
9
+ "swig",
10
+ "swig-core"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/gina-io/swig.git",
15
+ "directory": "packages/swig-core"
16
+ },
17
+ "author": "Rhinostone <contact@gina.io>",
18
+ "license": "MIT",
19
+ "main": "lib/index.js",
20
+ "engines": {
21
+ "node": ">=12"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ }
26
+ }