Skip to content

Commit

Permalink
perf(injector): shorthand optimization (#4462)
Browse files Browse the repository at this point in the history
* perf(injector): shorthand syntax
* test(injector): add test cases for getSize()
* refactor(injector): reduce exec() complexity
  • Loading branch information
SukkaW authored Aug 8, 2020
1 parent 0fb1866 commit ec1e39f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
77 changes: 49 additions & 28 deletions lib/extend/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Injector {
return arr.join('');
}

getSize(entry) {
return this.cache.apply(`${entry}-size`, Object.keys(this.store[entry]).length);
}

register(entry, value, to = 'default') {
if (!entry) throw new TypeError('entry is required');
if (typeof value === 'function') value = value();
Expand All @@ -38,41 +42,58 @@ class Injector {
entryMap[to] = valueSet;
}

exec(data, locals = { page: {} }) {
_getPageType(pageLocals) {
let currentType = 'default';
const { page } = locals;
if (pageLocals.__index) currentType = 'home';
if (pageLocals.__post) currentType = 'post';
if (pageLocals.__page) currentType = 'page';
if (pageLocals.archive) currentType = 'archive';
if (pageLocals.category) currentType = 'category';
if (pageLocals.tag) currentType = 'tag';
if (pageLocals.layout) currentType = pageLocals.layout;

return currentType;
}

if (page.__index) currentType = 'home';
if (page.__post) currentType = 'post';
if (page.__page) currentType = 'page';
if (page.archive) currentType = 'archive';
if (page.category) currentType = 'category';
if (page.tag) currentType = 'tag';
if (page.layout) currentType = page.layout;
_injector(input, pattern, flag, isBegin = true, currentType) {
if (input.includes(`hexo injector ${flag}`)) return input;

const injector = (data, pattern, flag, isBegin = true) => {
if (data.includes(`hexo injector ${flag}`)) return data;
const code = this.cache.apply(`${flag}-${currentType}-code`, () => {
const content = currentType === 'default' ? this.getText(flag, 'default') : this.getText(flag, currentType) + this.getText(flag, 'default');

const code = this.cache.apply(`${flag}-${currentType}-code`, () => {
const content = currentType === 'default' ? this.getText(flag, 'default') : this.getText(flag, currentType) + this.getText(flag, 'default');
if (!content.length) return '';
return '<!-- hexo injector ' + flag + ' start -->' + content + '<!-- hexo injector ' + flag + ' end -->';
});

if (!content.length) return '';
return '<!-- hexo injector ' + flag + ' start -->' + content + '<!-- hexo injector ' + flag + ' end -->';
});
// avoid unnecessary replace() for better performance
if (!code.length) return input;

// avoid unnecessary replace() for better performance
if (!code.length) return data;
return data.replace(pattern, str => { return isBegin ? str + code : code + str; });
};
return input.replace(pattern, str => { return isBegin ? str + code : code + str; });
}

// Inject head_begin
data = injector(data, /<head.*?>/, 'head_begin', true);
// Inject head_end
data = injector(data, '</head>', 'head_end', false);
// Inject body_begin
data = injector(data, /<body.*?>/, 'body_begin', true);
// Inject body_end
data = injector(data, '</body>', 'body_end', false);
exec(data, locals = { page: {} }) {
const { page } = locals;
const currentType = this._getPageType(page);

if (this.getSize('head_begin') !== 0) {
// Inject head_begin
data = this._injector(data, /<head.*?>/, 'head_begin', true, currentType);
}

if (this.getSize('head_end') !== 0) {
// Inject head_end
data = this._injector(data, '</head>', 'head_end', false, currentType);
}

if (this.getSize('body_begin') !== 0) {
// Inject body_begin
data = this._injector(data, /<body.*?>/, 'body_begin', true, currentType);
}

if (this.getSize('body_end') !== 0) {
// Inject body_end
data = this._injector(data, '</body>', 'body_end', false, currentType);
}

return data;
}
Expand Down
13 changes: 13 additions & 0 deletions test/scripts/extend/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ describe('Injector', () => {
i.getText('body_end').should.eql('');
});

it('getSize()', () => {
const i = new Injector();
const str = '<script src="jquery.min.js"></script>';

i.register('head_end', str);
i.register('body_end', str);
i.register('body_end', str, 'home');

i.getSize('head_begin').should.eql(0);
i.getSize('head_end').should.eql(1);
i.getSize('body_end').should.eql(2);
});

it('exec() - default', () => {
const i = new Injector();
const result = i.exec(content);
Expand Down

0 comments on commit ec1e39f

Please sign in to comment.