Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. 'use strict';
  2. var ArrayProto = Array.prototype;
  3. var ObjProto = Object.prototype;
  4. var escapeMap = {
  5. '&': '&',
  6. '"': '"',
  7. '\'': ''',
  8. '<': '&lt;',
  9. '>': '&gt;'
  10. };
  11. var escapeRegex = /[&"'<>]/g;
  12. var _exports = module.exports = {};
  13. function hasOwnProp(obj, k) {
  14. return ObjProto.hasOwnProperty.call(obj, k);
  15. }
  16. _exports.hasOwnProp = hasOwnProp;
  17. function lookupEscape(ch) {
  18. return escapeMap[ch];
  19. }
  20. function _prettifyError(path, withInternals, err) {
  21. if (!err.Update) {
  22. // not one of ours, cast it
  23. err = new _exports.TemplateError(err);
  24. }
  25. err.Update(path); // Unless they marked the dev flag, show them a trace from here
  26. if (!withInternals) {
  27. var old = err;
  28. err = new Error(old.message);
  29. err.name = old.name;
  30. }
  31. return err;
  32. }
  33. _exports._prettifyError = _prettifyError;
  34. function TemplateError(message, lineno, colno) {
  35. var err;
  36. var cause;
  37. if (message instanceof Error) {
  38. cause = message;
  39. message = cause.name + ": " + cause.message;
  40. }
  41. if (Object.setPrototypeOf) {
  42. err = new Error(message);
  43. Object.setPrototypeOf(err, TemplateError.prototype);
  44. } else {
  45. err = this;
  46. Object.defineProperty(err, 'message', {
  47. enumerable: false,
  48. writable: true,
  49. value: message
  50. });
  51. }
  52. Object.defineProperty(err, 'name', {
  53. value: 'Template render error'
  54. });
  55. if (Error.captureStackTrace) {
  56. Error.captureStackTrace(err, this.constructor);
  57. }
  58. var getStack;
  59. if (cause) {
  60. var stackDescriptor = Object.getOwnPropertyDescriptor(cause, 'stack');
  61. getStack = stackDescriptor && (stackDescriptor.get || function () {
  62. return stackDescriptor.value;
  63. });
  64. if (!getStack) {
  65. getStack = function getStack() {
  66. return cause.stack;
  67. };
  68. }
  69. } else {
  70. var stack = new Error(message).stack;
  71. getStack = function getStack() {
  72. return stack;
  73. };
  74. }
  75. Object.defineProperty(err, 'stack', {
  76. get: function get() {
  77. return getStack.call(err);
  78. }
  79. });
  80. Object.defineProperty(err, 'cause', {
  81. value: cause
  82. });
  83. err.lineno = lineno;
  84. err.colno = colno;
  85. err.firstUpdate = true;
  86. err.Update = function Update(path) {
  87. var msg = '(' + (path || 'unknown path') + ')'; // only show lineno + colno next to path of template
  88. // where error occurred
  89. if (this.firstUpdate) {
  90. if (this.lineno && this.colno) {
  91. msg += " [Line " + this.lineno + ", Column " + this.colno + "]";
  92. } else if (this.lineno) {
  93. msg += " [Line " + this.lineno + "]";
  94. }
  95. }
  96. msg += '\n ';
  97. if (this.firstUpdate) {
  98. msg += ' ';
  99. }
  100. this.message = msg + (this.message || '');
  101. this.firstUpdate = false;
  102. return this;
  103. };
  104. return err;
  105. }
  106. if (Object.setPrototypeOf) {
  107. Object.setPrototypeOf(TemplateError.prototype, Error.prototype);
  108. } else {
  109. TemplateError.prototype = Object.create(Error.prototype, {
  110. constructor: {
  111. value: TemplateError
  112. }
  113. });
  114. }
  115. _exports.TemplateError = TemplateError;
  116. function escape(val) {
  117. return val.replace(escapeRegex, lookupEscape);
  118. }
  119. _exports.escape = escape;
  120. function isFunction(obj) {
  121. return ObjProto.toString.call(obj) === '[object Function]';
  122. }
  123. _exports.isFunction = isFunction;
  124. function isArray(obj) {
  125. return ObjProto.toString.call(obj) === '[object Array]';
  126. }
  127. _exports.isArray = isArray;
  128. function isString(obj) {
  129. return ObjProto.toString.call(obj) === '[object String]';
  130. }
  131. _exports.isString = isString;
  132. function isObject(obj) {
  133. return ObjProto.toString.call(obj) === '[object Object]';
  134. }
  135. _exports.isObject = isObject;
  136. /**
  137. * @param {string|number} attr
  138. * @returns {(string|number)[]}
  139. * @private
  140. */
  141. function _prepareAttributeParts(attr) {
  142. if (!attr) {
  143. return [];
  144. }
  145. if (typeof attr === 'string') {
  146. return attr.split('.');
  147. }
  148. return [attr];
  149. }
  150. /**
  151. * @param {string} attribute Attribute value. Dots allowed.
  152. * @returns {function(Object): *}
  153. */
  154. function getAttrGetter(attribute) {
  155. var parts = _prepareAttributeParts(attribute);
  156. return function attrGetter(item) {
  157. var _item = item;
  158. for (var i = 0; i < parts.length; i++) {
  159. var part = parts[i]; // If item is not an object, and we still got parts to handle, it means
  160. // that something goes wrong. Just roll out to undefined in that case.
  161. if (hasOwnProp(_item, part)) {
  162. _item = _item[part];
  163. } else {
  164. return undefined;
  165. }
  166. }
  167. return _item;
  168. };
  169. }
  170. _exports.getAttrGetter = getAttrGetter;
  171. function groupBy(obj, val, throwOnUndefined) {
  172. var result = {};
  173. var iterator = isFunction(val) ? val : getAttrGetter(val);
  174. for (var i = 0; i < obj.length; i++) {
  175. var value = obj[i];
  176. var key = iterator(value, i);
  177. if (key === undefined && throwOnUndefined === true) {
  178. throw new TypeError("groupby: attribute \"" + val + "\" resolved to undefined");
  179. }
  180. (result[key] || (result[key] = [])).push(value);
  181. }
  182. return result;
  183. }
  184. _exports.groupBy = groupBy;
  185. function toArray(obj) {
  186. return Array.prototype.slice.call(obj);
  187. }
  188. _exports.toArray = toArray;
  189. function without(array) {
  190. var result = [];
  191. if (!array) {
  192. return result;
  193. }
  194. var length = array.length;
  195. var contains = toArray(arguments).slice(1);
  196. var index = -1;
  197. while (++index < length) {
  198. if (indexOf(contains, array[index]) === -1) {
  199. result.push(array[index]);
  200. }
  201. }
  202. return result;
  203. }
  204. _exports.without = without;
  205. function repeat(char_, n) {
  206. var str = '';
  207. for (var i = 0; i < n; i++) {
  208. str += char_;
  209. }
  210. return str;
  211. }
  212. _exports.repeat = repeat;
  213. function each(obj, func, context) {
  214. if (obj == null) {
  215. return;
  216. }
  217. if (ArrayProto.forEach && obj.forEach === ArrayProto.forEach) {
  218. obj.forEach(func, context);
  219. } else if (obj.length === +obj.length) {
  220. for (var i = 0, l = obj.length; i < l; i++) {
  221. func.call(context, obj[i], i, obj);
  222. }
  223. }
  224. }
  225. _exports.each = each;
  226. function map(obj, func) {
  227. var results = [];
  228. if (obj == null) {
  229. return results;
  230. }
  231. if (ArrayProto.map && obj.map === ArrayProto.map) {
  232. return obj.map(func);
  233. }
  234. for (var i = 0; i < obj.length; i++) {
  235. results[results.length] = func(obj[i], i);
  236. }
  237. if (obj.length === +obj.length) {
  238. results.length = obj.length;
  239. }
  240. return results;
  241. }
  242. _exports.map = map;
  243. function asyncIter(arr, iter, cb) {
  244. var i = -1;
  245. function next() {
  246. i++;
  247. if (i < arr.length) {
  248. iter(arr[i], i, next, cb);
  249. } else {
  250. cb();
  251. }
  252. }
  253. next();
  254. }
  255. _exports.asyncIter = asyncIter;
  256. function asyncFor(obj, iter, cb) {
  257. var keys = keys_(obj || {});
  258. var len = keys.length;
  259. var i = -1;
  260. function next() {
  261. i++;
  262. var k = keys[i];
  263. if (i < len) {
  264. iter(k, obj[k], i, len, next);
  265. } else {
  266. cb();
  267. }
  268. }
  269. next();
  270. }
  271. _exports.asyncFor = asyncFor;
  272. function indexOf(arr, searchElement, fromIndex) {
  273. return Array.prototype.indexOf.call(arr || [], searchElement, fromIndex);
  274. }
  275. _exports.indexOf = indexOf;
  276. function keys_(obj) {
  277. /* eslint-disable no-restricted-syntax */
  278. var arr = [];
  279. for (var k in obj) {
  280. if (hasOwnProp(obj, k)) {
  281. arr.push(k);
  282. }
  283. }
  284. return arr;
  285. }
  286. _exports.keys = keys_;
  287. function _entries(obj) {
  288. return keys_(obj).map(function (k) {
  289. return [k, obj[k]];
  290. });
  291. }
  292. _exports._entries = _entries;
  293. function _values(obj) {
  294. return keys_(obj).map(function (k) {
  295. return obj[k];
  296. });
  297. }
  298. _exports._values = _values;
  299. function extend(obj1, obj2) {
  300. obj1 = obj1 || {};
  301. keys_(obj2).forEach(function (k) {
  302. obj1[k] = obj2[k];
  303. });
  304. return obj1;
  305. }
  306. _exports._assign = _exports.extend = extend;
  307. function inOperator(key, val) {
  308. if (isArray(val) || isString(val)) {
  309. return val.indexOf(key) !== -1;
  310. } else if (isObject(val)) {
  311. return key in val;
  312. }
  313. throw new Error('Cannot use "in" operator to search for "' + key + '" in unexpected types.');
  314. }
  315. _exports.inOperator = inOperator;