8000 refactor: use Object.assign & Map by SukkaW · Pull Request #36 · hexojs/hexo-pagination · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: use Object.assign & Map #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions lib/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ function pagination(base, posts, options = {}) {
if (typeof base !== 'string') throw new TypeError('base must be a string!');
if (!posts) throw new TypeError('posts is required!');

if (base && base[base.length - 1] !== '/') base += '/';
if (base && !base.endsWith('/')) base += '/';

const { length } = posts;

const { format: _format, layout, data, perPage } = Object.assign({
format: 'page/%d/',
layout: ['archive', 'index'],
data: {},
perPage: 10
}, options);

const length = posts.length;
const perPage = Object.prototype.hasOwnProperty.call(options, 'perPage') ? +options.perPage : 10;
const total = perPage ? Math.ceil(length / perPage 6D09 ) : 1;
const _format = options.format || 'page/%d/';
const layout = options.layout || ['archive', 'index'];
const data = options.data || {};
const result = [];
const urlCache = {};
const urlCache = new Map();

function formatURL(i) {
if (urlCache[i]) return urlCache[i];
if (urlCache.has(i)) return urlCache.get(i);

let url = base;
if (i > 1) url += format(_format, i);
urlCache[i] = url;
const url = i > 1 ? base + format(_format, i) : base;
urlCache.set(i, url);

return url;
}
Expand Down
0