Skip to content

Commit

Permalink
refactor: replace Object.assign with util._extend
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 27, 2019
1 parent 293dd23 commit 4fd4a9f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/theme/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

const { dirname, extname, join } = require('path');
const yfm = require('hexo-front-matter');
const omit = require('lodash/omit');
const Promise = require('bluebird');

/* Although this API is already deprecated in Node.js 6, but util._extend() is still about
* 3x faster than Object.assign(). Enen Node.js itself is still using util._extend()
*
* https://github.com/nodejs/node/pull/4903
* https://github.com/nodejs/node/pull/4593
* https://github.com/nodejs/node/pull/6977
* https://github.com/nodejs/node/pull/7208
* https://github.com/nodejs/node/pull/7255
*/
// eslint-disable-next-line node/no-deprecated-api
const { _extend } = require('util');

function View(path, data) {
this.path = path;
this.source = join(this._theme.base, 'layout', path);
Expand All @@ -31,7 +42,7 @@ View.prototype.render = function(options, callback) {
const layoutView = this._resolveLayout(layout);
if (!layoutView) return result;

const layoutLocals = Object.assign({}, locals, {
const layoutLocals = _extend({}, locals, {
body: result,
layout: false
});
Expand All @@ -51,7 +62,7 @@ View.prototype.renderSync = function(options = {}) {
const layoutView = this._resolveLayout(layout);
if (!layoutView) return result;

const layoutLocals = Object.assign({}, locals, {
const layoutLocals = _extend({}, locals, {
body: result,
layout: false
});
Expand All @@ -60,8 +71,9 @@ View.prototype.renderSync = function(options = {}) {
};

View.prototype._buildLocals = function(locals) {

return Object.assign({}, locals, Object.getPrototypeOf(locals), omit(this.data, ['layout', '_content']), {
// eslint-disable-next-line no-unused-vars
const { layout, _content, ...data } = this.data;
return _extend({}, locals, Object.getPrototypeOf(locals), data, {
filename: this.source
});
};
Expand Down

0 comments on commit 4fd4a9f

Please sign in to comment.