New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@spare/util

Package Overview
Dependencies
Maintainers
1
Versions
207
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@spare/util - npm Package Compare versions

Comparing version 0.0.6 to 0.1.0

1224

dist/index.cjs.js

@@ -5,7 +5,2 @@ 'use strict';

var lange = require('@spare/lange');
var typen = require('typen');
var matrix = require('@vect/matrix');
var vector = require('@vect/vector');
const noop = () => {};

@@ -52,30 +47,2 @@ /**

const {
isNumeric
} = typen.Num;
const lpad = (tx, pd, ansi = false, fill) => ansi && lange.hasAnsi(tx) ? tx.padStart(tx.length + pd - lange.lange(tx), fill) : tx.padStart(pd, fill);
const rpad = (tx, pd, ansi = false, fill) => ansi && lange.hasAnsi(tx) ? tx.padEnd(tx.length + pd - lange.lange(tx), fill) : tx.padEnd(pd, fill);
const npad = (tx, ref, pd, ansi = false, fill) => isNumeric(ref) ? lpad(tx, pd, ansi, fill) : rpad(tx, pd, ansi, fill);
// export const isVisual = visual =>
const defaultPreset = {
max: '#74FF03',
min: '#FF5252',
na: '#A27767'
};
const isVisual = visual => {
if (!visual) return undefined;
if (typeof visual !== typen.OBJ) return defaultPreset;
for (let k in defaultPreset) if (!(k in visual)) visual[k] = defaultPreset[k];
return visual;
};
const readCrop = ({
head,
tail
}) => [head, tail];
const rn = '\r\n';

@@ -95,1178 +62,2 @@ const tb = ' ';

/**
*
* @param {Array<?string>} arr
* @param ansi
*/
const maxLen = (arr, ansi = false) => ansi ? Math.max(...arr.map(x => x ? lange.lange(x) : 0)) : Math.max(...arr.map(x => {
var _ref;
return (_ref = x === null || x === void 0 ? void 0 : x.length) !== null && _ref !== void 0 ? _ref : 0;
}));
const intDigits = num => (num = ~~Math.log10(Math.abs(num)), ++num);
const indexMaxLen = (arr, base = 0) => intDigits(arr.length + base);
/**
*
* @param {string[]} text
* @param {*[]} [raw]
* @param {function[]} [dye]
* @param {number[]|number} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const formatVector = ({
text,
raw,
dye,
pad,
ansi
} = {}) => text.map((tx, i) => {
var _npad;
return _npad = npad(tx, raw[i], pad[i], ansi), dye[i](_npad);
});
/**
*
* @param {string[]} text
* @param {*[]} raw
* @param {number[]} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const padVector = ({
text,
raw,
pad,
ansi
}) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi));
/**
*
* @param {string[]} text
* @param {*[]} [raw]
* @param {function[]} [dye]
* @param {number[]|number} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const vecPalPad = (text, raw, dye, pad, ansi) => text.map((tx, i) => {
var _npad;
return _npad = npad(tx, raw[i], pad[i], ansi), dye[i](_npad);
});
/**
*
* @param {string[]} text
* @param {*[]} raw
* @param {number[]} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const vecPad = (text, raw, pad, ansi) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi));
/**
*
* @param {string[][]} text
* @param {*[][]} raw
* @param {function[][]} dye
* @param {number[]} pad
* @param {boolean} ansi
* @returns {string[][]}
*/
const formatMatrix = ({
text,
raw,
dye,
pad,
ansi
}) => {
return dye ? text.map((row, i) => formatVector({
text: row,
raw: raw[i],
dye: dye[i],
pad,
ansi
})) : text.map((row, i) => padVector({
text: row,
raw: raw[i],
pad,
ansi
}));
};
/**
*
* @param {*} x
* @return {string}
*/
const totx$1 = x => `${x}`;
const isTab$1 = c => c === '\t' || c === ' ';
const tabify$1 = tx => {
var _tx;
const i = (_tx = tx, deNaTab$1(_tx));
return endsBracs$1(tx) ? tx : `${tx.substring(0, i)}[${tx.substring(i)}]`;
};
const deNaTab$1 = tx => {
let i = 0;
for (let {
length
} = tx; i < length; i++) if (!isTab$1(tx.charAt(i))) return i;
return i;
};
const endsBracs$1 = tx => tx.endsWith(')') || tx.endsWith(']');
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Callable extends Function {
constructor(f) {
super();
Reflect.setPrototypeOf(f, new.target.prototype);
return f;
}
}
class Zu {
/**
* Generate a random integer between [min, max].
* Both min & max are inclusive.
* @param {Number} min Int
* @param {Number} max Int
* @returns {Number} Int
* @deprecated Prefer Roulett.between in npm package Roulett
*/
static randBetween(min, max) {
return ~~(Math.random() * (max - min + 1)) + min;
}
/**
* Generate a random integer between [min, max).
* Notice: min is inclusive & max is exclusive.
* @param {Number} min Int
* @param {Number} max(exclusive) Int
* @returns {Number} Int
* @deprecated Prefer Roulett.rand in npm package Roulett
*/
static rand(min, max) {
return ~~(Math.random() * (max - min)) + min;
}
static almostEquals(x, y, epsilon) {
return Math.abs(x - y) < epsilon;
}
static almostInt(x, epsilon) {
// let rounded = Math.round(x)
// return rounded - epsilon < x && rounded + epsilon > x
return Math.abs(x - Math.round(x)) < epsilon;
}
/**
*
* @param {number} x
* @returns {number}
*/
static intExponent(x) {
return ~~Math.log10(x);
}
/**
*
* @param {number} x
* @returns {number}
*/
static round(x) {
return x + (x > 0 ? 0.5 : -0.5) << 0;
}
} // ? x => Math.log10(x)
/**
*
* @param {number} n
* @param {number} h
* @param {number} a
* @param {number} l
* @returns {number}
*/
const hf = (n, h, a, l) => {
const k = (n + h / 30) % 12;
return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
};
const {
round
} = Zu; // export const round = x => ~~x
class Hsl {
/**
*
* @param {number} h
* @param {number} s
* @param {number} l
* @returns {number[]}
*/
static toRgb([h, s, l]) {
s /= 100;
l /= 100;
const a = s * Math.min(l, 1 - l),
r = hf(0, h, a, l),
g = hf(8, h, a, l),
b = hf(4, h, a, l);
return [round(r * 0xFF), round(g * 0xFF), round(b * 0xFF)]; // return [r * 0xFF & 0xFF, g * 0xFF & 0xFF, b * 0xFF & 0xFF]
}
}
const hue = (r, g, b, max, dif) => {
if (dif === 0) return 0;
switch (max) {
case r:
return ((g - b) / dif + (g < b ? 6 : 0)) % 6;
case g:
return (b - r) / dif + 2;
case b:
return (r - g) / dif + 4;
}
};
const bound = ([r, g, b]) => {
let ma = r,
mi = r;
if (g > r) {
ma = g;
} else {
mi = g;
}
if (b > ma) ma = b;
if (b < mi) mi = b;
return {
max: ma,
sum: ma + mi,
dif: ma - mi
};
};
const expandHex = (hex, hi) => {
hi = hi || hex.length;
let x = '';
for (let i = 0, el; i < hi; i++) {
el = hex[i];
x += el + el;
} // for (let c of hex) x += c + c
return x;
};
class Rgb {
/**
* !dif: dif===0
* @param {number} r - [0,255]
* @param {number} g - [0,255]
* @param {number} b - [0,255]
* @returns {[number,number,number]} [Hue([0,360]), Saturation([0,100]), Lightness([0,100])]
*/
static toHsl([r, g, b]) {
r /= 255;
g /= 255;
b /= 255;
const {
max,
sum,
dif
} = bound([r, g, b]);
let h = hue(r, g, b, max, dif) * 60,
s = !dif ? 0 : sum > 1 ? dif / (2 - sum) : dif / sum,
l = sum / 2;
return [round(h), round(s * 1000) / 10, round(l * 1000) / 10];
}
/**
*
* @param {number} r
* @param {number} g
* @param {number} b
* @returns {string}
*/
static toHex([r, g, b]) {
// [r, g, b] = [Math.round(r), Math.round(g), Math.round(b)]
return '#' + (((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF)).toString(16).toUpperCase().padStart(6, '0');
}
static fromHex(hex) {
if (hex.charAt(0) === '#') hex = hex.substring(1);
if (!hex[3]) hex = expandHex(hex);
const n = parseInt(hex, 16);
return [n >> 16 & 0xFF, n >> 8 & 0xFF, n & 0xFF];
}
}
function _defineProperty$1(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
const ADD_FORE = '38;2';
const CLR_FORE = '39';
const ESC = '\u001b';
const Effects = {
bold: ['1', '22'],
italic: ['3', '23'],
underline: ['4', '24'],
inverse: ['7', '27']
};
const ConsoleColors = {
black: '30',
red: '31',
green: '32',
yellow: '33',
blue: '34',
magenta: '35',
cyan: '36',
white: '37',
grey: '90'
};
const br$1 = config => `${ESC}[${config.join(';')}m`;
const render = (tx, {
color,
head,
tail
} = {}) => {
let h = Array.isArray(color) ? [ADD_FORE].concat(color.slice(0, 3)) : [ConsoleColors[color] || ConsoleColors.white],
t = [CLR_FORE],
ve;
if (head && (ve = Object.values(head)) && ve.length) h = h.concat(ve);
if (tail && (ve = Object.values(tail)) && ve.length) t = t.concat(ve);
return br$1(h) + tx + br$1(t);
};
const rgbToLong = ([r, g, b]) => ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF);
let _Symbol$toPrimitive; // let n = 0
_Symbol$toPrimitive = Symbol.toPrimitive;
class Hatsu extends Callable {
/** @property {{color:([*,*,*]), head:{}, tail:{}}} */
constructor(rgb) {
super(msg => render(msg, this.config));
_defineProperty$1(this, "config", void 0);
this.config = {
color: rgb,
head: {},
tail: {}
};
return new Proxy(this, {
/**
*
* @param target
* @param p
* @param receiver
* @returns {Hatsu|function(string):string}
*/
get(target, p, receiver) {
if (p in target) return target[p];
if (p.startsWith('ext')) return (p = p.slice(3).toLowerCase()) in Effects ? target.removeEffect(p) : (target.clear(), receiver);
const {
config
} = target;
if (p in Effects && !(p in config)) return [config.head[p], config.tail[p]] = Effects[p], receiver;
if (p in ConsoleColors) return config.color = p, receiver;
return receiver;
}
});
}
[_Symbol$toPrimitive](h) {
const {
config
} = this;
switch (h) {
case 'string':
case 'default':
return render(config.color, config);
case 'number':
return Array.isArray(config.color) ? rgbToLong(config.color) : Number.NaN;
default:
throw new Error('ink Symbol.toPrimitive error');
}
}
/**
*
* @param {number[]} arr
* @returns {Hatsu|function}
*/
static rgb(arr) {
return new Hatsu(arr);
}
/**
*
* @param str
* @returns {Hatsu|function}
*/
static hex(str) {
return Hatsu.rgb(Rgb.fromHex(str));
}
/**
*
* @param arr
* @returns {Hatsu|function}
*/
static hsl(arr) {
return Hatsu.rgb(Hsl.toRgb(arr));
}
rgb(rgb, text) {
const {
config
} = this;
config.color = rgb;
return text ? render(text, config) : this;
}
hex(hex, text) {
const {
config
} = this;
config.color = Rgb.fromHex(hex);
return text ? render(text, config) : this;
}
removeEffect(effect) {
const {
config
} = this;
delete config.head[effect];
delete config.tail[effect];
return this;
}
clear() {
const {
config
} = this;
config.head = {};
config.tail = {};
return this;
}
}
const red = {
base: '#F44336',
lighten_5: '#FFEBEE',
lighten_4: '#FFCDD2',
lighten_3: '#EF9A9A',
lighten_2: '#E57373',
lighten_1: '#EF5350',
darken_1: '#E53935',
darken_2: '#D32F2F',
darken_3: '#C62828',
darken_4: '#B71C1C',
accent_1: '#FF8A80',
accent_2: '#FF5252',
accent_3: '#FF1744',
accent_4: '#D50000'
};
const pink = {
base: '#E91E63',
lighten_5: '#FCE4EC',
lighten_4: '#F8BBD0',
lighten_3: '#F48FB1',
lighten_2: '#F06292',
lighten_1: '#EC407A',
darken_1: '#D81B60',
darken_2: '#C2185B',
darken_3: '#AD1457',
darken_4: '#880E4F',
accent_1: '#FF80AB',
accent_2: '#FF4081',
accent_3: '#F50057',
accent_4: '#C51162'
};
const purple = {
base: '#9C27B0',
lighten_5: '#F3E5F5',
lighten_4: '#E1BEE7',
lighten_3: '#CE93D8',
lighten_2: '#BA68C8',
lighten_1: '#AB47BC',
darken_1: '#8E24AA',
darken_2: '#7B1FA2',
darken_3: '#6A1B9A',
darken_4: '#4A148C',
accent_1: '#EA80FC',
accent_2: '#E040FB',
accent_3: '#D500F9',
accent_4: '#AA00FF'
};
const deepPurple = {
base: '#673AB7',
lighten_5: '#EDE7F6',
lighten_4: '#D1C4E9',
lighten_3: '#B39DDB',
lighten_2: '#9575CD',
lighten_1: '#7E57C2',
darken_1: '#5E35B1',
darken_2: '#512DA8',
darken_3: '#4527A0',
darken_4: '#311B92',
accent_1: '#B388FF',
accent_2: '#7C4DFF',
accent_3: '#651FFF',
accent_4: '#6200EA'
};
const indigo = {
base: '#3F51B5',
lighten_5: '#E8EAF6',
lighten_4: '#C5CAE9',
lighten_3: '#9FA8DA',
lighten_2: '#7986CB',
lighten_1: '#5C6BC0',
darken_1: '#3949AB',
darken_2: '#303F9F',
darken_3: '#283593',
darken_4: '#1A237E',
accent_1: '#8C9EFF',
accent_2: '#536DFE',
accent_3: '#3D5AFE',
accent_4: '#304FFE'
};
const blue = {
base: '#2196F3',
lighten_5: '#E3F2FD',
lighten_4: '#BBDEFB',
lighten_3: '#90CAF9',
lighten_2: '#64B5F6',
lighten_1: '#42A5F5',
darken_1: '#1E88E5',
darken_2: '#1976D2',
darken_3: '#1565C0',
darken_4: '#0D47A1',
accent_1: '#82B1FF',
accent_2: '#448AFF',
accent_3: '#2979FF',
accent_4: '#2962FF'
};
const lightBlue = {
base: '#03A9F4',
lighten_5: '#E1F5FE',
lighten_4: '#B3E5FC',
lighten_3: '#81D4FA',
lighten_2: '#4FC3F7',
lighten_1: '#29B6F6',
darken_1: '#039BE5',
darken_2: '#0288D1',
darken_3: '#0277BD',
darken_4: '#01579B',
accent_1: '#80D8FF',
accent_2: '#40C4FF',
accent_3: '#00B0FF',
accent_4: '#0091EA'
};
const cyan = {
base: '#00BCD4',
lighten_5: '#E0F7FA',
lighten_4: '#B2EBF2',
lighten_3: '#80DEEA',
lighten_2: '#4DD0E1',
lighten_1: '#26C6DA',
darken_1: '#00ACC1',
darken_2: '#0097A7',
darken_3: '#00838F',
darken_4: '#006064',
accent_1: '#84FFFF',
accent_2: '#18FFFF',
accent_3: '#00E5FF',
accent_4: '#00B8D4'
};
const teal = {
base: '#009688',
lighten_5: '#E0F2F1',
lighten_4: '#B2DFDB',
lighten_3: '#80CBC4',
lighten_2: '#4DB6AC',
lighten_1: '#26A69A',
darken_1: '#00897B',
darken_2: '#00796B',
darken_3: '#00695C',
darken_4: '#004D40',
accent_1: '#A7FFEB',
accent_2: '#64FFDA',
accent_3: '#1DE9B6',
accent_4: '#00BFA5'
};
const green = {
base: '#4CAF50',
lighten_5: '#E8F5E9',
lighten_4: '#C8E6C9',
lighten_3: '#A5D6A7',
lighten_2: '#81C784',
lighten_1: '#66BB6A',
darken_1: '#43A047',
darken_2: '#388E3C',
darken_3: '#2E7D32',
darken_4: '#1B5E20',
accent_1: '#B9F6CA',
accent_2: '#69F0AE',
accent_3: '#00E676',
accent_4: '#00C853'
};
const lightGreen = {
base: '#8BC34A',
lighten_5: '#F1F8E9',
lighten_4: '#DCEDC8',
lighten_3: '#C5E1A5',
lighten_2: '#AED581',
lighten_1: '#9CCC65',
darken_1: '#7CB342',
darken_2: '#689F38',
darken_3: '#558B2F',
darken_4: '#33691E',
accent_1: '#CCFF90',
accent_2: '#B2FF59',
accent_3: '#76FF03',
accent_4: '#64DD17'
};
const lime = {
base: '#CDDC39',
lighten_5: '#F9FBE7',
lighten_4: '#F0F4C3',
lighten_3: '#E6EE9C',
lighten_2: '#DCE775',
lighten_1: '#D4E157',
darken_1: '#C0CA33',
darken_2: '#AFB42B',
darken_3: '#9E9D24',
darken_4: '#827717',
accent_1: '#F4FF81',
accent_2: '#EEFF41',
accent_3: '#C6FF00',
accent_4: '#AEEA00'
};
const yellow = {
base: '#FFEB3B',
lighten_5: '#FFFDE7',
lighten_4: '#FFF9C4',
lighten_3: '#FFF59D',
lighten_2: '#FFF176',
lighten_1: '#FFEE58',
darken_1: '#FDD835',
darken_2: '#FBC02D',
darken_3: '#F9A825',
darken_4: '#F57F17',
accent_1: '#FFFF8D',
accent_2: '#FFFF00',
accent_3: '#FFEA00',
accent_4: '#FFD600'
};
const amber = {
base: '#FFC107',
lighten_5: '#FFF8E1',
lighten_4: '#FFECB3',
lighten_3: '#FFE082',
lighten_2: '#FFD54F',
lighten_1: '#FFCA28',
darken_1: '#FFB300',
darken_2: '#FFA000',
darken_3: '#FF8F00',
darken_4: '#FF6F00',
accent_1: '#FFE57F',
accent_2: '#FFD740',
accent_3: '#FFC400',
accent_4: '#FFAB00'
};
const orange = {
base: '#FF9800',
lighten_5: '#FFF3E0',
lighten_4: '#FFE0B2',
lighten_3: '#FFCC80',
lighten_2: '#FFB74D',
lighten_1: '#FFA726',
darken_1: '#FB8C00',
darken_2: '#F57C00',
darken_3: '#EF6C00',
darken_4: '#E65100',
accent_1: '#FFD180',
accent_2: '#FFAB40',
accent_3: '#FF9100',
accent_4: '#FF6D00'
};
const deepOrange = {
base: '#FF5722',
lighten_5: '#FBE9E7',
lighten_4: '#FFCCBC',
lighten_3: '#FFAB91',
lighten_2: '#FF8A65',
lighten_1: '#FF7043',
darken_1: '#F4511E',
darken_2: '#E64A19',
darken_3: '#D84315',
darken_4: '#BF360C',
accent_1: '#FF9E80',
accent_2: '#FF6E40',
accent_3: '#FF3D00',
accent_4: '#DD2C00'
};
const brown = {
base: '#795548',
lighten_5: '#EFEBE9',
lighten_4: '#D7CCC8',
lighten_3: '#BCAAA4',
lighten_2: '#A1887F',
lighten_1: '#8D6E63',
darken_1: '#6D4C41',
darken_2: '#5D4037',
darken_3: '#4E342E',
darken_4: '#3E2723',
accent_1: '#D2BEB6',
accent_2: '#B59387',
accent_3: '#A27767',
accent_4: '#855F51'
};
const blueGrey = {
base: '#607D8B',
lighten_5: '#ECEFF1',
lighten_4: '#CFD8DC',
lighten_3: '#B0BEC5',
lighten_2: '#90A4AE',
lighten_1: '#78909C',
darken_1: '#546E7A',
darken_2: '#455A64',
darken_3: '#37474F',
darken_4: '#263238',
accent_1: '#B7C9D1',
accent_2: '#89A5B3',
accent_3: '#6A8EA0',
accent_4: '#547383'
};
const grey = {
base: '#9E9E9E',
lighten_5: '#FAFAFA',
lighten_4: '#F5F5F5',
lighten_3: '#EEEEEE',
lighten_2: '#E0E0E0',
lighten_1: '#BDBDBD',
darken_1: '#757575',
darken_2: '#616161',
darken_3: '#424242',
darken_4: '#212121',
accent_1: '#C4C4C4',
accent_2: '#9E9E9E',
accent_3: '#858585',
accent_4: '#6B6B6B'
};
/**
*
* @param {Object} palett
* @returns {Object|{colors:string[],degrees:string[]}}
*/
const addProps = palett => {
Reflect.defineProperty(palett, 'colors', {
get() {
return Object.keys(palett);
},
enumerable: false
});
Reflect.defineProperty(palett, 'degrees', {
get() {
for (let color in palett) return Object.keys(palett[color]);
},
enumerable: false
});
return palett;
};
var _red$pink$purple$deep;
/**
* @type {Object.<string,Object<string,Object>>}
* @property {string[]} colors
* @property {string[]} degrees
*/
const Palett = (_red$pink$purple$deep = {
red,
pink,
purple,
deepPurple,
indigo,
blue,
lightBlue,
cyan,
teal,
green,
lightGreen,
lime,
yellow,
amber,
orange,
deepOrange,
brown,
blueGrey,
grey
}, addProps(_red$pink$purple$deep));
const {
hex
} = Hatsu;
const Thm = {
brm: hex(Palett.blueGrey.base),
br: hex(Palett.orange.lighten_3),
pr: hex(Palett.indigo.lighten_1)
};
const bracketMain = tx => Thm.brm('[') + tx + Thm.brm(']');
const bracket = tx => Thm.br('[') + tx + Thm.br(']');
const parenthesis = tx => Thm.pr('(') + tx + Thm.pr(')');
const render$1 = (tx, {
indent,
stream
}) => {
if (tx === null || tx === void 0 ? void 0 : tx.length) stream.push(tx);
return ' '.repeat(indent << 1) + stream.join(' ');
};
const preset = (label, ...items) => {
var _label;
let stream = [],
indent,
len;
if (label && (label = String(label)) && (len = label.length) && (indent = (_label = label, deNaTab$1(_label))) < len) {
var _label$slice;
stream.push((_label$slice = label.slice(indent), bracketMain(_label$slice)));
}
if (items.length) {
var _items$map$join;
stream.push((_items$map$join = items.map(totx$1).join(','), parenthesis(_items$map$join)));
}
return {
indent,
stream
};
};
let _Symbol$toPrimitive$1;
class Callable$1 extends Function {
constructor(f) {
super();
Reflect.setPrototypeOf(f, new.target.prototype);
return f;
}
}
/**
* @type {object<string,string>}
*/
_Symbol$toPrimitive$1 = Symbol.toPrimitive;
class Ink extends Callable$1 {
/** @type {number} */
/** @type {string[]} */
constructor(label, ...items) {
super(tx => render$1(tx, this));
_defineProperty(this, "indent", void 0);
_defineProperty(this, "stream", void 0);
this.cr(label, ...items);
return new Proxy(this, {
/** @returns {Ink|function(...string):Ink} */
get(target, p, receiver) {
var _String;
if (p in target) return target[p];
target.stream.push((_String = String(p), bracket(_String)));
return (...items) => {
var _items$map$join;
target.stream.push((_items$map$join = items.map(totx$1).join(', '), parenthesis(_items$map$join)));
return receiver;
};
}
});
}
[_Symbol$toPrimitive$1](h) {
switch (h) {
case 'string':
case 'default':
return render$1(null, this);
case 'number':
return this.indent;
default:
throw new Error('ink Symbol.toPrimitive error');
}
}
cr(label, ...items) {
const {
indent,
stream
} = preset(label, ...items);
this.indent = indent;
this.stream = stream;
return this;
}
asc() {
this.indent++;
return this;
}
desc() {
this.indent--;
return this;
}
tag(key, ...items) {
var _ref, _key, _items$map$join2;
this.stream.push((_ref = (_key = key, totx$1(_key)), tabify$1(_ref)));
if (items.length) this.stream.push((_items$map$join2 = items.map(totx$1).join(','), parenthesis(_items$map$join2)));
return this;
}
br(...items) {
this.stream.push(items.map(parenthesis).join(','));
return this;
}
p(...items) {
this.stream.push(...items);
return this;
}
get tx() {
return render$1(null, this);
}
get say() {
return render$1(null, this);
}
toString() {
return render$1(null, this);
}
}
const ink = new Ink();
const FullAngleReg = /[\u4e00-\u9fa5]|[\uff00-\uffff]/;
/**
* Return if a string contains Chinese character.
* halfAng = str.match(/[\u0000-\u00ff]/g) || [] //半角
* chinese = str.match(/[\u4e00-\u9fa5]/g) || [] //中文
* fullAng = str.match(/[\uff00-\uffff]/g) || [] //全角
* @param {string} str
* @returns {boolean}
*/
const hasChn = str => str.search(FullAngleReg) !== -1;
/**
* Half-angle string -> Full-angle string
* 半角转化为全角
* a.全角空格为12288,半角空格为32
* b.其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
* @param {string} tx
* @returns {string}
* @constructor
*/
const toFullAngle = tx => {
let t = '',
co;
for (let c of tx) {
co = c.charCodeAt(0);
t = co === 32 ? t + String.fromCharCode(12288) : co < 127 ? t + String.fromCharCode(co + 65248) : t + c;
}
return t;
};
/**
*
*
* @param {string[][]} text
* @param {*[][]} raw
* @param {function[][]} [dye]
* @param {*[][]} head
* @param {boolean=false} [ansi]
* @param {boolean=false} chinese
* @return {{head: string[], rows: string[][], hr: string[]}}
*/
const formatTable = ({
text,
raw,
dye,
head,
ansi = false,
chinese = false
} = {}) => {
if (chinese) return formatTableFullAngle({
head,
text,
raw,
dye,
ansi
});
const pads = matrix.transpose([head].concat(text)).map(col => maxLen(col, ansi)),
[h, hr, rows] = [vector.zipper(head, pads, (x, p) => lpad(x, p, ansi)), pads.map(l => '-'.repeat(l)), formatMatrix({
text,
raw,
dye,
pads,
ansi
})];
return {
head: h,
rows,
hr
};
};
/**
*
*
* @param {string[][]} text
* @param {*[][]} raw
* @param {function[][]} [dye]
* @param {*[][]} head
* @param {boolean=false} [ansi]
* @return {{head: string[], rows: string[][], hr: string[]}}
*/
const formatTableFullAngle = ({
text,
raw,
dye,
head,
ansi = false
} = {}) => {
const {
dash,
space
} = FAChars;
/**
*
* @type {{pd:number,cn:boolean}[]}
*/
const pads = matrix.transpose([head].concat(text)).map(col => ({
pd: maxLen(col, ansi),
cn: col.some(hasChn)
})),
[h, hr, rows] = [vector.zipper(head, pads, (x, {
cn,
pd
}) => cn ? rpad(toFullAngle(x), pd, ansi, space) : rpad(x, pd, ansi)), pads.map(p => (p.cn ? dash : '-').repeat(p.pd)), dye ? text.map((text, i) => vector.zipper(text, pads, (tx, {
cn,
pd
}, j) => {
var _npad, _npad2;
return cn ? (_npad = npad(toFullAngle(tx), raw[i][j], pd, ansi, space), dye[i][j](_npad)) : (_npad2 = npad(tx, raw[i][j], pd, ansi), dye[i][j](_npad2));
})) : text.map((text, i) => vector.zipper(text, pads, (tx, {
cn,
pd
}, j) => cn ? npad(toFullAngle(tx), raw[i][j], pd, ansi, space) : npad(tx, raw[i][j], pd, ansi)))];
return {
head: h,
rows,
hr
};
};
exports.AEU = AEU;

@@ -1285,23 +76,8 @@ exports.DASH = DASH;

exports.endsBracs = endsBracs;
exports.formatMatrix = formatMatrix;
exports.formatTable = formatTable;
exports.formatVector = formatVector;
exports.indexMaxLen = indexMaxLen;
exports.intDigits = intDigits;
exports.isNumeric = isNumeric;
exports.isTab = isTab;
exports.isVisual = isVisual;
exports.lpad = lpad;
exports.maxLen = maxLen;
exports.noop = noop;
exports.npad = npad;
exports.padVector = padVector;
exports.pr = pr;
exports.readCrop = readCrop;
exports.rn = rn;
exports.rpad = rpad;
exports.tabify = tabify;
exports.tb = tb;
exports.totx = totx;
exports.vecPad = vecPad;
exports.vecPalPad = vecPalPad;

1211

dist/index.esm.js

@@ -1,6 +0,1 @@

import { hasAnsi, lange } from '@spare/lange';
import { Num, OBJ } from 'typen';
import { transpose } from '@vect/matrix';
import { zipper } from '@vect/vector';
const noop = () => {};

@@ -47,30 +42,2 @@ /**

const {
isNumeric
} = Num;
const lpad = (tx, pd, ansi = false, fill) => ansi && hasAnsi(tx) ? tx.padStart(tx.length + pd - lange(tx), fill) : tx.padStart(pd, fill);
const rpad = (tx, pd, ansi = false, fill) => ansi && hasAnsi(tx) ? tx.padEnd(tx.length + pd - lange(tx), fill) : tx.padEnd(pd, fill);
const npad = (tx, ref, pd, ansi = false, fill) => isNumeric(ref) ? lpad(tx, pd, ansi, fill) : rpad(tx, pd, ansi, fill);
// export const isVisual = visual =>
const defaultPreset = {
max: '#74FF03',
min: '#FF5252',
na: '#A27767'
};
const isVisual = visual => {
if (!visual) return undefined;
if (typeof visual !== OBJ) return defaultPreset;
for (let k in defaultPreset) if (!(k in visual)) visual[k] = defaultPreset[k];
return visual;
};
const readCrop = ({
head,
tail
}) => [head, tail];
const rn = '\r\n';

@@ -90,1178 +57,2 @@ const tb = ' ';

/**
*
* @param {Array<?string>} arr
* @param ansi
*/
const maxLen = (arr, ansi = false) => ansi ? Math.max(...arr.map(x => x ? lange(x) : 0)) : Math.max(...arr.map(x => {
var _ref;
return (_ref = x === null || x === void 0 ? void 0 : x.length) !== null && _ref !== void 0 ? _ref : 0;
}));
const intDigits = num => (num = ~~Math.log10(Math.abs(num)), ++num);
const indexMaxLen = (arr, base = 0) => intDigits(arr.length + base);
/**
*
* @param {string[]} text
* @param {*[]} [raw]
* @param {function[]} [dye]
* @param {number[]|number} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const formatVector = ({
text,
raw,
dye,
pad,
ansi
} = {}) => text.map((tx, i) => {
var _npad;
return _npad = npad(tx, raw[i], pad[i], ansi), dye[i](_npad);
});
/**
*
* @param {string[]} text
* @param {*[]} raw
* @param {number[]} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const padVector = ({
text,
raw,
pad,
ansi
}) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi));
/**
*
* @param {string[]} text
* @param {*[]} [raw]
* @param {function[]} [dye]
* @param {number[]|number} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const vecPalPad = (text, raw, dye, pad, ansi) => text.map((tx, i) => {
var _npad;
return _npad = npad(tx, raw[i], pad[i], ansi), dye[i](_npad);
});
/**
*
* @param {string[]} text
* @param {*[]} raw
* @param {number[]} [pad]
* @param {boolean=false} [ansi]
* @return {string[]}
*/
const vecPad = (text, raw, pad, ansi) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi));
/**
*
* @param {string[][]} text
* @param {*[][]} raw
* @param {function[][]} dye
* @param {number[]} pad
* @param {boolean} ansi
* @returns {string[][]}
*/
const formatMatrix = ({
text,
raw,
dye,
pad,
ansi
}) => {
return dye ? text.map((row, i) => formatVector({
text: row,
raw: raw[i],
dye: dye[i],
pad,
ansi
})) : text.map((row, i) => padVector({
text: row,
raw: raw[i],
pad,
ansi
}));
};
/**
*
* @param {*} x
* @return {string}
*/
const totx$1 = x => `${x}`;
const isTab$1 = c => c === '\t' || c === ' ';
const tabify$1 = tx => {
var _tx;
const i = (_tx = tx, deNaTab$1(_tx));
return endsBracs$1(tx) ? tx : `${tx.substring(0, i)}[${tx.substring(i)}]`;
};
const deNaTab$1 = tx => {
let i = 0;
for (let {
length
} = tx; i < length; i++) if (!isTab$1(tx.charAt(i))) return i;
return i;
};
const endsBracs$1 = tx => tx.endsWith(')') || tx.endsWith(']');
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Callable extends Function {
constructor(f) {
super();
Reflect.setPrototypeOf(f, new.target.prototype);
return f;
}
}
class Zu {
/**
* Generate a random integer between [min, max].
* Both min & max are inclusive.
* @param {Number} min Int
* @param {Number} max Int
* @returns {Number} Int
* @deprecated Prefer Roulett.between in npm package Roulett
*/
static randBetween(min, max) {
return ~~(Math.random() * (max - min + 1)) + min;
}
/**
* Generate a random integer between [min, max).
* Notice: min is inclusive & max is exclusive.
* @param {Number} min Int
* @param {Number} max(exclusive) Int
* @returns {Number} Int
* @deprecated Prefer Roulett.rand in npm package Roulett
*/
static rand(min, max) {
return ~~(Math.random() * (max - min)) + min;
}
static almostEquals(x, y, epsilon) {
return Math.abs(x - y) < epsilon;
}
static almostInt(x, epsilon) {
// let rounded = Math.round(x)
// return rounded - epsilon < x && rounded + epsilon > x
return Math.abs(x - Math.round(x)) < epsilon;
}
/**
*
* @param {number} x
* @returns {number}
*/
static intExponent(x) {
return ~~Math.log10(x);
}
/**
*
* @param {number} x
* @returns {number}
*/
static round(x) {
return x + (x > 0 ? 0.5 : -0.5) << 0;
}
} // ? x => Math.log10(x)
/**
*
* @param {number} n
* @param {number} h
* @param {number} a
* @param {number} l
* @returns {number}
*/
const hf = (n, h, a, l) => {
const k = (n + h / 30) % 12;
return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
};
const {
round
} = Zu; // export const round = x => ~~x
class Hsl {
/**
*
* @param {number} h
* @param {number} s
* @param {number} l
* @returns {number[]}
*/
static toRgb([h, s, l]) {
s /= 100;
l /= 100;
const a = s * Math.min(l, 1 - l),
r = hf(0, h, a, l),
g = hf(8, h, a, l),
b = hf(4, h, a, l);
return [round(r * 0xFF), round(g * 0xFF), round(b * 0xFF)]; // return [r * 0xFF & 0xFF, g * 0xFF & 0xFF, b * 0xFF & 0xFF]
}
}
const hue = (r, g, b, max, dif) => {
if (dif === 0) return 0;
switch (max) {
case r:
return ((g - b) / dif + (g < b ? 6 : 0)) % 6;
case g:
return (b - r) / dif + 2;
case b:
return (r - g) / dif + 4;
}
};
const bound = ([r, g, b]) => {
let ma = r,
mi = r;
if (g > r) {
ma = g;
} else {
mi = g;
}
if (b > ma) ma = b;
if (b < mi) mi = b;
return {
max: ma,
sum: ma + mi,
dif: ma - mi
};
};
const expandHex = (hex, hi) => {
hi = hi || hex.length;
let x = '';
for (let i = 0, el; i < hi; i++) {
el = hex[i];
x += el + el;
} // for (let c of hex) x += c + c
return x;
};
class Rgb {
/**
* !dif: dif===0
* @param {number} r - [0,255]
* @param {number} g - [0,255]
* @param {number} b - [0,255]
* @returns {[number,number,number]} [Hue([0,360]), Saturation([0,100]), Lightness([0,100])]
*/
static toHsl([r, g, b]) {
r /= 255;
g /= 255;
b /= 255;
const {
max,
sum,
dif
} = bound([r, g, b]);
let h = hue(r, g, b, max, dif) * 60,
s = !dif ? 0 : sum > 1 ? dif / (2 - sum) : dif / sum,
l = sum / 2;
return [round(h), round(s * 1000) / 10, round(l * 1000) / 10];
}
/**
*
* @param {number} r
* @param {number} g
* @param {number} b
* @returns {string}
*/
static toHex([r, g, b]) {
// [r, g, b] = [Math.round(r), Math.round(g), Math.round(b)]
return '#' + (((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF)).toString(16).toUpperCase().padStart(6, '0');
}
static fromHex(hex) {
if (hex.charAt(0) === '#') hex = hex.substring(1);
if (!hex[3]) hex = expandHex(hex);
const n = parseInt(hex, 16);
return [n >> 16 & 0xFF, n >> 8 & 0xFF, n & 0xFF];
}
}
function _defineProperty$1(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
const ADD_FORE = '38;2';
const CLR_FORE = '39';
const ESC = '\u001b';
const Effects = {
bold: ['1', '22'],
italic: ['3', '23'],
underline: ['4', '24'],
inverse: ['7', '27']
};
const ConsoleColors = {
black: '30',
red: '31',
green: '32',
yellow: '33',
blue: '34',
magenta: '35',
cyan: '36',
white: '37',
grey: '90'
};
const br$1 = config => `${ESC}[${config.join(';')}m`;
const render = (tx, {
color,
head,
tail
} = {}) => {
let h = Array.isArray(color) ? [ADD_FORE].concat(color.slice(0, 3)) : [ConsoleColors[color] || ConsoleColors.white],
t = [CLR_FORE],
ve;
if (head && (ve = Object.values(head)) && ve.length) h = h.concat(ve);
if (tail && (ve = Object.values(tail)) && ve.length) t = t.concat(ve);
return br$1(h) + tx + br$1(t);
};
const rgbToLong = ([r, g, b]) => ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + (b & 0xFF);
let _Symbol$toPrimitive; // let n = 0
_Symbol$toPrimitive = Symbol.toPrimitive;
class Hatsu extends Callable {
/** @property {{color:([*,*,*]), head:{}, tail:{}}} */
constructor(rgb) {
super(msg => render(msg, this.config));
_defineProperty$1(this, "config", void 0);
this.config = {
color: rgb,
head: {},
tail: {}
};
return new Proxy(this, {
/**
*
* @param target
* @param p
* @param receiver
* @returns {Hatsu|function(string):string}
*/
get(target, p, receiver) {
if (p in target) return target[p];
if (p.startsWith('ext')) return (p = p.slice(3).toLowerCase()) in Effects ? target.removeEffect(p) : (target.clear(), receiver);
const {
config
} = target;
if (p in Effects && !(p in config)) return [config.head[p], config.tail[p]] = Effects[p], receiver;
if (p in ConsoleColors) return config.color = p, receiver;
return receiver;
}
});
}
[_Symbol$toPrimitive](h) {
const {
config
} = this;
switch (h) {
case 'string':
case 'default':
return render(config.color, config);
case 'number':
return Array.isArray(config.color) ? rgbToLong(config.color) : Number.NaN;
default:
throw new Error('ink Symbol.toPrimitive error');
}
}
/**
*
* @param {number[]} arr
* @returns {Hatsu|function}
*/
static rgb(arr) {
return new Hatsu(arr);
}
/**
*
* @param str
* @returns {Hatsu|function}
*/
static hex(str) {
return Hatsu.rgb(Rgb.fromHex(str));
}
/**
*
* @param arr
* @returns {Hatsu|function}
*/
static hsl(arr) {
return Hatsu.rgb(Hsl.toRgb(arr));
}
rgb(rgb, text) {
const {
config
} = this;
config.color = rgb;
return text ? render(text, config) : this;
}
hex(hex, text) {
const {
config
} = this;
config.color = Rgb.fromHex(hex);
return text ? render(text, config) : this;
}
removeEffect(effect) {
const {
config
} = this;
delete config.head[effect];
delete config.tail[effect];
return this;
}
clear() {
const {
config
} = this;
config.head = {};
config.tail = {};
return this;
}
}
const red = {
base: '#F44336',
lighten_5: '#FFEBEE',
lighten_4: '#FFCDD2',
lighten_3: '#EF9A9A',
lighten_2: '#E57373',
lighten_1: '#EF5350',
darken_1: '#E53935',
darken_2: '#D32F2F',
darken_3: '#C62828',
darken_4: '#B71C1C',
accent_1: '#FF8A80',
accent_2: '#FF5252',
accent_3: '#FF1744',
accent_4: '#D50000'
};
const pink = {
base: '#E91E63',
lighten_5: '#FCE4EC',
lighten_4: '#F8BBD0',
lighten_3: '#F48FB1',
lighten_2: '#F06292',
lighten_1: '#EC407A',
darken_1: '#D81B60',
darken_2: '#C2185B',
darken_3: '#AD1457',
darken_4: '#880E4F',
accent_1: '#FF80AB',
accent_2: '#FF4081',
accent_3: '#F50057',
accent_4: '#C51162'
};
const purple = {
base: '#9C27B0',
lighten_5: '#F3E5F5',
lighten_4: '#E1BEE7',
lighten_3: '#CE93D8',
lighten_2: '#BA68C8',
lighten_1: '#AB47BC',
darken_1: '#8E24AA',
darken_2: '#7B1FA2',
darken_3: '#6A1B9A',
darken_4: '#4A148C',
accent_1: '#EA80FC',
accent_2: '#E040FB',
accent_3: '#D500F9',
accent_4: '#AA00FF'
};
const deepPurple = {
base: '#673AB7',
lighten_5: '#EDE7F6',
lighten_4: '#D1C4E9',
lighten_3: '#B39DDB',
lighten_2: '#9575CD',
lighten_1: '#7E57C2',
darken_1: '#5E35B1',
darken_2: '#512DA8',
darken_3: '#4527A0',
darken_4: '#311B92',
accent_1: '#B388FF',
accent_2: '#7C4DFF',
accent_3: '#651FFF',
accent_4: '#6200EA'
};
const indigo = {
base: '#3F51B5',
lighten_5: '#E8EAF6',
lighten_4: '#C5CAE9',
lighten_3: '#9FA8DA',
lighten_2: '#7986CB',
lighten_1: '#5C6BC0',
darken_1: '#3949AB',
darken_2: '#303F9F',
darken_3: '#283593',
darken_4: '#1A237E',
accent_1: '#8C9EFF',
accent_2: '#536DFE',
accent_3: '#3D5AFE',
accent_4: '#304FFE'
};
const blue = {
base: '#2196F3',
lighten_5: '#E3F2FD',
lighten_4: '#BBDEFB',
lighten_3: '#90CAF9',
lighten_2: '#64B5F6',
lighten_1: '#42A5F5',
darken_1: '#1E88E5',
darken_2: '#1976D2',
darken_3: '#1565C0',
darken_4: '#0D47A1',
accent_1: '#82B1FF',
accent_2: '#448AFF',
accent_3: '#2979FF',
accent_4: '#2962FF'
};
const lightBlue = {
base: '#03A9F4',
lighten_5: '#E1F5FE',
lighten_4: '#B3E5FC',
lighten_3: '#81D4FA',
lighten_2: '#4FC3F7',
lighten_1: '#29B6F6',
darken_1: '#039BE5',
darken_2: '#0288D1',
darken_3: '#0277BD',
darken_4: '#01579B',
accent_1: '#80D8FF',
accent_2: '#40C4FF',
accent_3: '#00B0FF',
accent_4: '#0091EA'
};
const cyan = {
base: '#00BCD4',
lighten_5: '#E0F7FA',
lighten_4: '#B2EBF2',
lighten_3: '#80DEEA',
lighten_2: '#4DD0E1',
lighten_1: '#26C6DA',
darken_1: '#00ACC1',
darken_2: '#0097A7',
darken_3: '#00838F',
darken_4: '#006064',
accent_1: '#84FFFF',
accent_2: '#18FFFF',
accent_3: '#00E5FF',
accent_4: '#00B8D4'
};
const teal = {
base: '#009688',
lighten_5: '#E0F2F1',
lighten_4: '#B2DFDB',
lighten_3: '#80CBC4',
lighten_2: '#4DB6AC',
lighten_1: '#26A69A',
darken_1: '#00897B',
darken_2: '#00796B',
darken_3: '#00695C',
darken_4: '#004D40',
accent_1: '#A7FFEB',
accent_2: '#64FFDA',
accent_3: '#1DE9B6',
accent_4: '#00BFA5'
};
const green = {
base: '#4CAF50',
lighten_5: '#E8F5E9',
lighten_4: '#C8E6C9',
lighten_3: '#A5D6A7',
lighten_2: '#81C784',
lighten_1: '#66BB6A',
darken_1: '#43A047',
darken_2: '#388E3C',
darken_3: '#2E7D32',
darken_4: '#1B5E20',
accent_1: '#B9F6CA',
accent_2: '#69F0AE',
accent_3: '#00E676',
accent_4: '#00C853'
};
const lightGreen = {
base: '#8BC34A',
lighten_5: '#F1F8E9',
lighten_4: '#DCEDC8',
lighten_3: '#C5E1A5',
lighten_2: '#AED581',
lighten_1: '#9CCC65',
darken_1: '#7CB342',
darken_2: '#689F38',
darken_3: '#558B2F',
darken_4: '#33691E',
accent_1: '#CCFF90',
accent_2: '#B2FF59',
accent_3: '#76FF03',
accent_4: '#64DD17'
};
const lime = {
base: '#CDDC39',
lighten_5: '#F9FBE7',
lighten_4: '#F0F4C3',
lighten_3: '#E6EE9C',
lighten_2: '#DCE775',
lighten_1: '#D4E157',
darken_1: '#C0CA33',
darken_2: '#AFB42B',
darken_3: '#9E9D24',
darken_4: '#827717',
accent_1: '#F4FF81',
accent_2: '#EEFF41',
accent_3: '#C6FF00',
accent_4: '#AEEA00'
};
const yellow = {
base: '#FFEB3B',
lighten_5: '#FFFDE7',
lighten_4: '#FFF9C4',
lighten_3: '#FFF59D',
lighten_2: '#FFF176',
lighten_1: '#FFEE58',
darken_1: '#FDD835',
darken_2: '#FBC02D',
darken_3: '#F9A825',
darken_4: '#F57F17',
accent_1: '#FFFF8D',
accent_2: '#FFFF00',
accent_3: '#FFEA00',
accent_4: '#FFD600'
};
const amber = {
base: '#FFC107',
lighten_5: '#FFF8E1',
lighten_4: '#FFECB3',
lighten_3: '#FFE082',
lighten_2: '#FFD54F',
lighten_1: '#FFCA28',
darken_1: '#FFB300',
darken_2: '#FFA000',
darken_3: '#FF8F00',
darken_4: '#FF6F00',
accent_1: '#FFE57F',
accent_2: '#FFD740',
accent_3: '#FFC400',
accent_4: '#FFAB00'
};
const orange = {
base: '#FF9800',
lighten_5: '#FFF3E0',
lighten_4: '#FFE0B2',
lighten_3: '#FFCC80',
lighten_2: '#FFB74D',
lighten_1: '#FFA726',
darken_1: '#FB8C00',
darken_2: '#F57C00',
darken_3: '#EF6C00',
darken_4: '#E65100',
accent_1: '#FFD180',
accent_2: '#FFAB40',
accent_3: '#FF9100',
accent_4: '#FF6D00'
};
const deepOrange = {
base: '#FF5722',
lighten_5: '#FBE9E7',
lighten_4: '#FFCCBC',
lighten_3: '#FFAB91',
lighten_2: '#FF8A65',
lighten_1: '#FF7043',
darken_1: '#F4511E',
darken_2: '#E64A19',
darken_3: '#D84315',
darken_4: '#BF360C',
accent_1: '#FF9E80',
accent_2: '#FF6E40',
accent_3: '#FF3D00',
accent_4: '#DD2C00'
};
const brown = {
base: '#795548',
lighten_5: '#EFEBE9',
lighten_4: '#D7CCC8',
lighten_3: '#BCAAA4',
lighten_2: '#A1887F',
lighten_1: '#8D6E63',
darken_1: '#6D4C41',
darken_2: '#5D4037',
darken_3: '#4E342E',
darken_4: '#3E2723',
accent_1: '#D2BEB6',
accent_2: '#B59387',
accent_3: '#A27767',
accent_4: '#855F51'
};
const blueGrey = {
base: '#607D8B',
lighten_5: '#ECEFF1',
lighten_4: '#CFD8DC',
lighten_3: '#B0BEC5',
lighten_2: '#90A4AE',
lighten_1: '#78909C',
darken_1: '#546E7A',
darken_2: '#455A64',
darken_3: '#37474F',
darken_4: '#263238',
accent_1: '#B7C9D1',
accent_2: '#89A5B3',
accent_3: '#6A8EA0',
accent_4: '#547383'
};
const grey = {
base: '#9E9E9E',
lighten_5: '#FAFAFA',
lighten_4: '#F5F5F5',
lighten_3: '#EEEEEE',
lighten_2: '#E0E0E0',
lighten_1: '#BDBDBD',
darken_1: '#757575',
darken_2: '#616161',
darken_3: '#424242',
darken_4: '#212121',
accent_1: '#C4C4C4',
accent_2: '#9E9E9E',
accent_3: '#858585',
accent_4: '#6B6B6B'
};
/**
*
* @param {Object} palett
* @returns {Object|{colors:string[],degrees:string[]}}
*/
const addProps = palett => {
Reflect.defineProperty(palett, 'colors', {
get() {
return Object.keys(palett);
},
enumerable: false
});
Reflect.defineProperty(palett, 'degrees', {
get() {
for (let color in palett) return Object.keys(palett[color]);
},
enumerable: false
});
return palett;
};
var _red$pink$purple$deep;
/**
* @type {Object.<string,Object<string,Object>>}
* @property {string[]} colors
* @property {string[]} degrees
*/
const Palett = (_red$pink$purple$deep = {
red,
pink,
purple,
deepPurple,
indigo,
blue,
lightBlue,
cyan,
teal,
green,
lightGreen,
lime,
yellow,
amber,
orange,
deepOrange,
brown,
blueGrey,
grey
}, addProps(_red$pink$purple$deep));
const {
hex
} = Hatsu;
const Thm = {
brm: hex(Palett.blueGrey.base),
br: hex(Palett.orange.lighten_3),
pr: hex(Palett.indigo.lighten_1)
};
const bracketMain = tx => Thm.brm('[') + tx + Thm.brm(']');
const bracket = tx => Thm.br('[') + tx + Thm.br(']');
const parenthesis = tx => Thm.pr('(') + tx + Thm.pr(')');
const render$1 = (tx, {
indent,
stream
}) => {
if (tx === null || tx === void 0 ? void 0 : tx.length) stream.push(tx);
return ' '.repeat(indent << 1) + stream.join(' ');
};
const preset = (label, ...items) => {
var _label;
let stream = [],
indent,
len;
if (label && (label = String(label)) && (len = label.length) && (indent = (_label = label, deNaTab$1(_label))) < len) {
var _label$slice;
stream.push((_label$slice = label.slice(indent), bracketMain(_label$slice)));
}
if (items.length) {
var _items$map$join;
stream.push((_items$map$join = items.map(totx$1).join(','), parenthesis(_items$map$join)));
}
return {
indent,
stream
};
};
let _Symbol$toPrimitive$1;
class Callable$1 extends Function {
constructor(f) {
super();
Reflect.setPrototypeOf(f, new.target.prototype);
return f;
}
}
/**
* @type {object<string,string>}
*/
_Symbol$toPrimitive$1 = Symbol.toPrimitive;
class Ink extends Callable$1 {
/** @type {number} */
/** @type {string[]} */
constructor(label, ...items) {
super(tx => render$1(tx, this));
_defineProperty(this, "indent", void 0);
_defineProperty(this, "stream", void 0);
this.cr(label, ...items);
return new Proxy(this, {
/** @returns {Ink|function(...string):Ink} */
get(target, p, receiver) {
var _String;
if (p in target) return target[p];
target.stream.push((_String = String(p), bracket(_String)));
return (...items) => {
var _items$map$join;
target.stream.push((_items$map$join = items.map(totx$1).join(', '), parenthesis(_items$map$join)));
return receiver;
};
}
});
}
[_Symbol$toPrimitive$1](h) {
switch (h) {
case 'string':
case 'default':
return render$1(null, this);
case 'number':
return this.indent;
default:
throw new Error('ink Symbol.toPrimitive error');
}
}
cr(label, ...items) {
const {
indent,
stream
} = preset(label, ...items);
this.indent = indent;
this.stream = stream;
return this;
}
asc() {
this.indent++;
return this;
}
desc() {
this.indent--;
return this;
}
tag(key, ...items) {
var _ref, _key, _items$map$join2;
this.stream.push((_ref = (_key = key, totx$1(_key)), tabify$1(_ref)));
if (items.length) this.stream.push((_items$map$join2 = items.map(totx$1).join(','), parenthesis(_items$map$join2)));
return this;
}
br(...items) {
this.stream.push(items.map(parenthesis).join(','));
return this;
}
p(...items) {
this.stream.push(...items);
return this;
}
get tx() {
return render$1(null, this);
}
get say() {
return render$1(null, this);
}
toString() {
return render$1(null, this);
}
}
const ink = new Ink();
const FullAngleReg = /[\u4e00-\u9fa5]|[\uff00-\uffff]/;
/**
* Return if a string contains Chinese character.
* halfAng = str.match(/[\u0000-\u00ff]/g) || [] //半角
* chinese = str.match(/[\u4e00-\u9fa5]/g) || [] //中文
* fullAng = str.match(/[\uff00-\uffff]/g) || [] //全角
* @param {string} str
* @returns {boolean}
*/
const hasChn = str => str.search(FullAngleReg) !== -1;
/**
* Half-angle string -> Full-angle string
* 半角转化为全角
* a.全角空格为12288,半角空格为32
* b.其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
* @param {string} tx
* @returns {string}
* @constructor
*/
const toFullAngle = tx => {
let t = '',
co;
for (let c of tx) {
co = c.charCodeAt(0);
t = co === 32 ? t + String.fromCharCode(12288) : co < 127 ? t + String.fromCharCode(co + 65248) : t + c;
}
return t;
};
/**
*
*
* @param {string[][]} text
* @param {*[][]} raw
* @param {function[][]} [dye]
* @param {*[][]} head
* @param {boolean=false} [ansi]
* @param {boolean=false} chinese
* @return {{head: string[], rows: string[][], hr: string[]}}
*/
const formatTable = ({
text,
raw,
dye,
head,
ansi = false,
chinese = false
} = {}) => {
if (chinese) return formatTableFullAngle({
head,
text,
raw,
dye,
ansi
});
const pads = transpose([head].concat(text)).map(col => maxLen(col, ansi)),
[h, hr, rows] = [zipper(head, pads, (x, p) => lpad(x, p, ansi)), pads.map(l => '-'.repeat(l)), formatMatrix({
text,
raw,
dye,
pads,
ansi
})];
return {
head: h,
rows,
hr
};
};
/**
*
*
* @param {string[][]} text
* @param {*[][]} raw
* @param {function[][]} [dye]
* @param {*[][]} head
* @param {boolean=false} [ansi]
* @return {{head: string[], rows: string[][], hr: string[]}}
*/
const formatTableFullAngle = ({
text,
raw,
dye,
head,
ansi = false
} = {}) => {
const {
dash,
space
} = FAChars;
/**
*
* @type {{pd:number,cn:boolean}[]}
*/
const pads = transpose([head].concat(text)).map(col => ({
pd: maxLen(col, ansi),
cn: col.some(hasChn)
})),
[h, hr, rows] = [zipper(head, pads, (x, {
cn,
pd
}) => cn ? rpad(toFullAngle(x), pd, ansi, space) : rpad(x, pd, ansi)), pads.map(p => (p.cn ? dash : '-').repeat(p.pd)), dye ? text.map((text, i) => zipper(text, pads, (tx, {
cn,
pd
}, j) => {
var _npad, _npad2;
return cn ? (_npad = npad(toFullAngle(tx), raw[i][j], pd, ansi, space), dye[i][j](_npad)) : (_npad2 = npad(tx, raw[i][j], pd, ansi), dye[i][j](_npad2));
})) : text.map((text, i) => zipper(text, pads, (tx, {
cn,
pd
}, j) => cn ? npad(toFullAngle(tx), raw[i][j], pd, ansi, space) : npad(tx, raw[i][j], pd, ansi)))];
return {
head: h,
rows,
hr
};
};
export { AEU, DASH, FAChars, RN, SPACE, TB, aeu, afterNaTab, bc, beforeNaTab, br, deNaTab, endsBracs, formatMatrix, formatTable, formatVector, indexMaxLen, intDigits, isNumeric, isTab, isVisual, lpad, maxLen, noop, npad, padVector, pr, readCrop, rn, rpad, tabify, tb, totx, vecPad, vecPalPad };
export { AEU, DASH, FAChars, RN, SPACE, TB, aeu, afterNaTab, bc, beforeNaTab, br, deNaTab, endsBracs, isTab, noop, pr, rn, tabify, tb, totx };

12

package.json
{
"name": "@spare/util",
"version": "0.0.6",
"version": "0.1.0",
"description": "A util toolset for @spare.",

@@ -18,6 +18,6 @@ "main": "dist/index.cjs.js",

"dependencies": {
"@spare/lange": "^0.0.6",
"@vect/matrix": "^0.0.17",
"@vect/vector": "^0.0.17",
"typen": "^0.1.9"
"@spare/lange": "^0.1.0",
"@typen/num-strict": "^0.0.1",
"@vect/matrix": "^0.0.18",
"@vect/vector": "^0.0.18"
},

@@ -40,3 +40,3 @@ "repository": {

"homepage": "https://github.com/hoyeungw/spare#readme",
"gitHead": "026b65cd7566cd2bda3e7eaa0bcb03c97671bebf"
"gitHead": "905ae80c07c6d8bc1f4524bc20e88d50187e5c3e"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc