├── .editorconfig ├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── lib └── octopage.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | # Logs 3 | logs 4 | *.log 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # Compiled binary addons (http://nodejs.org/api/addons.html) 21 | build/Release 22 | 23 | # Dependency directory 24 | # Commenting this out is preferred by some people, see 25 | # https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 26 | node_modules 27 | 28 | # Users Environment Variables 29 | .lock-wscript 30 | # workspace files are user-specific 31 | *.sublime-workspace 32 | 33 | # project files should be checked into the repository, unless a significant 34 | # proportion of contributors will probably not be using SublimeText 35 | # *.sublime-project 36 | 37 | #sftp configuration file 38 | sftp-config.json 39 | 40 | testing.js 41 | demo/ 42 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "immed": true, 8 | "newcap": true, 9 | "noarg": true, 10 | "undef": true, 11 | "unused": "vars", 12 | "strict": true, 13 | "mocha": true 14 | } -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | benchmarks/ 2 | coverage/ 3 | test/ 4 | demo/ 5 | testing.js 6 | .DS_Store 7 | .travis.yml 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | script: "make test-travis" 5 | after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Leigh Zhu 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NODE ?= 2 | 3 | TESTS = test/* 4 | 5 | test: 6 | @$(NODE) ./node_modules/.bin/mocha \ 7 | --require should \ 8 | --reporter spec \ 9 | --slow 5s \ 10 | --timeout 30000 \ 11 | --bail 12 | 13 | test-cov: 14 | @NODE_ENV=test node \ 15 | node_modules/.bin/istanbul cover \ 16 | ./node_modules/.bin/_mocha \ 17 | -- -u exports \ 18 | --require should \ 19 | --timeout 30000 \ 20 | $(TESTS) \ 21 | --bail 22 | 23 | test-travis: 24 | @NODE_ENV=test node \ 25 | node_modules/.bin/istanbul cover \ 26 | ./node_modules/.bin/_mocha \ 27 | --report lcovonly \ 28 | -- -u exports \ 29 | --require should \ 30 | --slow 5s \ 31 | --timeout 50000 \ 32 | $(TESTS) \ 33 | --bail 34 | 35 | .PHONY: test 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # github-pagination 2 | [![NPM version](https://img.shields.io/npm/v/github-pagination.svg?style=flat)](https://www.npmjs.org/package/github-pagination) 3 | 4 | A simple js lib for parse GitHub's pagination info from API's response. 5 | 6 | ------ 7 | 8 | ## Installation 9 | 10 | ```bash 11 | $ npm install github-pagination --save 12 | ``` 13 | 14 | or 15 | 16 | ```bash 17 | $ bower install github-pagination --save 18 | ``` 19 | 20 | ## Example 21 | in node.js / io.js 22 | 23 | ```js 24 | var octopage = require('./'); 25 | 26 | var test = '; rel="next", ; rel="last", ; rel="first", ; rel="prev"'; 27 | 28 | console.log(octopage.parser(test)); 29 | // { next: '15', last: '34', first: '1', prev: '13' } 30 | ``` 31 | 32 | in browser 33 | 34 | link this script and then, 35 | 36 | ```html 37 | 38 | 44 | ``` 45 | 46 | ## License 47 | 48 | MIT © [Leigh Zhu](#) 49 | -------------------------------------------------------------------------------- /lib/octopage.js: -------------------------------------------------------------------------------- 1 | (function(window) { 2 | var octopage = {}; 3 | octopage.parser = parser; 4 | 5 | /** 6 | * @param {String} linkStr String from API's response in 'Link' field 7 | * @return {Object} 8 | */ 9 | function parser(linkStr) { 10 | return linkStr.split(',').map(function(rel) { 11 | return rel.split(';').map(function(curr, idx) { 12 | if (idx === 0) return /[^_]page=(\d+)/.exec(curr)[1]; 13 | if (idx === 1) return /rel="(.+)"/.exec(curr)[1]; 14 | }) 15 | }).reduce(function(obj, curr, i) { 16 | obj[curr[1]] = curr[0]; 17 | return obj; 18 | }, {}); 19 | } 20 | 21 | // Node.js / io.js 22 | if (typeof module !== 'undefined' && module.exports) { 23 | module.exports = octopage; 24 | } 25 | // AMD / RequireJS 26 | else if (typeof define !== 'undefined' && define.amd) { 27 | define([], function () { 28 | return octopage; 29 | }); 30 | } 31 | // browser side 32 | else { 33 | window.octopage = octopage; 34 | } 35 | }) (this); 36 | 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-pagination", 3 | "version": "0.1.0", 4 | "description": "A simple js lib for parse GitHub's pagination info from API's response.", 5 | "main": "lib/octopage.js", 6 | "dependencies": { 7 | }, 8 | "devDependencies": { 9 | "istanbul": "^0.3.2", 10 | "mocha": "^1.21.5", 11 | "should": "^4.0.4" 12 | }, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git://github.com/lisposter/github-pagination.git" 19 | }, 20 | "bugs": { 21 | "url": "https://github.com/lisposter/github-pagination/issues" 22 | }, 23 | "homepage": "https://github.com/lisposter/github-pagination", 24 | "keywords": ["GitHub","api","parser","pagination"], 25 | "author": "Leigh Zhu", 26 | "license": "MIT" 27 | } 28 | --------------------------------------------------------------------------------