├── .editorconfig ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── fixture.js ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # .editorconfig 2 | # 3 | # Copyright (c) 2015 Charlike Mike Reagent, contributors. 4 | # Released under the MIT license. 5 | # 6 | 7 | root = true 8 | 9 | [*] 10 | charset = utf-8 11 | end_of_line = lf 12 | indent_size = 2 13 | indent_style = space 14 | insert_final_newline = true 15 | trim_trailing_whitespace = true 16 | 17 | [*.php] 18 | indent_size = 4 19 | 20 | [*.md] 21 | insert_final_newline = false 22 | trim_trailing_whitespace = false 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore 2 | # 3 | # Copyright (c) 2015 Charlike Mike Reagent, contributors. 4 | # Released under the MIT license. 5 | # 6 | 7 | # Always-ignore dirs # 8 | # #################### 9 | _gh_pages 10 | node_modules 11 | bower_components 12 | components 13 | vendor 14 | build 15 | dest 16 | dist 17 | src 18 | lib-cov 19 | coverage 20 | nbproject 21 | cache 22 | temp 23 | tmp 24 | get-comments 25 | 26 | # Packages # 27 | # ########## 28 | *.7z 29 | *.dmg 30 | *.gz 31 | *.iso 32 | *.jar 33 | *.rar 34 | *.tar 35 | *.zip 36 | 37 | # OS, Logs and databases # 38 | # ######################### 39 | *.pid 40 | *.dat 41 | *.log 42 | *.sql 43 | *.sqlite 44 | *~ 45 | ~* 46 | 47 | # Another files # 48 | # ############### 49 | Icon? 50 | .DS_Store* 51 | Thumbs.db 52 | ehthumbs.db 53 | Desktop.ini 54 | npm-debug.log 55 | .directory 56 | ._* 57 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: "node_js" 2 | sudo: false 3 | 4 | node_js: 5 | - "0.8" 6 | - "0.10" 7 | - "0.12" 8 | - "1.0" 9 | - "1.8" 10 | - "2.0" 11 | - "2.1" 12 | 13 | matrix: 14 | allow_failures: 15 | - node_js: "0.8" 16 | 17 | notifications: 18 | email: 19 | on_success: never 20 | on_failure: never 21 | 22 | before_script: 23 | - npm install standard 24 | - standard 25 | 26 | script: 27 | - npm install istanbul 28 | - istanbul cover test.js 29 | 30 | after_success: 31 | - npm install coveralls 32 | - cat ./coverage/lcov.info | coveralls 33 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 1.0.1 - 2015-05-30 4 | - Release v1.0.1 / npm@v1.0.1 5 | - update travis to test more versions and limit the spam from notifications 6 | 7 | ## 1.0.0 - 2015-05-30 8 | - Release v1.0.0 / npm@v1.0.0 9 | - add `see also` section 10 | - apidocs, readme 11 | - improve coverage 12 | - refactor 13 | - codeclimate tweaks 14 | - keywords, publish 0.2.0 15 | - implement 16 | 17 | ## 0.0.0 - 2015-05-26 18 | - first commits -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Contributions are always welcome! 4 | 5 | **Before spending lots of time on something, ask for feedback on your idea first!** 6 | 7 | Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations. 8 | 9 | 10 | ## Installing 11 | 12 | Fork and clone the repo, then `npm install` to install all dependencies and `npm test` to ensure all is okey before you start anything. 13 | 14 | 15 | ## Testing 16 | 17 | Tests are run with `npm test`. Please ensure all tests are passing before submitting a pull request (unless you're creating a failing test to increase test coverage or show a problem). 18 | 19 | ## Code Style 20 | 21 | [![standard][standard-image]][standard-url] 22 | 23 | This repository uses [`standard`][standard-url] to maintain code style and consistency, and to avoid style arguments. You are encouraged to install it globally. `npm test` runs `standard` so you don't have to! 24 | 25 | ``` 26 | npm i standard -g 27 | ``` 28 | 29 | It is intentional to don't have `standard`, `istanbul` and `coveralls` in the devDependencies. Travis will handle all that stuffs. That approach will save bandwidth also installing and development time. 30 | 31 | [standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg 32 | [standard-url]: https://github.com/feross/standard -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License 2 | 3 | Copyright (c) 2015 [Charlike Make Reagent](http://j.mp/1stW47C) 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | > SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # get-comments [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] 2 | 3 | > Extract javascript comments on per character basis. Comment object compatible with `acorn-extract-comments` and Esprima format. 4 | 5 | **Notice:** It extracts only first level block comments, not include nested comments. 6 | 7 | [![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![travis build status][travis-img]][travis-url] [![coverage status][coveralls-img]][coveralls-url] [![dependency status][david-img]][david-url] 8 | 9 | 10 | ## Install 11 | ``` 12 | npm i get-comments --save 13 | npm test 14 | ``` 15 | 16 | 17 | ## Usage 18 | > For more use-cases see the [tests](./test.js) 19 | 20 | - `` **{String}** string to extract from 21 | - `[resType]` **{Boolean}** if true, will return array, otherwise object 22 | - `return` **{Array|Object}** 23 | 24 | **Example** 25 | ```js 26 | var getComments = require('get-comments'); 27 | 28 | var input = fs.readFileSync('./fixture.js', 'utf8') 29 | var comments = getComments(input, true) 30 | //=> 31 | // [ { 32 | // "start": 249, 33 | // "end": 1353, 34 | // "type": "Block", 35 | // "loc": { 36 | // "start": { 37 | // "line": 12, 38 | // "column": 0 39 | // }, 40 | // "end": { 41 | // "line": 48, 42 | // "column": 3 43 | // } 44 | // }, 45 | // "value": "*\n * > Filtering incoming request ...", 46 | // "api": true, 47 | // "after": "module.exports = function koaIpFilter (options) {" 48 | // }, { 49 | // "start": 2187, 50 | // "end": 2267, 51 | // "type": "Block", 52 | // "loc": { 53 | // "start": { 54 | // "line": 82, 55 | // "column": 0 56 | // }, 57 | // "end": { 58 | // "line": 87, 59 | // "column": 3 60 | // } 61 | // }, 62 | // "value": "*\n * abcd description\n *\n * @name koaIpFilter\n * @param {Object} `options`\n ", 63 | // "api": false, 64 | // "after": "exports.data = function () {" 65 | // } ] 66 | ``` 67 | 68 | 69 | ## See also 70 | - [acorn-extract-comments](https://github.com/tunnckocore/acorn-extract-comments): Extract JavaScript code comments from a string, using `acorn`… [more](https://github.com/tunnckocore/acorn-extract-comments) 71 | - [acorn-strip-comments](https://github.com/tunnckocore/acorn-strip-comments): Strip code comments from given input string, using acorn javascript… [more](https://github.com/tunnckocore/acorn-strip-comments) 72 | - [esprima-extract-comments](https://github.com/jonschlinkert/esprima-extract-comments): Extract code comments from string or from a glob of… [more](https://github.com/jonschlinkert/esprima-extract-comments) 73 | - [js-comments](https://github.com/jonschlinkert/js-comments): Parse JavaScript code comments and generate API documentation. 74 | - [parse-comments](https://github.com/jonschlinkert/parse-comments): Parse code comments from JavaScript or any language that uses… [more](https://github.com/jonschlinkert/parse-comments) 75 | 76 | 77 | ## Contributing 78 | 79 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/get-comments/issues/new). 80 | But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines. 81 | 82 | 83 | ## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url] 84 | 85 | [![tunnckocore.tk][author-www-img]][author-www-url] [![keybase tunnckocore][keybase-img]][keybase-url] [![tunnckoCore npm][author-npm-img]][author-npm-url] [![tunnckoCore twitter][author-twitter-img]][author-twitter-url] [![tunnckoCore github][author-github-img]][author-github-url] 86 | 87 | 88 | [npmjs-url]: https://www.npmjs.com/package/get-comments 89 | [npmjs-img]: https://img.shields.io/npm/v/get-comments.svg?label=get-comments 90 | 91 | [license-url]: https://github.com/tunnckoCore/get-comments/blob/master/LICENSE.md 92 | [license-img]: https://img.shields.io/badge/license-MIT-blue.svg 93 | 94 | 95 | [codeclimate-url]: https://codeclimate.com/github/tunnckoCore/get-comments 96 | [codeclimate-img]: https://img.shields.io/codeclimate/github/tunnckoCore/get-comments.svg 97 | 98 | [travis-url]: https://travis-ci.org/tunnckoCore/get-comments 99 | [travis-img]: https://img.shields.io/travis/tunnckoCore/get-comments.svg 100 | 101 | [coveralls-url]: https://coveralls.io/r/tunnckoCore/get-comments 102 | [coveralls-img]: https://img.shields.io/coveralls/tunnckoCore/get-comments.svg 103 | 104 | [david-url]: https://david-dm.org/tunnckoCore/get-comments 105 | [david-img]: https://img.shields.io/david/dev/tunnckoCore/get-comments.svg 106 | 107 | [standard-url]: https://github.com/feross/standard 108 | [standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg 109 | 110 | 111 | [author-www-url]: http://www.tunnckocore.tk 112 | [author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg 113 | 114 | [keybase-url]: https://keybase.io/tunnckocore 115 | [keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg 116 | 117 | [author-npm-url]: https://www.npmjs.com/~tunnckocore 118 | [author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg 119 | 120 | [author-twitter-url]: https://twitter.com/tunnckoCore 121 | [author-twitter-img]: https://img.shields.io/badge/twitter-@tunnckoCore-55acee.svg 122 | 123 | [author-github-url]: https://github.com/tunnckoCore 124 | [author-github-img]: https://img.shields.io/badge/github-@tunnckoCore-4183c4.svg 125 | 126 | [freenode-url]: http://webchat.freenode.net/?channels=charlike 127 | [freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg 128 | 129 | [new-message-url]: https://github.com/tunnckoCore/messages 130 | [new-message-img]: https://img.shields.io/badge/send%20me-message-green.svg -------------------------------------------------------------------------------- /fixture.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * koa-ip-filter 3 | * 4 | * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) 5 | * Released under the MIT license. 6 | */ 7 | 8 | 'use strict' 9 | 10 | var ipFilter = require('ip-filter') 11 | 12 | /** 13 | * > Filtering incoming request with glob patterns 14 | * array, regexp, string or matcher function 15 | * 16 | * **Example** 17 | * ```js 18 | * 'use strict' 19 | * 20 | * var koa = require('koa') 21 | * var ipFilter = require('koa-ip-filter') 22 | * var helloWorld = require('koa-hello-world') 23 | * 24 | * var app = koa() 25 | * 26 | * app 27 | * .use(ipFilter({ 28 | * forbidden: '403: Get out of here!', 29 | * filter: ['127.??.6*.12', '!1.2.*.4'] 30 | * })) 31 | * .use(helloWorld()) 32 | * 33 | * app.listen(1234) 34 | * console.log('koa server start listening on http://localhost:1234') 35 | * 36 | * // if your IP is `127.43.65.12` you will see `Hello World` 37 | * // otherwise you will see `403: Get out of here!` 38 | * ``` 39 | * 40 | * @name koaIpFilter 41 | * @param {Object} `options` 42 | * @option {Function} [options] `id` custom identifier, defaults to `this.ip` 43 | * @option {Boolean} [options] `strict` to throw when not valid IPv4/IPv6? default `true` 44 | * @option {Array|String|RegExp|Function} [options] `filter` black/white list filter 45 | * @option {String|Function} [options] `forbidden` custom message when `403 Forbidden` response 46 | * @return {GeneratorFunction} 47 | * @api public 48 | */ 49 | 50 | module.exports = function koaIpFilter (options) { 51 | options = typeof options === 'object' ? options : {} 52 | 53 | return function * (next) { 54 | var id = typeof options.id === 'function' ? options.id.call(this, this) : this.ip 55 | 56 | if (!id || !options.filter) { 57 | return yield * next 58 | } 59 | 60 | // always strict 61 | var strict = typeof options.strict === 'boolean' ? options.strict : true 62 | var forbidden = options.forbidden || '403 Forbidden' 63 | 64 | /** 65 | * [identifier description] 66 | * @type {Object} 67 | */ 68 | var identifier = ipFilter(id, options.filter, !strict) 69 | if (identifier === null) { 70 | this.status = 403 71 | this.body = typeof forbidden === 'function' ? forbidden.call(this, this) : forbidden 72 | return 73 | } 74 | 75 | this.filter = ipFilter 76 | this.identifier = identifier 77 | 78 | return yield * next 79 | } 80 | } 81 | 82 | /** 83 | * abcd description 84 | * 85 | * @name koaIpFilter 86 | * @param {Object} `options` 87 | */ 88 | exports.data = function () { 89 | return true 90 | } 91 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * get-comments 3 | * 4 | * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) 5 | * Released under the MIT license. 6 | */ 7 | 8 | /* jshint asi:true */ 9 | 10 | 'use strict' 11 | 12 | /** 13 | * Get block code comments from string 14 | * 15 | * @param {String} `` string to extract from 16 | * @param {Boolean} `[resType]` if true, will return array, otherwise object 17 | * @return {Array|Object} 18 | * @api public 19 | */ 20 | module.exports = function getComments (input, resType) { 21 | if (typeof input !== 'string') { 22 | throw new TypeError('get-comments expects a string') 23 | } 24 | 25 | var len = input.length 26 | var obj = {start: 0, end: 0} 27 | var res = resType ? [] : {} 28 | var raw = '' 29 | var column = 0 30 | var line = 1 31 | var i = 0 32 | 33 | while (i < len) { 34 | var ch = input[i] 35 | var prev = input[i - 1] 36 | var next = input[i + 1] 37 | var post = input[i + 2] 38 | 39 | raw += ch 40 | 41 | if (prev !== ' ' && ch === '/' && next === '*' && post === '*') { 42 | obj.type = 'Block' 43 | obj.start = i 44 | obj.loc = {} 45 | obj.loc.start = { 46 | line: line, 47 | column: column 48 | } 49 | } 50 | if (obj.start && prev === '*' && ch === '/') { 51 | var value = raw.slice(obj.start + 2, i - 1) 52 | 53 | obj.api = value.indexOf('@api') !== -1 54 | obj.end = i + 1 55 | obj.value = value 56 | obj.loc.end = { 57 | line: line, 58 | column: column 59 | } 60 | } 61 | if (obj.start && prev === '/' && (ch === '\n' || ch === '\r')) { 62 | var lines = input.split(/[\r\n]/) 63 | obj.after = lines[line] 64 | if (!lines[line].length) { 65 | obj.after = lines[line + 1] 66 | } 67 | if (resType) { 68 | res.push(obj) 69 | } else { 70 | var l = Object.keys(res).length 71 | var j = l > 0 ? l : 0 72 | res[j] = obj 73 | } 74 | 75 | // resets 76 | obj = {start: 0, end: 0} 77 | } 78 | if (ch === '\n' || ch === '\r') { 79 | line++ 80 | column = 0 81 | } 82 | 83 | column++ 84 | i++ 85 | } 86 | 87 | return res 88 | } 89 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "get-comments", 3 | "version": "1.0.1", 4 | "description": "Extract javascript comments on per character basis. Comment object compatible with `acorn-extract-comments` and Esprima format.", 5 | "repository": "tunnckoCore/get-comments", 6 | "author": "Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)", 7 | "main": "index.js", 8 | "license": "MIT", 9 | "scripts": { 10 | "test": "standard && node test.js" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "assertit": "^0.1.0" 15 | }, 16 | "keywords": [ 17 | "array", 18 | "code", 19 | "codecomments", 20 | "comments", 21 | "esprima", 22 | "extract", 23 | "jscomments", 24 | "locations", 25 | "object" 26 | ] 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * get-comments 3 | * 4 | * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) 5 | * Released under the MIT license. 6 | */ 7 | 8 | /* jshint asi:true */ 9 | 10 | 'use strict' 11 | 12 | var fs = require('fs') 13 | var test = require('assertit') 14 | var getComments = require('./index') 15 | 16 | test('get-comments:', function () { 17 | test('should throw TypeError if not string given', function (done) { 18 | function fixture () { 19 | getComments() 20 | } 21 | 22 | test.throws(fixture, TypeError) 23 | test.throws(fixture, /get-comments expects a string/) 24 | done() 25 | }) 26 | test('should extract comments as object when getComments(input)', function (done) { 27 | var input = fs.readFileSync('./fixture.js', 'utf8') 28 | var actual = getComments(input) 29 | 30 | test.equal(typeof actual, 'object') 31 | test.equal(Object.keys(actual).length, 2) 32 | test.ok(actual[0].after) 33 | test.ok(actual[0].after.indexOf('module.exports = function koaIpFilter') !== -1) 34 | done() 35 | }) 36 | test('should extract comments as array when getComments(input, true)', function (done) { 37 | var input = fs.readFileSync('./fixture.js', 'utf8') 38 | var actual = getComments(input, true) 39 | 40 | test.equal(Array.isArray(actual), true) 41 | test.equal(actual.length, 2) 42 | test.ok(actual[1].after) 43 | test.ok(actual[1].after.indexOf('exports.data =') !== -1) 44 | done() 45 | }) 46 | }) 47 | --------------------------------------------------------------------------------