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.

index.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. function toArr(any) {
  2. return any == null ? [] : Array.isArray(any) ? any : [any];
  3. }
  4. function toVal(out, key, val, opts) {
  5. var x, old=out[key], nxt=(
  6. !!~opts.string.indexOf(key) ? (val == null || val === true ? '' : String(val))
  7. : typeof val === 'boolean' ? val
  8. : !!~opts.boolean.indexOf(key) ? (val === 'false' ? false : val === 'true' || (out._.push((x = +val,x * 0 === 0) ? x : val),!!val))
  9. : (x = +val,x * 0 === 0) ? x : val
  10. );
  11. out[key] = old == null ? nxt : (Array.isArray(old) ? old.concat(nxt) : [old, nxt]);
  12. }
  13. module.exports = function (args, opts) {
  14. args = args || [];
  15. opts = opts || {};
  16. var k, arr, arg, name, val, out={ _:[] };
  17. var i=0, j=0, idx=0, len=args.length;
  18. const alibi = opts.alias !== void 0;
  19. const strict = opts.unknown !== void 0;
  20. const defaults = opts.default !== void 0;
  21. opts.alias = opts.alias || {};
  22. opts.string = toArr(opts.string);
  23. opts.boolean = toArr(opts.boolean);
  24. if (alibi) {
  25. for (k in opts.alias) {
  26. arr = opts.alias[k] = toArr(opts.alias[k]);
  27. for (i=0; i < arr.length; i++) {
  28. (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
  29. }
  30. }
  31. }
  32. for (i=opts.boolean.length; i-- > 0;) {
  33. arr = opts.alias[opts.boolean[i]] || [];
  34. for (j=arr.length; j-- > 0;) opts.boolean.push(arr[j]);
  35. }
  36. for (i=opts.string.length; i-- > 0;) {
  37. arr = opts.alias[opts.string[i]] || [];
  38. for (j=arr.length; j-- > 0;) opts.string.push(arr[j]);
  39. }
  40. if (defaults) {
  41. for (k in opts.default) {
  42. name = typeof opts.default[k];
  43. arr = opts.alias[k] = opts.alias[k] || [];
  44. if (opts[name] !== void 0) {
  45. opts[name].push(k);
  46. for (i=0; i < arr.length; i++) {
  47. opts[name].push(arr[i]);
  48. }
  49. }
  50. }
  51. }
  52. const keys = strict ? Object.keys(opts.alias) : [];
  53. for (i=0; i < len; i++) {
  54. arg = args[i];
  55. if (arg === '--') {
  56. out._ = out._.concat(args.slice(++i));
  57. break;
  58. }
  59. for (j=0; j < arg.length; j++) {
  60. if (arg.charCodeAt(j) !== 45) break; // "-"
  61. }
  62. if (j === 0) {
  63. out._.push(arg);
  64. } else if (arg.substring(j, j + 3) === 'no-') {
  65. name = arg.substring(j + 3);
  66. if (strict && !~keys.indexOf(name)) {
  67. return opts.unknown(arg);
  68. }
  69. out[name] = false;
  70. } else {
  71. for (idx=j+1; idx < arg.length; idx++) {
  72. if (arg.charCodeAt(idx) === 61) break; // "="
  73. }
  74. name = arg.substring(j, idx);
  75. val = arg.substring(++idx) || (i+1 === len || (''+args[i+1]).charCodeAt(0) === 45 || args[++i]);
  76. arr = (j === 2 ? [name] : name);
  77. for (idx=0; idx < arr.length; idx++) {
  78. name = arr[idx];
  79. if (strict && !~keys.indexOf(name)) return opts.unknown('-'.repeat(j) + name);
  80. toVal(out, name, (idx + 1 < arr.length) || val, opts);
  81. }
  82. }
  83. }
  84. if (defaults) {
  85. for (k in opts.default) {
  86. if (out[k] === void 0) {
  87. out[k] = opts.default[k];
  88. }
  89. }
  90. }
  91. if (alibi) {
  92. for (k in out) {
  93. arr = opts.alias[k] || [];
  94. while (arr.length > 0) {
  95. out[arr.shift()] = out[k];
  96. }
  97. }
  98. }
  99. return out;
  100. }