CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
date.js250 linesDownload Raw Back to elements
1'use strict';2 3function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }4 5function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }6 7const color = require('kleur');8 9const Prompt = require('./prompt');10 11const _require = require('../util'),12      style = _require.style,13      clear = _require.clear,14      figures = _require.figures;15 16const _require2 = require('sisteransi'),17      erase = _require2.erase,18      cursor = _require2.cursor;19 20const _require3 = require('../dateparts'),21      DatePart = _require3.DatePart,22      Meridiem = _require3.Meridiem,23      Day = _require3.Day,24      Hours = _require3.Hours,25      Milliseconds = _require3.Milliseconds,26      Minutes = _require3.Minutes,27      Month = _require3.Month,28      Seconds = _require3.Seconds,29      Year = _require3.Year;30 31const regex = /\\(.)|"((?:\\["\\]|[^"])+)"|(D[Do]?|d{3,4}|d)|(M{1,4})|(YY(?:YY)?)|([aA])|([Hh]{1,2})|(m{1,2})|(s{1,2})|(S{1,4})|./g;32const regexGroups = {33  1: ({34    token35  }) => token.replace(/\\(.)/g, '$1'),36  2: opts => new Day(opts),37  // Day // TODO38  3: opts => new Month(opts),39  // Month40  4: opts => new Year(opts),41  // Year42  5: opts => new Meridiem(opts),43  // AM/PM // TODO (special)44  6: opts => new Hours(opts),45  // Hours46  7: opts => new Minutes(opts),47  // Minutes48  8: opts => new Seconds(opts),49  // Seconds50  9: opts => new Milliseconds(opts) // Fractional seconds51 52};53const dfltLocales = {54  months: 'January,February,March,April,May,June,July,August,September,October,November,December'.split(','),55  monthsShort: 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','),56  weekdays: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),57  weekdaysShort: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(',')58};59/**60 * DatePrompt Base Element61 * @param {Object} opts Options62 * @param {String} opts.message Message63 * @param {Number} [opts.initial] Index of default value64 * @param {String} [opts.mask] The format mask65 * @param {object} [opts.locales] The date locales66 * @param {String} [opts.error] The error message shown on invalid value67 * @param {Function} [opts.validate] Function to validate the submitted value68 * @param {Stream} [opts.stdin] The Readable stream to listen to69 * @param {Stream} [opts.stdout] The Writable stream to write readline data to70 */71 72class DatePrompt extends Prompt {73  constructor(opts = {}) {74    super(opts);75    this.msg = opts.message;76    this.cursor = 0;77    this.typed = '';78    this.locales = Object.assign(dfltLocales, opts.locales);79    this._date = opts.initial || new Date();80    this.errorMsg = opts.error || 'Please Enter A Valid Value';81 82    this.validator = opts.validate || (() => true);83 84    this.mask = opts.mask || 'YYYY-MM-DD HH:mm:ss';85    this.clear = clear('', this.out.columns);86    this.render();87  }88 89  get value() {90    return this.date;91  }92 93  get date() {94    return this._date;95  }96 97  set date(date) {98    if (date) this._date.setTime(date.getTime());99  }100 101  set mask(mask) {102    let result;103    this.parts = [];104 105    while (result = regex.exec(mask)) {106      let match = result.shift();107      let idx = result.findIndex(gr => gr != null);108      this.parts.push(idx in regexGroups ? regexGroups[idx]({109        token: result[idx] || match,110        date: this.date,111        parts: this.parts,112        locales: this.locales113      }) : result[idx] || match);114    }115 116    let parts = this.parts.reduce((arr, i) => {117      if (typeof i === 'string' && typeof arr[arr.length - 1] === 'string') arr[arr.length - 1] += i;else arr.push(i);118      return arr;119    }, []);120    this.parts.splice(0);121    this.parts.push(...parts);122    this.reset();123  }124 125  moveCursor(n) {126    this.typed = '';127    this.cursor = n;128    this.fire();129  }130 131  reset() {132    this.moveCursor(this.parts.findIndex(p => p instanceof DatePart));133    this.fire();134    this.render();135  }136 137  exit() {138    this.abort();139  }140 141  abort() {142    this.done = this.aborted = true;143    this.error = false;144    this.fire();145    this.render();146    this.out.write('\n');147    this.close();148  }149 150  validate() {151    var _this = this;152 153    return _asyncToGenerator(function* () {154      let valid = yield _this.validator(_this.value);155 156      if (typeof valid === 'string') {157        _this.errorMsg = valid;158        valid = false;159      }160 161      _this.error = !valid;162    })();163  }164 165  submit() {166    var _this2 = this;167 168    return _asyncToGenerator(function* () {169      yield _this2.validate();170 171      if (_this2.error) {172        _this2.color = 'red';173 174        _this2.fire();175 176        _this2.render();177 178        return;179      }180 181      _this2.done = true;182      _this2.aborted = false;183 184      _this2.fire();185 186      _this2.render();187 188      _this2.out.write('\n');189 190      _this2.close();191    })();192  }193 194  up() {195    this.typed = '';196    this.parts[this.cursor].up();197    this.render();198  }199 200  down() {201    this.typed = '';202    this.parts[this.cursor].down();203    this.render();204  }205 206  left() {207    let prev = this.parts[this.cursor].prev();208    if (prev == null) return this.bell();209    this.moveCursor(this.parts.indexOf(prev));210    this.render();211  }212 213  right() {214    let next = this.parts[this.cursor].next();215    if (next == null) return this.bell();216    this.moveCursor(this.parts.indexOf(next));217    this.render();218  }219 220  next() {221    let next = this.parts[this.cursor].next();222    this.moveCursor(next ? this.parts.indexOf(next) : this.parts.findIndex(part => part instanceof DatePart));223    this.render();224  }225 226  _(c) {227    if (/\d/.test(c)) {228      this.typed += c;229      this.parts[this.cursor].setTo(this.typed);230      this.render();231    }232  }233 234  render() {235    if (this.closed) return;236    if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText, this.out.columns));237    super.render(); // Print prompt238 239    this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(false), this.parts.reduce((arr, p, idx) => arr.concat(idx === this.cursor && !this.done ? color.cyan().underline(p.toString()) : p), []).join('')].join(' '); // Print error240 241    if (this.error) {242      this.outputText += this.errorMsg.split('\n').reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);243    }244 245    this.out.write(erase.line + cursor.to(0) + this.outputText);246  }247 248}249 250module.exports = DatePrompt;
basant307/AI_Governance_Project · CoolFace