├── index.coffee ├── README.md ├── .gitignore ├── package.json └── src └── hubot-caniuse.coffee /index.coffee: -------------------------------------------------------------------------------- 1 | fs = require 'fs' 2 | path = require 'path' 3 | 4 | module.exports = (robot, scripts) -> 5 | scriptsPath = path.resolve(__dirname, 'src') 6 | fs.exists scriptsPath, (exists) -> 7 | if exists 8 | for script in fs.readdirSync(scriptsPath) 9 | if scripts? and '*' not in scripts 10 | robot.loadFile(scriptsPath, script) if script in scripts 11 | else 12 | robot.loadFile(scriptsPath, script) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### hubot-caniuse 2 | 3 | #### Description 4 | A caniuse.com integration right inside your slack channel. 5 | Ask hubot about browser support! 6 | 7 | #### Installation 8 | `npm install --save hubot-caniuse` 9 | 10 | add `["hubot-caniuse"]` to your `external-scripts.json`. 11 | 12 | #### Examples 13 | `hubot caniuse borde-radius` -> 14 | 15 | ``` 16 | CSS3 Border-radius (rounded corners)* (http://caniuse.com/#feat=border-radius): 17 | `ie:9+, firefox:3+, chrome:4+, safari:3.1+, opera:10.5+, ios_saf:3.2+, op_mini:-, android:2.1+, bb:7+, op_mob:11+, and_chr:40+, and_ff:33+, ie_mob:10+, and_uc:9.9+ 18 | ``` 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 27 | node_modules 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hubot-caniuse", 3 | "version": "1.0.2", 4 | "description": "A caniuse hubot script", 5 | "main": "index.coffee", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "hubot", 11 | "hubot-scripts" 12 | ], 13 | 14 | "repository": { 15 | "type": "git", 16 | "url": "git@github.com:altryne/hubot-caniuse.git" 17 | }, 18 | 19 | "bugs": { 20 | "url": "https://github.com/altryne/hubot-caniuse/issues" 21 | }, 22 | 23 | "dependencies": { 24 | "fuzzy": "^0.1.0", 25 | "request": "^2.51.0", 26 | "lodash": "^3.3.1" 27 | }, 28 | "author": "Alex Volkov (http://alexw.me)", 29 | "license": "ISC" 30 | } 31 | -------------------------------------------------------------------------------- /src/hubot-caniuse.coffee: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Display browser support stats for a requested feature from caniuse.com 3 | # 4 | # Dependencies: 5 | # "fuzzy": "^0.1.0", 6 | # "request": "^2.51.0" 7 | # 8 | # Configuration: 9 | # NONE 10 | # 11 | # Commands: 12 | # hubot caniuse|can I use 13 | # 14 | # Notes: 15 | # This might not ever work 16 | # 17 | # Author: 18 | # altryne 19 | 20 | request = require 'request' 21 | fuzzy = require 'fuzzy' 22 | _ = require 'lodash' 23 | 24 | exports.caniuse_data = [] 25 | url = "https://raw.github.com/Fyrd/caniuse/master/data.json" 26 | request {url: url}, (err, res, body) -> 27 | if err 28 | console.log "Errors getting url: #{url}" 29 | return 30 | exports.caniuse_data = JSON.parse(body).data 31 | 32 | module.exports = (robot) -> 33 | robot.respond /(?:can.?i.?use )(.*)/i, (msg) -> 34 | results = fuzzy.filter(msg.match[1], Object.keys(exports.caniuse_data)) 35 | if results.length > 2 36 | msg.send "Found more than #{results.length - 1} results. Please narrow down your search to one of the following: \n `#{_.pluck(results, 'string').join(', ')}`" 37 | else if results.length > 0 38 | msg.send prepareResult result for result in results 39 | else 40 | msg.send "Nothing found for query *#{msg.match[1]}*. Try something else, or go to caniuse.com yourself." 41 | 42 | prepareResult = (result) -> 43 | res_obj = exports.caniuse_data[result.string] 44 | return "*#{res_obj.title}* (http://caniuse.com/#feat=#{result.string}):\n `#{browserVersion res_obj.stats}`" 45 | 46 | browserVersion = (stats) -> 47 | support = [] 48 | for browser, stat of stats 49 | supported = [] 50 | for v, res of stat 51 | switch res 52 | when "y", "y x" 53 | supported.push v.split('-')[0] 54 | min_supported = _.min(supported, (x) -> parseFloat(x)) 55 | min_supported_clean = if (min_supported == Infinity) then "-" else "#{min_supported}+" 56 | support.push "#{browser}:#{min_supported_clean}" 57 | return support.join(', ') 58 | --------------------------------------------------------------------------------