UNPKG

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