Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tphummel committed Jan 15, 2018
0 parents commit 6f68246
Show file tree
Hide file tree
Showing 9 changed files with 4,708 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.nyc_output/
node_modules/
*.tar.gz
*.log
.DS_Store
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "9"
- "8"
- "7"
- "6"
install: npm install
test: npm test
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Tom Hummel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# streak

gather streaks from an ordered json array on stdin. results are printed in json to stdout.

[![Build Status](https://travis-ci.org/tphummel/streak.svg?branch=master)](https://travis-ci.org/tphummel/streak)

### setup

with npm:
```
npm install --global @tphummel/streak
```

or with homebrew:

```
brew install tphummel/util/streak
```

or download a standalone [executable](https://github.com/tphummel/streak/releases/latest) for your system. Put the file (or link) on your $PATH.

### usage

```
streak --help
```

### definitions
- streak: two or more consecutive rows that satisfy the criteria
- active streak: a streak which is still active as of the final provided row

### scope/limits
- the rows from stdin must be filled if it contains sparse data
- the rows from stdin must be ordered from oldest to newest
- the data from stdin must be filtered to include only relevant rows
- stdout streaks list is not ordered
79 changes: 79 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env node

'use strict'

const minimist = require('minimist')
const getStdin = require('get-stdin')

const lib = require('./index')

const argv = minimist(process.argv, {
string: ['label', 'column', 'min', 'max'],
boolean: ['help', 'version'],
alias: {
l: 'label',
c: 'column',
h: 'help',
v: 'version'
},
default: {
label: 0,
column: 1,
min: 1
}
})

if (argv.help) {
printUsage()
process.exit(0)
}

if (argv.version) {
console.log(require('./package.json').version)
process.exit(0)
}

getStdin().then(str => {
try {
var input = JSON.parse(str)
} catch (e) {
console.log(`stdin was not valid valid json. failed to parse`)
process.exit(1)
}

const result = lib({
data: input,
displayColumn: argv.label,
criteria: {
column: argv.column,
minValue: argv.min,
maxValue: argv.max
}
})
console.log(JSON.stringify(result))
})

function printUsage () {
const usage = `
Streak.
Usage:
streak --label|-l <idx> --column|-c <idx> --min <val> [--max <val>]
streak -h | --help
streak -v | --version
Options:
-l --label index of column holding label. zero-indexed. [default: 0]
-c --column index of column holding criteria. zero-indexed. [default: 1]
--min Numeric criteria to evaluate criteria field (>=). [default: 1]
--max Numeric criteria to evaluate criteria field (<=)
-h --help Show this screen.
-v --version Show version.
Example:
echo -n '[["2017-01-01", 3],["2017-01-02", 5],["2017-01-03", 5],["2017-01-07", 1]]' \\
| streak -l 0 -c 1 --min 5
`
console.log(usage)
}
62 changes: 62 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

function rowSatisfiesCriteria ({row, criteria}) {
if (criteria && criteria.minValue != null) {
if (row[criteria.column] < criteria.minValue) {
return false
}
}

if (criteria && criteria.maxValue != null) {
if (row[criteria.column] > criteria.maxValue) {
return false
}
}

return true
}

function getStreaks ({data, displayColumn, criteria}) {
let streaks = []
let active = []
const minStreakLength = 2

data.forEach(row => {
let streakInProgress = active.length > 0
let criteriaSatisfied = rowSatisfiesCriteria({row, criteria})

if (streakInProgress) {
if (criteriaSatisfied) {
active.push(row)
} else {
if (active.length >= minStreakLength) streaks.push(active)
active = []
}
} else {
if (criteriaSatisfied) active.push(row)
}
})

let output = streaks.map(raw => {
return {
start: raw[0][displayColumn],
end: raw[raw.length - 1][displayColumn],
value: raw.length,
active: false
}
})

if (active.length >= minStreakLength) {
output.push({
start: active[0][displayColumn],
end: active[active.length - 1][displayColumn],
value: active.length,
active: true
})
}

return output
}

module.exports = getStreaks
module.exports.rowSatisfiesCriteria = rowSatisfiesCriteria
Loading

0 comments on commit 6f68246

Please sign in to comment.