Spaces:
Runtime error
Runtime error
File size: 6,987 Bytes
4a51346 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
'use strict';
const toString = Object.prototype.toString;
const colors = require('ansi-colors');
let called = false;
let fns = [];
const complements = {
'yellow': 'blue',
'cyan': 'red',
'green': 'magenta',
'black': 'white',
'blue': 'yellow',
'red': 'cyan',
'magenta': 'green',
'white': 'black'
};
exports.longest = (arr, prop) => {
return arr.reduce((a, v) => Math.max(a, prop ? v[prop].length : v.length), 0);
};
exports.hasColor = str => !!str && colors.hasColor(str);
const isObject = exports.isObject = val => {
return val !== null && typeof val === 'object' && !Array.isArray(val);
};
exports.nativeType = val => {
return toString.call(val).slice(8, -1).toLowerCase().replace(/\s/g, '');
};
exports.isAsyncFn = val => {
return exports.nativeType(val) === 'asyncfunction';
};
exports.isPrimitive = val => {
return val != null && typeof val !== 'object' && typeof val !== 'function';
};
exports.resolve = (context, value, ...rest) => {
if (typeof value === 'function') {
return value.call(context, ...rest);
}
return value;
};
exports.scrollDown = (choices = []) => [...choices.slice(1), choices[0]];
exports.scrollUp = (choices = []) => [choices.pop(), ...choices];
exports.reorder = (arr = []) => {
let res = arr.slice();
res.sort((a, b) => {
if (a.index > b.index) return 1;
if (a.index < b.index) return -1;
return 0;
});
return res;
};
exports.swap = (arr, index, pos) => {
let len = arr.length;
let idx = pos === len ? 0 : pos < 0 ? len - 1 : pos;
let choice = arr[index];
arr[index] = arr[idx];
arr[idx] = choice;
};
exports.width = (stream, fallback = 80) => {
let columns = (stream && stream.columns) ? stream.columns : fallback;
if (stream && typeof stream.getWindowSize === 'function') {
columns = stream.getWindowSize()[0];
}
if (process.platform === 'win32') {
return columns - 1;
}
return columns;
};
exports.height = (stream, fallback = 20) => {
let rows = (stream && stream.rows) ? stream.rows : fallback;
if (stream && typeof stream.getWindowSize === 'function') {
rows = stream.getWindowSize()[1];
}
return rows;
};
exports.wordWrap = (str, options = {}) => {
if (!str) return str;
if (typeof options === 'number') {
options = { width: options };
}
let { indent = '', newline = ('\n' + indent), width = 80 } = options;
let spaces = (newline + indent).match(/[^\S\n]/g) || [];
width -= spaces.length;
let source = `.{1,${width}}([\\s\\u200B]+|$)|[^\\s\\u200B]+?([\\s\\u200B]+|$)`;
let output = str.trim();
let regex = new RegExp(source, 'g');
let lines = output.match(regex) || [];
lines = lines.map(line => line.replace(/\n$/, ''));
if (options.padEnd) lines = lines.map(line => line.padEnd(width, ' '));
if (options.padStart) lines = lines.map(line => line.padStart(width, ' '));
return indent + lines.join(newline);
};
exports.unmute = color => {
let name = color.stack.find(n => colors.keys.color.includes(n));
if (name) {
return colors[name];
}
let bg = color.stack.find(n => n.slice(2) === 'bg');
if (bg) {
return colors[name.slice(2)];
}
return str => str;
};
exports.pascal = str => str ? str[0].toUpperCase() + str.slice(1) : '';
exports.inverse = color => {
if (!color || !color.stack) return color;
let name = color.stack.find(n => colors.keys.color.includes(n));
if (name) {
let col = colors['bg' + exports.pascal(name)];
return col ? col.black : color;
}
let bg = color.stack.find(n => n.slice(0, 2) === 'bg');
if (bg) {
return colors[bg.slice(2).toLowerCase()] || color;
}
return colors.none;
};
exports.complement = color => {
if (!color || !color.stack) return color;
let name = color.stack.find(n => colors.keys.color.includes(n));
let bg = color.stack.find(n => n.slice(0, 2) === 'bg');
if (name && !bg) {
return colors[complements[name] || name];
}
if (bg) {
let lower = bg.slice(2).toLowerCase();
let comp = complements[lower];
if (!comp) return color;
return colors['bg' + exports.pascal(comp)] || color;
}
return colors.none;
};
exports.meridiem = date => {
let hours = date.getHours();
let minutes = date.getMinutes();
let ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
let hrs = hours === 0 ? 12 : hours;
let min = minutes < 10 ? '0' + minutes : minutes;
return hrs + ':' + min + ' ' + ampm;
};
/**
* Set a value on the given object.
* @param {Object} obj
* @param {String} prop
* @param {any} value
*/
exports.set = (obj = {}, prop = '', val) => {
return prop.split('.').reduce((acc, k, i, arr) => {
let value = arr.length - 1 > i ? (acc[k] || {}) : val;
if (!exports.isObject(value) && i < arr.length - 1) value = {};
return (acc[k] = value);
}, obj);
};
/**
* Get a value from the given object.
* @param {Object} obj
* @param {String} prop
*/
exports.get = (obj = {}, prop = '', fallback) => {
let value = obj[prop] == null
? prop.split('.').reduce((acc, k) => acc && acc[k], obj)
: obj[prop];
return value == null ? fallback : value;
};
exports.mixin = (target, b) => {
if (!isObject(target)) return b;
if (!isObject(b)) return target;
for (let key of Object.keys(b)) {
let desc = Object.getOwnPropertyDescriptor(b, key);
if (desc.hasOwnProperty('value')) {
if (target.hasOwnProperty(key) && isObject(desc.value)) {
let existing = Object.getOwnPropertyDescriptor(target, key);
if (isObject(existing.value)) {
target[key] = exports.merge({}, target[key], b[key]);
} else {
Reflect.defineProperty(target, key, desc);
}
} else {
Reflect.defineProperty(target, key, desc);
}
} else {
Reflect.defineProperty(target, key, desc);
}
}
return target;
};
exports.merge = (...args) => {
let target = {};
for (let ele of args) exports.mixin(target, ele);
return target;
};
exports.mixinEmitter = (obj, emitter) => {
let proto = emitter.constructor.prototype;
for (let key of Object.keys(proto)) {
let val = proto[key];
if (typeof val === 'function') {
exports.define(obj, key, val.bind(emitter));
} else {
exports.define(obj, key, val);
}
}
};
exports.onExit = callback => {
const onExit = (quit, code) => {
if (called) return;
called = true;
fns.forEach(fn => fn());
if (quit === true) {
process.exit(128 + code);
}
};
if (fns.length === 0) {
process.once('SIGTERM', onExit.bind(null, true, 15));
process.once('SIGINT', onExit.bind(null, true, 2));
process.once('exit', onExit);
}
fns.push(callback);
};
exports.define = (obj, key, value) => {
Reflect.defineProperty(obj, key, { value });
};
exports.defineExport = (obj, key, fn) => {
let custom;
Reflect.defineProperty(obj, key, {
enumerable: true,
configurable: true,
set(val) {
custom = val;
},
get() {
return custom ? custom() : fn();
}
});
};
|