diff --git a/src/index.js b/src/index.ts similarity index 85% rename from src/index.js rename to src/index.ts index 856b3af..d1fdfec 100644 --- a/src/index.js +++ b/src/index.ts @@ -22,11 +22,13 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -function compare(v1, v2) { +export type CompareFunction = (v1: number, v2: number) => Boolean; + +function compare(v1: number, v2: number) { return v1 < v2; } -function upperBound(array, value, comp = compare) { +function upperBound(array: number[], value: number, comp: CompareFunction = compare) { let len = array.length; let i = 0; @@ -43,7 +45,7 @@ function upperBound(array, value, comp = compare) { return i; } -function lowerBound(array, value, comp = compare) { +function lowerBound(array: number[], value: number, comp: CompareFunction = compare) { let len = array.length; let i = 0; @@ -60,7 +62,7 @@ function lowerBound(array, value, comp = compare) { return i; } -function binarySearch(array, value, comp = compare) { +function binarySearch(array: number[], value: number, comp: CompareFunction = compare) { let cursor = lowerBound(array, value, comp); return cursor !== array.length && !comp(value, array[cursor]); } diff --git a/test/binary-search.js b/test/binary-search.js index 0d10186..7d54459 100644 --- a/test/binary-search.js +++ b/test/binary-search.js @@ -23,7 +23,7 @@ */ import assert from 'assert' -import { binarySearch } from '../src/index.js' +import { binarySearch } from '../src/index' describe('binarySearch', () => { it('less', () => { diff --git a/test/lower-bound.js b/test/lower-bound.js index df8f58a..b4b1a00 100644 --- a/test/lower-bound.js +++ b/test/lower-bound.js @@ -23,7 +23,7 @@ */ import assert from 'assert' -import { lowerBound } from '../src/index.js' +import { lowerBound } from '../src/index' describe('lower-bound', () => { it('less', () => { diff --git a/test/tsconfig.json b/test/tsconfig.json index cb359a9..aba9c36 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -2,10 +2,11 @@ "extends": "../tsconfig.json", "compilerOptions": { "declaration": false, - "noEmit": true + "noEmit": true, + "allowJs": true }, "include": [ "../src/**/*", "./**/*" ] -} \ No newline at end of file +} diff --git a/test/upper-bound.js b/test/upper-bound.js index f2419c7..a70bc2c 100644 --- a/test/upper-bound.js +++ b/test/upper-bound.js @@ -23,7 +23,7 @@ */ import assert from 'assert' -import { upperBound } from '../src/index.js' +import { upperBound } from '../src/index' describe('upper-bound', () => { it('less', () => { diff --git a/tsconfig.json b/tsconfig.json index 6e59769..2ecd1be 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,6 @@ "noUnusedParameters": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, - "allowJs": true }, "include": [ "src/**/*"