UNPKG

7.54 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const http = require("http");
4const Observable_1 = require("rxjs/Observable");
5const comparator_enum_1 = require("./models/comparator.enum");
6exports.httpGet = (endpoint) => {
7 return Observable_1.Observable.create((observer) => {
8 http.get(endpoint, (result) => {
9 result.setEncoding("utf8");
10 let rawData = "";
11 result.on("data", (chunk) => rawData += chunk);
12 result.on("end", () => {
13 const parsedData = JSON.parse(rawData);
14 observer.next(parsedData);
15 });
16 })
17 .on("error", function (e) {
18 debugger;
19 observer.error(e);
20 });
21 });
22};
23exports.compare = (value, condition, param) => {
24 switch (condition) {
25 case comparator_enum_1.Comparator.equal:
26 return value == param;
27 case comparator_enum_1.Comparator.not_equal:
28 return value != param;
29 case comparator_enum_1.Comparator.greater_than:
30 return value > param;
31 case comparator_enum_1.Comparator.less_than:
32 return value < param;
33 case comparator_enum_1.Comparator.contain:
34 return value.toString().indexOf(param.toString()) > -1;
35 }
36};
37exports.getValue = function (objOrArr, pathOrIndex) {
38 if (arguments.length == 1) {
39 pathOrIndex = objOrArr;
40 objOrArr = window;
41 }
42 if (!pathOrIndex)
43 debugger;
44 let tempobjOrArr;
45 if (this.is.array(objOrArr))
46 return objOrArr[pathOrIndex];
47 else {
48 tempobjOrArr = objOrArr;
49 const routes = pathOrIndex.split(".");
50 for (let i = 0, route; route = routes[i]; i++) {
51 if (tempobjOrArr[route] === undefined || tempobjOrArr[route] === null) {
52 return;
53 }
54 if (this.is.array(tempobjOrArr[route])) {
55 const res = [];
56 const partialRoutes = routes.splice(i + 1);
57 for (let j = 0, item; item = tempobjOrArr[route][j]; j++)
58 res[j] = this.getValue(item, partialRoutes.join("."));
59 return res;
60 }
61 else
62 tempobjOrArr = tempobjOrArr[route];
63 }
64 }
65 return tempobjOrArr;
66};
67exports.is = (function (_) {
68 const is = function (node, selector) {
69 if (node.matches)
70 return node.matches(selector);
71 const nodes = this.argToArray(node.parentNode.querySelectorAll(selector));
72 return (nodes.indexOf(node) > -1) ? true : false;
73 };
74 is.object = function (_var) {
75 return Object.prototype.toString.call(_var) === "[object Object]";
76 };
77 is.nodeList = function (obj) {
78 if (_.is.not.ie())
79 return Object.prototype.toString.call(obj) === "[object NodeList]";
80 else
81 return (obj.length !== undefined
82 && obj.push === undefined && (obj.length > 0 ? obj[0].tagName !== undefined : true));
83 };
84 is.element = function (obj) {
85 return Object.prototype.toString.call(obj).search("Element") > -1;
86 };
87 is.HTMLCollection = function (obj) {
88 return Object.prototype.toString.call(obj) === "[object HTMLCollection]";
89 };
90 is.array = function (_var) {
91 return Object.prototype.toString.call(_var) === "[object Array]";
92 };
93 is.number = function (_var) {
94 return Object.prototype.toString.call(_var) === "[object Number]";
95 };
96 is["function"] = function (_var) {
97 return Object.prototype.toString.call(_var) === "[object Function]";
98 };
99 is.string = function (_var) {
100 return (Object.prototype.toString.call(_var) === "[object String]");
101 };
102 is.undefined = function (_var) {
103 return Object.prototype.toString.call(_var) === "[object Undefined]";
104 };
105 is.event = function (_var) {
106 return Object.prototype.toString.call(_var).toLowerCase().search("event") > -1;
107 };
108 is.defined = function (_var) {
109 return _var !== undefined && _var !== null && _var !== "";
110 };
111 is.json = function () { };
112 is.error = function () { };
113 is.startWith = function (str, prefix) {
114 return str.indexOf(prefix) === 0;
115 };
116 is.endWith = function (str) { };
117 is.value = function (_var) {
118 return (_var) ? true : false;
119 };
120 is.empty = function (o) {
121 if (_.is.object(0))
122 for (const i in o)
123 if (o.hasOwnProperty(i))
124 return false;
125 if (_.is.array(o))
126 return o.length === 0;
127 return true;
128 };
129 is.truthy = function () { };
130 is.scalar = function (_var) {
131 return is.defined(_var) && is.not.array(_var) && is.not.object(_var) && is.not["function"](_var);
132 };
133 is.prototypeProp = function (obj, prop) {
134 return (obj[prop] && !obj.hasOwnProperty(prop));
135 };
136 is.equal = function (fv, sv) {
137 return (JSON.stringify(fv) == JSON.stringify(sv)) ? true : false;
138 };
139 is.equalText = function (fv, sv) {
140 return (fv.toLowerCase(fv) === sv.toLowerCase(sv)) ? true : false;
141 };
142 is.closet = function (fo, so) {
143 return _.is.equal(_.partial(fo, _.report.skeleton(so)), so);
144 };
145 is.contain = function (str, searchStr) {
146 const reg = (_.is.regex(searchStr)) ? searchStr : new RegExp(searchStr, "g");
147 return str.match(reg) && str.match(reg).length > 0;
148 };
149 is.regex = function (r) {
150 return r.constructor.name === "RegExp";
151 };
152 is.same = function (fv, sv) {
153 return (fv.isEqualNode) ? fv.isEqualNode(sv) : fv === sv;
154 };
155 is.persianLeapYear = function (year) {
156 return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682;
157 };
158 is.georgianLeapYear = function (year) {
159 return ((year % 4) == 0) &&
160 (!(((year % 100) == 0) && ((year % 400) != 0)));
161 };
162 const not = {};
163 let i;
164 for (i in is)
165 (function (i) {
166 if (is.hasOwnProperty(i))
167 not[i] = function (a, b, c) {
168 return !is[i](a, b, c);
169 };
170 })(i);
171 is.not = not;
172 const all = {};
173 for (i in is)
174 (function (i) {
175 if (is.hasOwnProperty(i))
176 all[i] = function (o) {
177 };
178 })(i);
179 is.all = all;
180 const any = {};
181 for (const j in is)
182 (function (j) {
183 if (is.hasOwnProperty(j))
184 any[j] = function (o) {
185 };
186 })(j);
187 is.any = any;
188 return is;
189})(this);
190exports.compileString = (str, varDef) => {
191 const regex = /\{\{/;
192 let result = [];
193 const varStartIdx = str.search(regex);
194 if (varStartIdx > -1) {
195 result[result.length] = str.substring(0, varStartIdx);
196 str = str.substring(varStartIdx);
197 const varEndIdx = str.search(/\}\}/);
198 const varKey = str.substring(2, varEndIdx).trim();
199 const varValue = exports.getValue(varDef, varKey);
200 result[result.length] = varValue;
201 str = str.substring(varEndIdx + 2);
202 result = result.concat(this.compileString(str, varDef));
203 }
204 else {
205 result[result.length] = str;
206 }
207 return result.join("");
208};
209//# sourceMappingURL=utility.js.map
\No newline at end of file