hexo-yam/lib/tools.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-12-02 08:48:09 +00:00
'use strict'
const micromatch = require('micromatch')
/**
* @param {string | string[]} patterns
* @param {{ basename: string }} options
*/
function isMatch(path = '', patterns = [], options = {}) {
if (path && patterns) {
if (path.length && patterns.length) {
if (typeof patterns === 'string') patterns = [patterns]
for (const pattern of patterns) {
// disable basename if a pattern includes a slash
let basename = options.basename
2024-12-02 08:48:09 +00:00
// only disable when basename is enabled
basename = basename && !pattern.includes('/')
if (micromatch.isMatch(path, pattern, { ...options, basename })) {
return true
}
}
}
}
return false
}
/**
* @param {string[]} paths
* @param {string | string[]} patterns
* @param {{ basename: string }} options
*/
function match(paths = [], patterns = [], options = {}) {
let input = paths
if (paths && patterns) {
if (paths.length && patterns.length) {
/** @type {string[]} */
const output = []
if (typeof patterns === 'string') patterns = [patterns]
const exclude = patterns.filter(pattern => pattern.startsWith('!'))
const include = patterns.filter(pattern => !pattern.startsWith('!'))
if (exclude.length) input = micromatch(paths, exclude, options)
if (include.length) {
for (const pattern of include) {
let basename = options.basename
2024-12-02 08:48:09 +00:00
basename = basename && !pattern.includes('/')
const tmp = micromatch(input, pattern, { ...options, basename })
if (tmp.length) output.push(...tmp)
}
return [...new Set(output)]
}
return input
}
}
return paths
}
/**
* @param {{ length: number }} original
* @param {{ length: number }} minified
2024-12-02 08:48:09 +00:00
* @param {string} path
* @param {string} ext
2024-12-02 08:48:09 +00:00
*/
function logFn(original, minified, path, ext) {
const saved = ((original.length - minified.length) / original.length * 100).toFixed(2)
const log = this.log || console
log.log(`${ext}: ${path} [${saved}% saved]`)
}
module.exports = {
isMatch,
match,
logFn
}