Skip to content

Commit

Permalink
refactor: native Array.flat() (#4806)
Browse files Browse the repository at this point in the history
- Node 12 feature
- remove misleading "old syntax"
  * "old syntax" implies deprecation,
    but in this case, it's just different syntax
  • Loading branch information
curbengh authored Oct 25, 2021
1 parent ed0f239 commit 0c6380c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 32 deletions.
17 changes: 2 additions & 15 deletions lib/plugins/helper/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@
const { htmlTag, url_for } = require('hexo-util');
const { default: moize } = require('moize');

const flatten = function(arr, result = []) {
for (const i in arr) {
const value = arr[i];
if (Array.isArray(value)) {
flatten(value, result);
} else {
result.push(value);
}
}
return result;
};

function cssHelper(...args) {
let result = '\n';

flatten(args).forEach(item => {
// Old syntax
args.flat(Infinity).forEach(item => {
if (typeof item === 'string' || item instanceof String) {
let path = item;
if (!path.endsWith('.css')) {
path += '.css';
}
result += `<link rel="stylesheet" href="${url_for.call(this, path)}">\n`;
} else {
// New syntax
// Custom attributes
item.href = url_for.call(this, item.href);
if (!item.href.endsWith('.css')) item.href += '.css';
result += htmlTag('link', { rel: 'stylesheet', ...item }) + '\n';
Expand Down
19 changes: 2 additions & 17 deletions lib/plugins/helper/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,18 @@
const { htmlTag, url_for } = require('hexo-util');
const { default: moize } = require('moize');

/* flatten() to be replaced by Array.flat()
after Node 10 has reached EOL */
const flatten = function(arr, result = []) {
for (const i in arr) {
const value = arr[i];
if (Array.isArray(value)) {
flatten(value, result);
} else {
result.push(value);
}
}
return result;
};

function jsHelper(...args) {
let result = '\n';

flatten(args).forEach(item => {
// Old syntax
args.flat(Infinity).forEach(item => {
if (typeof item === 'string' || item instanceof String) {
let path = item;
if (!path.endsWith('.js')) {
path += '.js';
}
result += `<script src="${url_for.call(this, path)}"></script>\n`;
} else {
// New syntax
// Custom attributes
item.src = url_for.call(this, item.src);
if (!item.src.endsWith('.js')) item.src += '.js';
result += htmlTag('script', { ...item }, '') + '\n';
Expand Down

0 comments on commit 0c6380c

Please sign in to comment.