@spare/util
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -5,4 +5,4 @@ 'use strict'; | ||
var lange = require('@spare/lange'); | ||
var typen = require('typen'); | ||
var lange = require('@spare/lange'); | ||
@@ -13,4 +13,62 @@ const { | ||
const isVisual = visual => visual !== false && (visual === null || visual === void 0 ? void 0 : visual.on) !== false; | ||
const noop = () => {}; | ||
/** | ||
* | ||
* @param {*} x | ||
* @return {string} | ||
*/ | ||
const totx = x => `${x}`; | ||
const isTab = c => c === '\t' || c === ' '; | ||
const tabify = tx => { | ||
var _tx; | ||
const i = (_tx = tx, deNaTab(_tx)); | ||
return endsBracs(tx) ? tx : `${tx.substring(0, i)}[${tx.substring(i)}]`; | ||
}; | ||
const deNaTab = tx => { | ||
let i = 0; | ||
for (let { | ||
length | ||
} = tx; i < length; i++) if (!isTab(tx.charAt(i))) return i; | ||
return i; | ||
}; | ||
const beforeNaTab = tx => tx.substring(0, deNaTab(tx)); | ||
const afterNaTab = tx => tx.substring(deNaTab(tx)); | ||
const pr = tx => '(' + tx + ')'; | ||
const br = tx => '[' + tx + ']'; | ||
const bc = tx => '{' + tx + '}'; | ||
const endsBracs = tx => tx.endsWith(')') || tx.endsWith(']'); | ||
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 = ({ | ||
@@ -26,8 +84,4 @@ head, | ||
*/ | ||
function coins(mx) { | ||
if (!(mx === null || mx === void 0 ? void 0 : mx.length)) return []; | ||
const [row] = mx; | ||
if (!row) return []; | ||
return row.map((_, i) => i); | ||
} | ||
const coins = mx => mx[0].map((_, i) => i); | ||
const width = mx => mx[0].length; | ||
/** | ||
@@ -39,5 +93,4 @@ * Transpose a 2d-array. | ||
function transpose(mx) { | ||
return coins(mx).map(n => mx.map(r => r[n])); | ||
} | ||
const tr = mx => coins(mx).map(n => mx.map(r => r[n])); | ||
/** | ||
@@ -47,6 +100,5 @@ * | ||
* @param {*[]} b | ||
* @param {function(*,*,?number)} zipper | ||
* @return {any[]} | ||
* @param {function(*,*,number?)} zipper | ||
* @return {*[]} | ||
*/ | ||
function zip(a, b, zipper) { | ||
@@ -72,3 +124,3 @@ const { | ||
const SPACE = ' '; | ||
const zhChars = { | ||
const FAChars = { | ||
dash: DASH, | ||
@@ -78,51 +130,161 @@ space: SPACE | ||
const noop = () => {}; | ||
/** | ||
* | ||
* @param {*} x | ||
* @return {string} | ||
* @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; | ||
const totx = x => `${x}`; | ||
return (_ref = x === null || x === void 0 ? void 0 : x.length) !== null && _ref !== void 0 ? _ref : 0; | ||
})); | ||
const indexMaxLen = (arr, base = 0) => ~~Math.log10(arr.length + base) + 1; | ||
const lpad = (tx, pd, ansi = false, fill) => ansi && lange.hasAnsi(tx) ? tx.padStart(tx.length + pd - lange.lange(tx), fill) : tx.padStart(pd, fill); | ||
/** | ||
* | ||
* @param {string[]} text | ||
* @param {*[]} [raw] | ||
* @param {function[]} [dye] | ||
* @param {number[]|number} [pad] | ||
* @param {boolean=false} [ansi] | ||
* @return {string[]} | ||
*/ | ||
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 formatVector = ({ | ||
text, | ||
raw, | ||
dye, | ||
pad, | ||
ansi | ||
} = {}) => text.map((tx, i) => { | ||
var _npad; | ||
const numPad = (tx, ref, pd, ansi = false, fill) => isNumeric(ref) ? lpad(tx, pd, ansi, fill) : rpad(tx, pd, ansi, fill); | ||
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 isTab = c => c === '\t' || c === ' '; | ||
const padVector = ({ | ||
text, | ||
raw, | ||
pad, | ||
ansi | ||
}) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi)); | ||
const tabify = tx => { | ||
var _tx; | ||
/** | ||
* | ||
* @param {string[]} text | ||
* @param {*[]} [raw] | ||
* @param {function[]} [dye] | ||
* @param {number[]|number} [pad] | ||
* @param {boolean=false} [ansi] | ||
* @return {string[]} | ||
*/ | ||
const i = (_tx = tx, deNaTab(_tx)); | ||
return endsBracs(tx) ? tx : `${tx.substring(0, i)}[${tx.substring(i)}]`; | ||
}; | ||
const vecPalPad = (text, raw, dye, pad, ansi) => text.map((tx, i) => { | ||
var _npad; | ||
const deNaTab = tx => { | ||
let i = 0; | ||
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[]} | ||
*/ | ||
for (let { | ||
length | ||
} = tx; i < length; i++) if (!isTab(tx.charAt(i))) return i; | ||
const vecPad = (text, raw, pad, ansi) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi)); | ||
return i; | ||
/** | ||
* | ||
* @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 | ||
})); | ||
}; | ||
const beforeNaTab = tx => tx.substring(0, deNaTab(tx)); | ||
const { | ||
isNumeric: isNumeric$1 | ||
} = typen.Num; | ||
const afterNaTab = tx => tx.substring(deNaTab(tx)); | ||
const lpad$1 = (tx, pd, ansi = false, fill) => ansi && lange.hasAnsi(tx) ? tx.padStart(tx.length + pd - lange.lange(tx), fill) : tx.padStart(pd, fill); | ||
const pr = tx => '(' + tx + ')'; | ||
const rpad$1 = (tx, pd, ansi = false, fill) => ansi && lange.hasAnsi(tx) ? tx.padEnd(tx.length + pd - lange.lange(tx), fill) : tx.padEnd(pd, fill); | ||
const br = tx => '[' + tx + ']'; | ||
const npad$1 = (tx, ref, pd, ansi = false, fill) => isNumeric$1(ref) ? lpad$1(tx, pd, ansi, fill) : rpad$1(tx, pd, ansi, fill); // export const isVisual = visual => | ||
/** | ||
* | ||
* @param {*[][]} mx | ||
* @returns {number[]} | ||
*/ | ||
const bc = tx => '{' + tx + '}'; | ||
const endsBracs = tx => tx.endsWith(')') || tx.endsWith(']'); | ||
const coins$1 = mx => mx[0].map((_, i) => i); | ||
/** | ||
* Transpose a 2d-array. | ||
* @param {*[][]} mx | ||
* @returns {*[][]} | ||
*/ | ||
const tr$1 = mx => coins$1(mx).map(n => mx.map(r => r[n])); | ||
/** | ||
* | ||
* @param {*[]} a | ||
* @param {*[]} b | ||
* @param {function(*,*,number?)} zipper | ||
* @return {*[]} | ||
*/ | ||
function zip$1(a, b, zipper) { | ||
const { | ||
length | ||
} = a, | ||
arr = Array(length); | ||
for (let i = 0; i < length; i++) arr[i] = zipper(a[i], b[i], i); | ||
return arr; | ||
} | ||
const DASH$1 = '-'; | ||
const SPACE$1 = ' '; | ||
const FAChars$1 = { | ||
dash: DASH$1, | ||
space: SPACE$1 | ||
}; | ||
/** | ||
* | ||
* @param {Array<?string>} arr | ||
@@ -132,43 +294,140 @@ * @param ansi | ||
function maxLen(arr, ansi = false) { | ||
return ansi ? Math.max(...arr.map(x => x ? lange.lange(x) : 0)) : Math.max(...arr.map(x => { | ||
var _ref; | ||
const maxLen$1 = (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; | ||
})); | ||
} | ||
return (_ref = x === null || x === void 0 ? void 0 : x.length) !== null && _ref !== void 0 ? _ref : 0; | ||
})); | ||
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[]} words | ||
* @param {*[]} raws | ||
* @param {function[]} pals | ||
* @param {number[]|number} pads | ||
* | ||
* @param {string[][]} text | ||
* @param {*[][]} raw | ||
* @param {function[][]} [dye] | ||
* @param {*[][]} head | ||
* @param {boolean=false} [ansi] | ||
* @param {number} len | ||
* @return {string[]} | ||
* @param {boolean=false} chinese | ||
* @return {{head: string[], rows: string[][], hr: string[]}} | ||
*/ | ||
function vecPalPad(words, raws, pals, pads, ansi, len) { | ||
return words.map((tx, i) => { | ||
var _numPad; | ||
return _numPad = numPad(tx, raws[i], pads[i], ansi), pals[i](_numPad); | ||
}, len); | ||
} | ||
const formatTable = ({ | ||
text, | ||
raw, | ||
dye, | ||
head, | ||
ansi = false, | ||
chinese = false | ||
} = {}) => { | ||
if (chinese) return formatTableFullAngle({ | ||
head, | ||
text, | ||
raw, | ||
dye, | ||
ansi | ||
}); | ||
const pads = tr$1([head].concat(text)).map(col => maxLen$1(col, ansi)), | ||
[h, hr, rows] = [zip$1(head, pads, (x, p) => lpad$1(x, p, ansi)), pads.map(l => '-'.repeat(l)), formatMatrix({ | ||
text: text, | ||
raw: raw, | ||
dye: dye, | ||
pads, | ||
ansi | ||
})]; | ||
return { | ||
head: h, | ||
rows, | ||
hr | ||
}; | ||
}; | ||
/** | ||
* | ||
* @param {string[]} words | ||
* @param {*[]} raws | ||
* @param {number[]} [pads] | ||
* | ||
* @param {string[][]} text | ||
* @param {*[][]} raw | ||
* @param {function[][]} [dye] | ||
* @param {*[][]} head | ||
* @param {boolean=false} [ansi] | ||
* @param {number} len | ||
* @return {string[]} | ||
* @return {{head: string[], rows: string[][], hr: string[]}} | ||
*/ | ||
function vecPad(words, raws, pads, ansi, len) { | ||
return words.map((tx, i) => numPad(tx, raws[i], pads[i], ansi), len); | ||
} | ||
const formatTableFullAngle = ({ | ||
text, | ||
raw, | ||
dye, | ||
head, | ||
ansi = false | ||
} = {}) => { | ||
const { | ||
dash, | ||
space | ||
} = FAChars$1; | ||
/** | ||
* | ||
* @type {{pd:number,cn:boolean}[]} | ||
*/ | ||
const pads = tr$1([head].concat(text)).map(col => ({ | ||
pd: maxLen$1(col, ansi), | ||
cn: col.some(hasChn) | ||
})), | ||
[h, hr, rows] = [zip$1(head, pads, (x, { | ||
cn, | ||
pd | ||
}) => cn ? rpad$1(toFullAngle(x), pd, ansi, space) : rpad$1(x, pd, ansi)), pads.map(p => (p.cn ? dash : '-').repeat(p.pd)), dye ? text.map((text, i) => zip$1(text, pads, (tx, { | ||
cn, | ||
pd | ||
}, j) => { | ||
var _npad, _npad2; | ||
return cn ? (_npad = npad$1(toFullAngle(tx), raw[i][j], pd, ansi, space), dye[i][j](_npad)) : (_npad2 = npad$1(tx, raw[i][j], pd, ansi), dye[i][j](_npad2)); | ||
})) : text.map((text, i) => zip$1(text, pads, (tx, { | ||
cn, | ||
pd | ||
}, j) => cn ? npad$1(toFullAngle(tx), raw[i][j], pd, ansi, space) : npad$1(tx, raw[i][j], pd, ansi)))]; | ||
return { | ||
head: h, | ||
rows, | ||
hr | ||
}; | ||
}; | ||
exports.AEU = AEU; | ||
exports.DASH = DASH; | ||
exports.FAChars = FAChars; | ||
exports.RN = RN; | ||
@@ -185,2 +444,6 @@ exports.SPACE = SPACE; | ||
exports.endsBracs = endsBracs; | ||
exports.formatMatrix = formatMatrix; | ||
exports.formatTable = formatTable; | ||
exports.formatVector = formatVector; | ||
exports.indexMaxLen = indexMaxLen; | ||
exports.isNumeric = isNumeric; | ||
@@ -192,3 +455,4 @@ exports.isTab = isTab; | ||
exports.noop = noop; | ||
exports.numPad = numPad; | ||
exports.npad = npad; | ||
exports.padVector = padVector; | ||
exports.pr = pr; | ||
@@ -201,6 +465,6 @@ exports.readCrop = readCrop; | ||
exports.totx = totx; | ||
exports.transpose = transpose; | ||
exports.tr = tr; | ||
exports.vecPad = vecPad; | ||
exports.vecPalPad = vecPalPad; | ||
exports.zhChars = zhChars; | ||
exports.width = width; | ||
exports.zip = zip; |
@@ -1,3 +0,3 @@ | ||
import { Num } from 'typen'; | ||
import { hasAnsi, lange } from '@spare/lange'; | ||
import { Num, OBJ } from 'typen'; | ||
@@ -8,4 +8,62 @@ const { | ||
const isVisual = visual => visual !== false && (visual === null || visual === void 0 ? void 0 : visual.on) !== false; | ||
const noop = () => {}; | ||
/** | ||
* | ||
* @param {*} x | ||
* @return {string} | ||
*/ | ||
const totx = x => `${x}`; | ||
const isTab = c => c === '\t' || c === ' '; | ||
const tabify = tx => { | ||
var _tx; | ||
const i = (_tx = tx, deNaTab(_tx)); | ||
return endsBracs(tx) ? tx : `${tx.substring(0, i)}[${tx.substring(i)}]`; | ||
}; | ||
const deNaTab = tx => { | ||
let i = 0; | ||
for (let { | ||
length | ||
} = tx; i < length; i++) if (!isTab(tx.charAt(i))) return i; | ||
return i; | ||
}; | ||
const beforeNaTab = tx => tx.substring(0, deNaTab(tx)); | ||
const afterNaTab = tx => tx.substring(deNaTab(tx)); | ||
const pr = tx => '(' + tx + ')'; | ||
const br = tx => '[' + tx + ']'; | ||
const bc = tx => '{' + tx + '}'; | ||
const endsBracs = tx => tx.endsWith(')') || tx.endsWith(']'); | ||
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 = ({ | ||
@@ -21,8 +79,4 @@ head, | ||
*/ | ||
function coins(mx) { | ||
if (!(mx === null || mx === void 0 ? void 0 : mx.length)) return []; | ||
const [row] = mx; | ||
if (!row) return []; | ||
return row.map((_, i) => i); | ||
} | ||
const coins = mx => mx[0].map((_, i) => i); | ||
const width = mx => mx[0].length; | ||
/** | ||
@@ -34,5 +88,4 @@ * Transpose a 2d-array. | ||
function transpose(mx) { | ||
return coins(mx).map(n => mx.map(r => r[n])); | ||
} | ||
const tr = mx => coins(mx).map(n => mx.map(r => r[n])); | ||
/** | ||
@@ -42,6 +95,5 @@ * | ||
* @param {*[]} b | ||
* @param {function(*,*,?number)} zipper | ||
* @return {any[]} | ||
* @param {function(*,*,number?)} zipper | ||
* @return {*[]} | ||
*/ | ||
function zip(a, b, zipper) { | ||
@@ -67,3 +119,3 @@ const { | ||
const SPACE = ' '; | ||
const zhChars = { | ||
const FAChars = { | ||
dash: DASH, | ||
@@ -73,51 +125,161 @@ space: SPACE | ||
const noop = () => {}; | ||
/** | ||
* | ||
* @param {*} x | ||
* @return {string} | ||
* @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; | ||
const totx = x => `${x}`; | ||
return (_ref = x === null || x === void 0 ? void 0 : x.length) !== null && _ref !== void 0 ? _ref : 0; | ||
})); | ||
const indexMaxLen = (arr, base = 0) => ~~Math.log10(arr.length + base) + 1; | ||
const lpad = (tx, pd, ansi = false, fill) => ansi && hasAnsi(tx) ? tx.padStart(tx.length + pd - lange(tx), fill) : tx.padStart(pd, fill); | ||
/** | ||
* | ||
* @param {string[]} text | ||
* @param {*[]} [raw] | ||
* @param {function[]} [dye] | ||
* @param {number[]|number} [pad] | ||
* @param {boolean=false} [ansi] | ||
* @return {string[]} | ||
*/ | ||
const rpad = (tx, pd, ansi = false, fill) => ansi && hasAnsi(tx) ? tx.padEnd(tx.length + pd - lange(tx), fill) : tx.padEnd(pd, fill); | ||
const formatVector = ({ | ||
text, | ||
raw, | ||
dye, | ||
pad, | ||
ansi | ||
} = {}) => text.map((tx, i) => { | ||
var _npad; | ||
const numPad = (tx, ref, pd, ansi = false, fill) => isNumeric(ref) ? lpad(tx, pd, ansi, fill) : rpad(tx, pd, ansi, fill); | ||
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 isTab = c => c === '\t' || c === ' '; | ||
const padVector = ({ | ||
text, | ||
raw, | ||
pad, | ||
ansi | ||
}) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi)); | ||
const tabify = tx => { | ||
var _tx; | ||
/** | ||
* | ||
* @param {string[]} text | ||
* @param {*[]} [raw] | ||
* @param {function[]} [dye] | ||
* @param {number[]|number} [pad] | ||
* @param {boolean=false} [ansi] | ||
* @return {string[]} | ||
*/ | ||
const i = (_tx = tx, deNaTab(_tx)); | ||
return endsBracs(tx) ? tx : `${tx.substring(0, i)}[${tx.substring(i)}]`; | ||
}; | ||
const vecPalPad = (text, raw, dye, pad, ansi) => text.map((tx, i) => { | ||
var _npad; | ||
const deNaTab = tx => { | ||
let i = 0; | ||
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[]} | ||
*/ | ||
for (let { | ||
length | ||
} = tx; i < length; i++) if (!isTab(tx.charAt(i))) return i; | ||
const vecPad = (text, raw, pad, ansi) => text.map((tx, i) => npad(tx, raw[i], pad[i], ansi)); | ||
return i; | ||
/** | ||
* | ||
* @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 | ||
})); | ||
}; | ||
const beforeNaTab = tx => tx.substring(0, deNaTab(tx)); | ||
const { | ||
isNumeric: isNumeric$1 | ||
} = Num; | ||
const afterNaTab = tx => tx.substring(deNaTab(tx)); | ||
const lpad$1 = (tx, pd, ansi = false, fill) => ansi && hasAnsi(tx) ? tx.padStart(tx.length + pd - lange(tx), fill) : tx.padStart(pd, fill); | ||
const pr = tx => '(' + tx + ')'; | ||
const rpad$1 = (tx, pd, ansi = false, fill) => ansi && hasAnsi(tx) ? tx.padEnd(tx.length + pd - lange(tx), fill) : tx.padEnd(pd, fill); | ||
const br = tx => '[' + tx + ']'; | ||
const npad$1 = (tx, ref, pd, ansi = false, fill) => isNumeric$1(ref) ? lpad$1(tx, pd, ansi, fill) : rpad$1(tx, pd, ansi, fill); // export const isVisual = visual => | ||
/** | ||
* | ||
* @param {*[][]} mx | ||
* @returns {number[]} | ||
*/ | ||
const bc = tx => '{' + tx + '}'; | ||
const endsBracs = tx => tx.endsWith(')') || tx.endsWith(']'); | ||
const coins$1 = mx => mx[0].map((_, i) => i); | ||
/** | ||
* Transpose a 2d-array. | ||
* @param {*[][]} mx | ||
* @returns {*[][]} | ||
*/ | ||
const tr$1 = mx => coins$1(mx).map(n => mx.map(r => r[n])); | ||
/** | ||
* | ||
* @param {*[]} a | ||
* @param {*[]} b | ||
* @param {function(*,*,number?)} zipper | ||
* @return {*[]} | ||
*/ | ||
function zip$1(a, b, zipper) { | ||
const { | ||
length | ||
} = a, | ||
arr = Array(length); | ||
for (let i = 0; i < length; i++) arr[i] = zipper(a[i], b[i], i); | ||
return arr; | ||
} | ||
const DASH$1 = '-'; | ||
const SPACE$1 = ' '; | ||
const FAChars$1 = { | ||
dash: DASH$1, | ||
space: SPACE$1 | ||
}; | ||
/** | ||
* | ||
* @param {Array<?string>} arr | ||
@@ -127,41 +289,137 @@ * @param ansi | ||
function maxLen(arr, ansi = false) { | ||
return ansi ? Math.max(...arr.map(x => x ? lange(x) : 0)) : Math.max(...arr.map(x => { | ||
var _ref; | ||
const maxLen$1 = (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; | ||
})); | ||
} | ||
return (_ref = x === null || x === void 0 ? void 0 : x.length) !== null && _ref !== void 0 ? _ref : 0; | ||
})); | ||
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[]} words | ||
* @param {*[]} raws | ||
* @param {function[]} pals | ||
* @param {number[]|number} pads | ||
* | ||
* @param {string[][]} text | ||
* @param {*[][]} raw | ||
* @param {function[][]} [dye] | ||
* @param {*[][]} head | ||
* @param {boolean=false} [ansi] | ||
* @param {number} len | ||
* @return {string[]} | ||
* @param {boolean=false} chinese | ||
* @return {{head: string[], rows: string[][], hr: string[]}} | ||
*/ | ||
function vecPalPad(words, raws, pals, pads, ansi, len) { | ||
return words.map((tx, i) => { | ||
var _numPad; | ||
return _numPad = numPad(tx, raws[i], pads[i], ansi), pals[i](_numPad); | ||
}, len); | ||
} | ||
const formatTable = ({ | ||
text, | ||
raw, | ||
dye, | ||
head, | ||
ansi = false, | ||
chinese = false | ||
} = {}) => { | ||
if (chinese) return formatTableFullAngle({ | ||
head, | ||
text, | ||
raw, | ||
dye, | ||
ansi | ||
}); | ||
const pads = tr$1([head].concat(text)).map(col => maxLen$1(col, ansi)), | ||
[h, hr, rows] = [zip$1(head, pads, (x, p) => lpad$1(x, p, ansi)), pads.map(l => '-'.repeat(l)), formatMatrix({ | ||
text: text, | ||
raw: raw, | ||
dye: dye, | ||
pads, | ||
ansi | ||
})]; | ||
return { | ||
head: h, | ||
rows, | ||
hr | ||
}; | ||
}; | ||
/** | ||
* | ||
* @param {string[]} words | ||
* @param {*[]} raws | ||
* @param {number[]} [pads] | ||
* | ||
* @param {string[][]} text | ||
* @param {*[][]} raw | ||
* @param {function[][]} [dye] | ||
* @param {*[][]} head | ||
* @param {boolean=false} [ansi] | ||
* @param {number} len | ||
* @return {string[]} | ||
* @return {{head: string[], rows: string[][], hr: string[]}} | ||
*/ | ||
function vecPad(words, raws, pads, ansi, len) { | ||
return words.map((tx, i) => numPad(tx, raws[i], pads[i], ansi), len); | ||
} | ||
const formatTableFullAngle = ({ | ||
text, | ||
raw, | ||
dye, | ||
head, | ||
ansi = false | ||
} = {}) => { | ||
const { | ||
dash, | ||
space | ||
} = FAChars$1; | ||
/** | ||
* | ||
* @type {{pd:number,cn:boolean}[]} | ||
*/ | ||
export { AEU, DASH, RN, SPACE, TB, aeu, afterNaTab, bc, beforeNaTab, br, coins, deNaTab, endsBracs, isNumeric, isTab, isVisual, lpad, maxLen, noop, numPad, pr, readCrop, rn, rpad, tabify, tb, totx, transpose, vecPad, vecPalPad, zhChars, zip }; | ||
const pads = tr$1([head].concat(text)).map(col => ({ | ||
pd: maxLen$1(col, ansi), | ||
cn: col.some(hasChn) | ||
})), | ||
[h, hr, rows] = [zip$1(head, pads, (x, { | ||
cn, | ||
pd | ||
}) => cn ? rpad$1(toFullAngle(x), pd, ansi, space) : rpad$1(x, pd, ansi)), pads.map(p => (p.cn ? dash : '-').repeat(p.pd)), dye ? text.map((text, i) => zip$1(text, pads, (tx, { | ||
cn, | ||
pd | ||
}, j) => { | ||
var _npad, _npad2; | ||
return cn ? (_npad = npad$1(toFullAngle(tx), raw[i][j], pd, ansi, space), dye[i][j](_npad)) : (_npad2 = npad$1(tx, raw[i][j], pd, ansi), dye[i][j](_npad2)); | ||
})) : text.map((text, i) => zip$1(text, pads, (tx, { | ||
cn, | ||
pd | ||
}, j) => cn ? npad$1(toFullAngle(tx), raw[i][j], pd, ansi, space) : npad$1(tx, raw[i][j], pd, ansi)))]; | ||
return { | ||
head: h, | ||
rows, | ||
hr | ||
}; | ||
}; | ||
export { AEU, DASH, FAChars, RN, SPACE, TB, aeu, afterNaTab, bc, beforeNaTab, br, coins, deNaTab, endsBracs, formatMatrix, formatTable, formatVector, indexMaxLen, isNumeric, isTab, isVisual, lpad, maxLen, noop, npad, padVector, pr, readCrop, rn, rpad, tabify, tb, totx, tr, vecPad, vecPalPad, width, zip }; |
{ | ||
"name": "@spare/util", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A util toolset for @spare.", | ||
@@ -18,4 +18,4 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@spare/lange": "^0.0.4", | ||
"typen": "^0.1.6" | ||
"@spare/lange": "^0.0.5", | ||
"typen": "^0.1.9" | ||
}, | ||
@@ -38,3 +38,3 @@ "repository": { | ||
"homepage": "https://github.com/hoyeungw/spare#readme", | ||
"gitHead": "e3908473fa4279240b2a0fb40796ecf38d6a0e60" | ||
"gitHead": "7895b3e77293b1eecd777df0d79d9fc4506b722d" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21761
738
1
+ Added@spare/lange@0.0.5(transitive)
- Removed@spare/lange@0.0.4(transitive)
Updated@spare/lange@^0.0.5
Updatedtypen@^0.1.9