-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata-expand-resolver.js
More file actions
119 lines (113 loc) · 3.4 KB
/
data-expand-resolver.js
File metadata and controls
119 lines (113 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
/*eslint no-var: "off"*/
// noinspection ES6ConvertVarToLetConst
var _ = require('lodash');
/**
* @param {string} s
* @returns {Array}
* @private
*/
function testSplitExpandExpression(s) {
var ix = 0;
var paren = -1, charAt, ix1 = -1,
isLiteral = false,
lastSplitIndex = 0,
hasParen = false,
matches = [],
match = null;
while(ix<s.length) {
charAt=s.charAt(ix);
if ((charAt==='(') && !isLiteral) {
if (paren<0) {
match = [];
match[0] = s.substr(lastSplitIndex, ix-lastSplitIndex);
paren = 0;
}
if (ix1 === -1) { ix1 = ix; }
hasParen = true;
paren += 1;
}
else if ((charAt===')') && !isLiteral) {
if (paren>0) { paren -= 1; }
}
else if (charAt==='\'') {
isLiteral = !isLiteral;
}
else if ((charAt === ',') && (paren === -1)) {
if (match==null) {
matches.push([s.substr(lastSplitIndex, ix-lastSplitIndex)]);
}
lastSplitIndex = ix+1;
}
if ((ix === s.length - 1) && (paren === -1)) {
matches.push([s.substr(lastSplitIndex, ix-lastSplitIndex+1)]);
match = null;
}
else if (paren === 0) {
match = match || [ ];
match[1] = s.substr(ix1+1, ix-ix1-1);
matches.push(match);
paren = -1;
ix1 = -1;
}
ix += 1;
}
return matches;
}
/**
* @constructor
*/
function DataExpandResolver() {
//
}
/**
* Tests a string expression and returns an array of matched expandable entities
* @param {string} s
*/
DataExpandResolver.prototype.testExpandExpression = function(s) {
if (_.isNil(s)) {
return [];
}
var result = [], reOptions = /(;|^)(\$expand|\$filter|\$levels|\$orderby|\$groupby|\$select|\$top|\$skip|\$search|\$count)=(.*?)(;\$|$)/ig;
var matches = testSplitExpandExpression(s);
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
if (typeof match[1] === 'undefined') {
result.push({ name:match[0].replace(/^\s+|\s+$/,'') });
}
else {
var expand = { };
expand['name'] = match[0].replace(/^\s+|\s+$/,'');
reOptions.lastIndex = 0;
var params = { };
var expandOptions = match[1];
var matchOption = reOptions.exec(expandOptions);
while(matchOption) {
if (matchOption[3]) {
params[matchOption[2]] = matchOption[3];
reOptions.lastIndex = reOptions.lastIndex-2;
}
matchOption = reOptions.exec(expandOptions);
}
expand.options = params;
result.push(expand);
}
}
return result;
};
/**
* Tests a string expression and returns an array of matched expandable entities
* @param {string} s
*/
DataExpandResolver.prototype.test = function(s) {
return this.testExpandExpression(s);
}
if (typeof exports !== 'undefined')
{
module.exports = {
DataExpandResolver,
testExpandExpression: function(s) {
return DataExpandResolver.prototype.testExpandExpression(s);
}
};
}