Files
NapCatQQ/.pnpm-store/v3/files/4e/4a1e9b19b58cabb81c29fef09aef8c1fdfb4c4fa8ff1d737e1683f3ba3ea27f20b0a8e473823bc8c219ff814f1a9972a23aac623494ac8b7987a4675267c57
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

131 lines
2.4 KiB
Plaintext

/**
* @file util
* @author junmer
*/
/* eslint-env node */
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var codePoints = require('code-points');
/**
* getFontFolder
*
* @return {string} fontFolder
*/
function getFontFolder() {
return path.resolve({
win32: '/Windows/fonts',
darwin: '/Library/Fonts',
linux: '/usr/share/fonts/truetype'
}[process.platform]);
}
/**
* getFonts
*
* @param {string} path path
* @return {Array} fonts
*/
function getFonts() {
return fs.readdirSync(getFontFolder());
}
/**
* getPureText
*
* @see https://msdn.microsoft.com/zh-cn/library/ie/2yfce773
* @see http://www.unicode.org/charts/
*
* @param {string} str target text
* @return {string} pure text
*/
function getPureText(str) {
// fix space
var emptyTextMap = {};
function replaceEmpty (word) {
emptyTextMap[word] = 1;
return '';
}
var pureText = String(str)
.trim()
.replace(/[\s]/g, replaceEmpty)
// .replace(/[\f]/g, '')
// .replace(/[\b]/g, '')
// .replace(/[\n]/g, '')
// .replace(/[\t]/g, '')
// .replace(/[\r]/g, '')
.replace(/[\u2028]/g, '')
.replace(/[\u2029]/g, '');
var emptyText = Object.keys(emptyTextMap).join('');
return pureText + emptyText;
}
/**
* getUniqText
*
* @deprecated since version 0.9.9
*
* @param {string} str target text
* @return {string} uniq text
*/
function getUniqText(str) {
return _.uniq(
str.split('')
).join('');
}
/**
* basic chars
*
* "!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}"
*
* @type {string}
*/
var basicText = String.fromCharCode.apply(this, _.range(33, 126));
/**
* get subset text
*
* @param {Object} opts opts
* @return {string} subset text
*/
function getSubsetText(opts) {
var text = opts.text || '';
// trim
text && opts.trim && (text = getPureText(text));
// basicText
opts.basicText && (text += basicText);
return text;
}
/**
* string to unicodes
*
* @param {string} str string
* @return {Array} unicodes
*/
function string2unicodes(str) {
return _.uniq(codePoints(str));
}
exports.getFontFolder = getFontFolder;
exports.getFonts = getFonts;
exports.getPureText = getPureText;
exports.getUniqText = getUniqText;
exports.getSubsetText = getSubsetText;
exports.string2unicodes = string2unicodes;