Skip to content

chauchakching/bisect-any

Repository files navigation

bisect-any

Build Version

Like git-bisect, but on arbitrary function or command to find the "bad" value.

Usage

# install globally
npm install -g bisect-any

# help
bisect-any --help

Examples - CLI

JS function

Run a default export function from a js file with bisect values

E.g. To find the first integer that it's square is larger than 300 (answer: 18)

Example js function test/isSquareLessThan300.js

bisect-any --file test/is-square-less-than300.js 1 100

# Running bisect...... 
# Found value: 18
# {
#   totalRounds: 8,
#   records: [
#     { goodVal: 1, badVal: 100 },
#     { goodVal: 1, badVal: 50 },
#     { goodVal: 1, badVal: 25 },
#     { goodVal: 13, badVal: 25 },
#     { goodVal: 13, badVal: 19 },
#     { goodVal: 16, badVal: 19 },
#     { goodVal: 17, badVal: 19 },
#     { goodVal: 17, badVal: 18 }
#   ]
# }

CLI command

Run a CLI command with bisect values. If the command exit with non-zero code, it's considered a bad value.

Use % as placeholder of bisect value in command string.

Example js program test/is-square-less-than300.js

bisect-any --command "node test/is-square-less-than300.js %" 1 100

Examples - library

const { bisect } = require('bisect-any')

(async () => {
  const [result, report] = await bisect({
    start: 1,
    end: 100,
    check: (val) => {
      return val ** 2 <= 300;
    },
  })
})()
/**
 * result === 18
 * 
 * report == {
 *   totalRounds: 8,
 *   records: [
 *     { goodVal: 1, badVal: 100 },
 *     { goodVal: 1, badVal: 50 },
 *     { goodVal: 1, badVal: 25 },
 *     { goodVal: 13, badVal: 25 },
 *     { goodVal: 13, badVal: 19 },
 *     { goodVal: 16, badVal: 19 },
 *     { goodVal: 17, badVal: 19 },
 *     { goodVal: 17, badVal: 18 }
 *   ]
 * }