sgentle / caniuse-cmd

Caniuse command line tool
MIT License
1.62k stars 51 forks source link

Can I configure a usage threshold? #16

Open wamoyo opened 7 years ago

wamoyo commented 7 years ago

Era is great, but I'd love to be able to only display browsers and usage percentage for only those browser above 1% or some other configurable threshold.

Just a feature idea : )

sgentle commented 7 years ago

Hey, neat idea! Would you be interested in implementing it?

The way it works at the moment is that the makeResults function loops through the versions in the caniuse data and checks them against the versionrange (line 164). Probably the simplest thing would be to add a condition in that loop to check if usage (defined on line 166) is less than the threshold and continue if it is.

That behaviour will cause the tool to ignore below-threshold versions completely. For example, if Firefox 123.45 (10% usage) supports a feature, Firefox 123.46 (0.1% usage) doesn't, and Firefox 123.47 (30% usage) does, we'll display that as "Firefox: ✓123.45+". I think that's in line with user intention, but it might be worth making sure the parameter description reflects it.

wamoyo commented 7 years ago

Woah, that was cool! It actually works : )

Well, almost. It still lists the browser without its versions if they're all under the threshold

For example:

 ./bin.js svg -t 1
SVG (basic support) ✔ 96.78% ◒ 0.75% [W3C Recommendation]
  Method of displaying basic Vector Graphics features using the embed or object elements. Refers to the SVG 1.1 spec.
  #SVG

  IE ✔ ² (4%) 
  Edge ✔ ² (1%) 
  Firefox ✔ (5%) 
  Chrome ✔ (25%) 
  Safari ✔ (1%) 
  Opera
  iOS Safari ✔ (8%) 
  Opera Mini ✔ (4%) 
  Android Browser ✔ (2%) 
  Blackberry Browser
  Opera Mobile
  Chrome for Android ✔ (23%) 
  Firefox for Android
  IE Mobile
  UC Browser for Android ✔ (9%) 
  Samsung Internet ✔ (3%) 

And here's the code I added:

  .option 'threshold',
    alias: 't'
    type: 'number'
    default: 0
    describe: "Show only browsers above usage percentage threshold"

And....

makeResults = (browser, stats) ->
  results = []
  current = {}
  for version, i in browser.versions when version and versionrange[0] <= i <= versionrange[1]
    support = stats[version]
    usage = browser.usage_global[version] || 0
    version += '+' if browser.versions[i + 1]
    threshold = argv.threshold

    # 'p' means no-but-polyfill-available, which we can treat as no
    if support[0] == "p"
      support = "n" + support.slice(1)

    # Check if usage is greater than the threshold, and continue if it is
    if usage <= threshold
      continue

    # Only add a new version result when browser support changes
    if !current.version || current.support != support
      current = version: version, support: support, usage: 0
      results.push current

    current.usage += usage

  results
wamoyo commented 7 years ago

Sorry meant to click comment, hit close by accident.

wamoyo commented 7 years ago

Noticing decimals don't quite work either...

 (master) $ ./bin.js svg -t .3
SVG (basic support) ✔ 96.78% ◒ 0.75% [W3C Recommendation]
  Method of displaying basic Vector Graphics features using the embed or object elements. Refers to the SVG 1.1 spec.
  #SVG

  IE ✘ 8+ (0%) ✔ 11² (4%) 
  Edge ✔ ² (1%) 
  Firefox ✔ (5%) 
  Chrome ✔ (26%) 
  Safari ✔ (2%) 
  Opera ✔ (0%) 
  iOS Safari ✔ (9%) 
  Opera Mini ✔ (4%) 
  Android Browser ◒ 4.2-4.3+¹ (0%) ✔ 4.4+ (2%) 
  Blackberry Browser
  Opera Mobile ✔ (0%) 
  Chrome for Android ✔ (23%) 
  Firefox for Android
  IE Mobile ✔ ² (0%) 
  UC Browser for Android ✔ (9%) 
  Samsung Internet ✔ (3%)